Hi,

I'm trying to read in a section of a line from a file, and to compare that to an integer. I know that the piece I'm reading in will be 3 numbers (xxx) and be followed by a space. It will also be the first entry on each line (I know this as I did the output).

What I need to be able to do is grab that number and compare it to an int to see if this is the line that I need.

But I'm having a bear of a time. From what I can tell, I can't compare the string to an int, I can't convert the string to an int to compare. And I've not had any luck trying it with a C-Style string. And help would be greatly appreciated.

Thanks
David

string number;
	string line;
	char temp;
	int index;
	int compare;
	bool success;

	ifstream in("records.txt");
	ofstream out("outfile.txt");
	if(in.is_open()){
		
		while( getline(in,line) ){
			index = line.find_first_of(" ",0,256);
			number = line.substr(0,index);
			temp = number;
			compare = atoi(temp);
			if(temp != registrationID){
				out << line << "\n";
			}

Hi,

I'm trying to read in a section of a line from a file, and to compare that to an integer. I know that the piece I'm reading in will be 3 numbers (xxx) and be followed by a space. It will also be the first entry on each line (I know this as I did the output).

What I need to be able to do is grab that number and compare it to an int to see if this is the line that I need.

But I'm having a bear of a time. From what I can tell, I can't compare the string to an int, I can't convert the string to an int to compare. And I've not had any luck trying it with a C-Style string. And help would be greatly appreciated.

Thanks
David

string number;
	string line;
	char temp;
	int index;
	int compare;
	bool success;

	ifstream in("records.txt");
	ofstream out("outfile.txt");
	if(in.is_open()){
		
		while( getline(in,line) ){
			index = line.find_first_of(" ",0,256);
			number = line.substr(0,index);
			temp = number;
			compare = atoi(temp);
			if(temp != registrationID){
				out << line << "\n";
			}

atoi takes const char* as an argument, not char. You have temp declared as char. You are assigning a string to a char in this line:

temp = number;

You pass a char to the atoi function in this line:

compare = atoi(temp);

I don't know what type registrationID is, but you are comparing it to a char in this line:

if(temp != registrationID)

You also don't use the compare variable that you assigned the value of the atoi function, so why did you use the atoi function at all? Should that line be this:

if(compare != registrationID)

instead of this?

if(temp != registrationID)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.