C++ :: Search Specified File For All Instances Of Substring

Mar 24, 2012

I'm trying to search a specified file for all instances of substring "abbaa."

The professor is making us use a 6x3 array in order to understand finite state machines.

Well the code I wrote runs perfectly, but the case for '/n' doesn't catch. It just flows through without it.

Significant portion of code:

Code:
if(fin) {
fin >> inChar;
while(fin) {
switch(inChar) {
case 'a':
currentState = state[currentState][a];

[Code] .....

View 3 Replies


ADVERTISEMENT

C++ :: How To Search For A Substring In A File

Jan 12, 2013

How I can search a sub string in a file? Length of sub string is Variable and the number of search is indefinite.

View 15 Replies View Related

C++ :: Substring / Loops And Writing To A File

May 21, 2013

Here are the requirements for a project I am working on.

Write a function that will write only unique ip addresses to a file (just the ip address, not the entire line).

Write a function that will search for an ip address and display all page requests for that ip address.

The ip is pulled from a "weblog.txt" file which has been put into a vector and sorted. I am having trouble with the loop to pull only unique ip addresses from the vector.

Code:
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
#include<algorithm>
void nonmember();
void sortnonmember(std::vector<std::string>& webvector);

[Code] .....

View 1 Replies View Related

C Sharp :: Substring From Number In Text File

Oct 15, 2013

How to substring first 8 digits from number?

I have text file with around 10k lines. Each line contains number with length 12-14 digits. I want to take only first 8 digits from each number and writ it in new file.

View 1 Replies View Related

C++ :: Class Instances To Text File

Mar 29, 2015

Code:
#include<iostream>
#include<conio.h>
#include<string.h>
#include<ostream>
#include<fstream>
#include<iomanip>
using namespace std;
class MathProblem {

[Code] ...

So my program is quite simple, have the user answer the answer to a question, and then compare with the correct answer. I needed to implement an inheritance of MathProblem in my second class aswell.

The program runs as I intend but I wish to have the data forwarded to a text file. After that I must read back to the command prompt the text file contents. It's fairly easy for simple statements but I don't understand how to forward all my data from my classes.

View 3 Replies View Related

C/C++ :: Create Class Instances From Text File Stream

Nov 10, 2014

I am trying to do some exercises but am struggling at the moment. The first task was to create a class (Planet), then allow the user to edit their entry or view it.

The second one is to create class instances from a text file. The file contains a new planet on each line in the form: 'id x y z' where x/y/z are its coordinates. As the file can have more then one lines, it has to dynamically create an undefined amount of class instances.

To do this I used 'new' and it works ok - it prints each one out to the screen as you go so you can see it working. However... I'm trying to get into good habits here and am encapsulating the class which is where I am getting stuck. I can read from the class but cannot put the values from the file into the class.. ..using the member functions I have created anyway.

My code so far is:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Planet {
private:
int id=0;
float x_coord=0.0, y_coord=0.0, z_coord=0.0;
public:
int GetID(){return id;}

[code]....

If I change the SetID etc to just p->id, p->x_coord etc it works fine. But I'd rather find a way to do it that keeps the encapsulation. Using p->z_coord etc requires that you change the class variables from private to public.

The question I have been given is this:

Define and implement a function, generate planet, that takes a stream argument that has already been connected to a file (i.e. the argument is istream& fin). The function must create a new instance of planet using new and read its details from the next line in the file.Each line of the file is in the format id x y z.The function must return the newly created planet.

Also, how would you go about 'viewing' one specific class instance once they've been created? So say the file had 5 lines, line three was '4 6 2 6'. How would I go about viewing that planet afterwards? I don't think thats required but... I'm just wondering Although I'm also wondering, are we actually creating a new class instance for each line here? Or just destroying the previous one?

View 14 Replies View Related

C++ :: Read In Lines From A File / Store In Variables Then Construct Instances Of Class

Aug 22, 2013

I can't get my code to compile, i need to read in lines from a file and store them in variables. Then i have to construct instances of my class for how many lines there are in the file and take those variables into them.

I'm getting this error :

"a2.cpp:40: error: cannot convert `Employee' to `Employee*' in assignment"

#include<iostream>
#include<string>
#include<fstream>
void displayInfo();
using namespace std;
class Employee{

[Code] .....

View 1 Replies View Related

C++ :: How To Do Substring To Get Last 4 Characters

May 8, 2013

let says i have char *yytext = "p103_stb" 0r "p0_stb" or "p100_stb"

How to do substr to get the "_stb" in C?

View 12 Replies View Related

C++ :: How To Remove A Substring

Aug 4, 2014

I have a C-based string. trying to remove some sub-string, I got at:

char str[]="alireza tavakkoli";
char* pos = strstr( str, "reza");
for(int j=pos-&str[0];j<pos-&str[0]+strlen("reza");++j)
str[j] = 7;

But I don't like the last line.isn't there a better solution?

View 7 Replies View Related

C++ :: Find Substring From A String

Apr 23, 2012

I am stuck in some logic and want to write a program to do following tasks : I have three string variables to be compared to each other,a string having sub string & hierarchy string!!

1.) name1=john name2=tom_john_tom name3=alextom_john
thus we need to search john from name2 and name3 and if name1 exists in both name2 and name3 then ok else check for step2

2.) name1=a.b.c.d ,name2=a.b.c.d and name3=a.b.c.d
we need to compare each string seperated by a dot in all three variables and we need to match each string seperated by a delimeter "."
if name1.a==name2.a==name3.a and name1.b==name2.b==name3.b ,name1.c==name2.c==name3.c and name1.d==name2.d==name3.d then its a match
else its a mismatch

Also,the catch is we can have name1 ,name2 and name3 in format name1=*.*.*.* and name2=*.*.*.* and name3=*.*.*.*
where * defines it can be any value to be matched

name1=government.india.public.property
name2=rural.roads.maximum.x
name3=government.india.public.sales
name4=rural.roads.minumum.x

If operator wants to match only the second field , then the logic should be like:

If the Value is to be matched = (*.#.*.*)
then "matched"
Else
its a mismacth

# => for this field of name2 and name3 shall be same
* => for this field name2 and name3 shall be ignored for comparison

to obtain option1 we can have find function and substr however pls suggest how to approach for second option !!!!

View 1 Replies View Related

C :: Parsing A Line To Extract A Particular Substring?

Mar 14, 2014

Suppose I have read a line from an ASCII file with fgets(). Now I want to parse the line, which looks something like this: Code: # John Q. Public et al. 2014, to be submitted The name, "John Q. Public" is what I want. However, the name can be anything, consisting of 1 or more tokens separated by spaces. it could be "John" Or "John Public", or "Thurston Howell the 3rd", or etc... Bascially, I need to get the entire substring between the first hash mark, and the "et al" in the line. I tried this: Code: sscanf(line,"# %s et al.",name); But I can only get the first token (which, in this case, is "John").

View 2 Replies View Related

C/C++ :: Finding And Printing A Substring In A String?

Feb 21, 2015

i couldnt solve the algorithm exactly. The program asks the user for a string. "Bugun h@v@ cok g'uzel" for example. then the program asks for another string. If that string doesnt exists in the main string program should say there isnt any substring. If it exist then the program should print the remaining part of main string. For example:

Write the first string: Tod@y weather is be'@utiful
write the substring : ug
>>ugun h@v@ cok guzel
write the substring :wldnqwbdbj
>>there isnt any substring
Here where i came so far

#include <stdio.h>
int main()
{
char mainstr[50],substr[50];

[Code]....

View 7 Replies View Related

C/C++ :: How To Compare A String To A Vector Substring

May 9, 2014

I would like to compare two strings, one of which is an independently stored string - the other being a string within a vector. However, i do not know how to do this, as

string thisString = "yes";
vector<string> stringHere;
stringHere.push_back("no");
if (thisString == stringHere[0]) {
cout << "It worked." << endll;
}

does not appear to work.

View 2 Replies View Related

C++ :: Given A String How To Find Longest Substring With All Unique Characters

Feb 9, 2015

I am new to C++ programming, writing a program for the below question:

given a string, how to find the longest substring with all unique characters.

View 1 Replies View Related

C++ ::  Calling All Instances Of Object?

May 28, 2013

So I am wondering how I would go about calling a function for all instances of an object. I tried googling it, but all I saw was solutions like making an array of pointers to the objects. What if I don't know how many objects there will be? Isn't there an easier way?

View 19 Replies View Related

C++ ::  how To Correctly Initialize Instances Of A Class

Feb 1, 2015

I am struggling to understand what the correct way to initialize instances of a class for object orient programming is.

If I am not mistaken, there are 2 methods to initialize an instance "instance_name" of CLASS1 by calling the constructor:

(1) CLASS1 instance_name(argument);
(2) CLASS1 instance_name=CLASS1(argument);

As it turns out, I cannot use method (1) when I try to initialize a private instance inside another class. But I can use it in the main() code. I hope the code below will explain what I mean. Why only method (2) will work inside another class? Or is there another fundamental mistake I am making?

(BTW: I used CodeBlocks 13.12 with GNU GCC compiler for this example)

#include <iostream>
using namespace std;
class CLASS1{

[Code].....

View 6 Replies View Related

C++ :: Set Member To Same Value In All Instances Of Templated Structure

Sep 3, 2013

template <typename T> struct avl_tree {
T data;
int balance;
struct avl_tree <T> *Link[2];
static int (*comp)(T, T);
};

In main, I have a function like so:
int compare(int a, int b) {
return ( a - b );
}

Now how do I assign the function pointer in my avl_tree class to the compare function?

I did this:
int (avl_tree<int>::*comp)(int, int) = compare;

But I got the compiler error:
Tree_Test.cc: In function ‘int main()’:
Tree_Test.cc:27:42: error: cannot convert ‘int (*)(int, int)’ to
‘int (avl_tree<int>::*)(int, int)’ in initialization

View 12 Replies View Related

C++ :: Static Const Class Instances?

Jun 17, 2012

i have this rather large class, which (in a way) somehow resembles a custom dialog control). This control is supposed to display data, which it does just fine. To do so, it maintains a

byte settings[10];

array, which holds information on how to display the data.

There are multiple ways to represent this custom set of data.In order to remain flexible in representing it, i thought of implementing some sort of DisplayProvider, which can be registered to the base class and provides that settings byte array.

Preferably, i would now have a set of static const instances of this provider.Using a struct would work nicely here:

PHP Code:

struct DisplayProvider
{
int settings[10];
}
static const DisplayProvider prov1 = {1,2,3,4,5,6,7,8,9,0}; 

The problem: The DisplayProvider would have to do some pre-processing, before handing over control to the base class, which then does the main work.I would end up with something like this:

PHP Code:

class DispalyProvider
{
baseclass* owner;
 int settings[10];
void PreProcessing(...);//ends up calling the owner.Processing(...) function
}; 

The main thing here is, that i dont really see a way to create a stock of default "static const DisplayProvder = {...}"s, as i could when using a struct.

View 7 Replies View Related

C :: Making Fork Call And Have The Instances Run Every Five Minutes

Dec 18, 2014

Understanding Fork() calls. I have to write a C program in UNIX machine, and make two Fork Calls Inside it and have these two instances run every five minutes all round the clock. How to achieve this.

View 1 Replies View Related

C/C++ :: Create Local Instances Of A Global Variable?

Oct 4, 2012

is it possible to have a global variable pointing to a different address depending on the thread?

Imagine one would like to use threads with the loop:

for (i=0;i<n;i++){
globalPointerVariable=getAddress(i);
DoThingsUsingThe_globalPointerVariable();
}

View 4 Replies View Related

C :: Filling Structure Client / Storing Different Instances In Array

Mar 31, 2013

Here is what I have going on.

Code:

struct client {
char firstName[stnd];
char lastName[stnd];
long ID;
char email[stnd];
float funds;
float wager;
}

typedef client...I would like to have these fields filled with this function below and stored in an array... Basically Multiple users and this is my function for it, if I can get it to work proper -.- .... I don't get syntax errors but I do get warnings

Code:

void getct(client *cl, int *pclientCounter) {
char input[buff];
char *pinput = NULL;
int typef = 0;
int lengthf = 0;
}

[code]....

View 3 Replies View Related

C++ :: Hold Vector Of Pointers In Same Class As Instances Of Those Objects?

Feb 10, 2015

Using SFML, I had a Board class which held multiple vectors of all of my object types in the game, and then it also held a vector of pointers to the memory addresses of these object instances, like this

class Board{
//...
std::vector<AbstractObject*> GetAllLevelObjects(){ return allLevelObjects; }
//so these are used to hold my object instances for each level

[Code]....

When looping through this vector and drawing the sprites of the objects, I get the runtime error 0xC0000005: Access violation reading location 0x00277000. I solved this error by storing the vector of pointers in the class that holds my Board instance, but I'm wondering why only this solution worked? Why couldn't I just have my vector of pointers in the same class that the instances of those objects were in?

View 2 Replies View Related

C/C++ :: Remove All Instances Of Element Within List - While Loop Error

Jun 23, 2014

I am getting an error with my while loop(feels dumb) when running this code that I am allowed to modify. It is to remove all instances of an element within the list.

The loop is whithin this function:

void arrayListType<elemType>::removeAll(int location)

I have attached both the header file and main function file in .txt format.

void arrayListType<elemType>::removeAll(int location)*

Attached File(s) : 
header.txt (12.23K)
main function.txt (2.95K)

View 2 Replies View Related

C++ :: Search And Find The Shortest Queue And Search After Some Condition?

Mar 7, 2013

I am trying to implement a Task scheduler where i have n number of tasks. The Idea behind my task scheduler is that in a loop of queues of a vector, task should get enqueued to the shortest queue among the loop of queues, which is done by the following code.

#include <vector>
#include <queue>
std::vector<std::queue<int> > q
int min_index = 0;
task t // implemented in the other part of the program

[Code] ....

Next i am trying to extend this paradigm to reduce the overhead time of the scheduler, Instead of searching the shortest queue every time, search after some condition ie. search the shortest queue after 5 tasks gets enqueued to the shortest queue.

i need to do something like this

#include <vector>
#include <queue>
std::vector<std::queue<int> > q
task t // implemented in the other part of the program
while(q[min_index].size()!=q[min_index].size()+5) // check whether current min_index queue's size is increased 5 more times if not goto enqueue

[code].....

View 1 Replies View Related

C :: Search File For String

Aug 22, 2014

I should state that I am recently new to C programming. I have dabbled with Python in the past, but nothing formal. I am taking a C programming class, but it is an introductory course. As such, the instructor is moving extremely slow through the material.

With this program, I am trying to search a given text file (line-by-line) for user-submitted data. I then want to be able to store which line that data was found on. The program runs, but it always results in "Sorry, couldn't find a match." Even if it shouldn't.

Code:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
}

[code]...

I should probably explain my overall goal with this program.I would like to write a program that will calculate the molecular weight of a compound. I expect the user entered molecular compound to be something like this: CH3COOH (An equivalent entry could be C2H4O2)Also, it needs to be case sensitive because the chemical formulas are case sensitive.

The output will be "The molecular weight of CH3COOH is xxx.xxxxx grams".I have two text files as well. One contains a list of all chemical symbols, and the second is a list of all atomic weights in the same order as the first. I want to place the user inputted data into an array, then search for each element of that array in the chemical symbol text file. It will show which line it was found on, then I can use that line to grab the atomic weight from the second text file.

View 5 Replies View Related

C++ :: File Info Search?

Feb 27, 2013

I am looking for a way that I could check the information of a folder and a drive, to show the data size and storage amount (KB, MB, GB). This is so that I can compare the size of a folder to the storage capacity of a disk drive.

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved