I'm finding issue with my current program. I built the program so that it reads an input code from a file and it is also supposed to display only the code's appropriate salary. However, for some reason, everything I've tried only results in all the salaries being pulled together, not one individually. Am I overlooking something incredibly simple here?

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	//displays the payrolls stored in the Intermediate24.txt
	string payRoll = "";
	string code = "";

	//create file object and open the file

	ifstream inFile;
	inFile.open("Intermediate24.txt", ios::in);
	

	//determine whether the file was opened
	if (inFile.is_open())
	{
		//enter a payroll code
		cout << "Please enter a code: ";
		cin >> code; 

		//read a code
		getline(inFile, code, '#');
		getline(inFile, payRoll);
		

		while (!inFile.eof())
		{
			//display the payroll
			cout << code << "." <<
				payRoll << endl;

			//read another code
			getline(inFile, code, '#');
			getline(inFile, payRoll);
			
		}//end while

		//close the file
		inFile.close();
	}
	else
		cout << "File could not be opened" << endl;
	//end if

system("pause");
return 0;
} //end of main function

You are printing everything... in the while( ! eof ) there should be an if before the cout.

You have 1 'code' variable, that you both use for the input from the user (cin >> code) and when reading from the file(getline(..,code,..)).

You need two variables.. one for the user input (e.g. userCode) and the one you have now, for reading from the file.

Then you check if( userCode == code ) cout << etc.

Hope that helps

You are printing everything... in the while( ! eof ) there should be an if before the cout.

You have 1 'code' variable, that you both use for the input from the user (cin >> code) and when reading from the file(getline(..,code,..)).

You need two variables.. one for the user input (e.g. userCode) and the one you have now, for reading from the file.

Then you check if( userCode == code ) cout << etc.

Hope that helps

I followed a lot of the steps, which were helpful in getting me to see it from a more logical point of view, but I find when I added if(userCode == code) it didn't match up. Should the userCode be an int variable?

Here's the modified code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	//displays the payrolls stored in the Intermediate24.txt
	string payRoll = "";
	string code = "";
	int userCode = 0;

	//create file object and open the file
	ifstream inFile;
	inFile.open("Intermediate24.txt", ios::in);
	

	//determine whether the file was opened
	if (inFile.is_open())
	{
		//enter a payroll code
		cout << "Please enter a code: ";
		cin >> userCode; 
    
		//read a code
		getline(inFile, code, '#');
		getline(inFile, payRoll);

		while (!inFile.eof())
		{
			if(userCode == code)
			{
			//display the payroll
			cout << code << "." <<
				payRoll << endl;

			//read another code
			getline(inFile, code, '#');
			getline(inFile, payRoll);
			
			}//end if
			
		}//end while

		//close the file
		inFile.close();
	}
	else
		cout << "File could not be opened" << endl;
	//end if

system("pause");
return 0;
} //end of main function

If you compare a building to an ant, are they ever going to be the same?
userCode can be an int, but you will need to convert it to a string before comparing it with code, which is a string. Or it should be possible to change code to an int.

Also, why are the 2 getline calls (after 'read another line') inside the if block? If you only want to read another code if the current one matches userCode then this is fine.. but I guess you want to read the next one when the current code doesnt match the userCode.

If there can only be one userCode in the whole text file, you can 'break;' out of the while loop when userCode==code

Hi, your code is almost complete! But there are a couple of things that you should consider to tidy it up a bit. Firstly, when you check for a file being open, it's better to check if it's not open. This way, the whole of your code doesn't end up inside a an if() statement. So, something like:

std::string filename = "in.txt";
std::ifstream myFile;
myFile.open(filename.c_str(), std::ios::in);
if(myFile.is_open() == false){
   std::cerr << argv[0] << ": Error!  Could not open " << filename << " for input. Exiting." << std::endl;
   return 1;
}

/* now the rest of you code goes here */

Since the program exits if the file isn't opened then you don't need to have the else part of the if() statement.

Secondly, you can tidy up the while() loop by changing the order that you get the lines and print them out. So you could do something like:

while(inFile.eof() == false){
   getline(inFile, code, '#');
   getline(inFile, payRoll);
   if(userCode == atoi(code.c_str()))
      cout << code << "." << payRoll << endl;
}

These few lines would replace lines 25 to 43 in your code. I also included the atoi() function to make the string into an int.

Hope that helps a bit.

Forgive me for being a pain in the ass.

std::cerr << argv[0]

It's bad to assume that argv[0] exists, using any array, you should first check the bounds, in this case: if argc == 1. Or not print argv[0] at all ;)

It's bad to assume that argv[0] exists, using any array, you should first check the bounds, in this case: if argc == 1. Or not print argv[0] at all ;)

No problem, it's good to get these things as correct as possible for reference :o)

I thought that if you have int main(int argc, char **argv) as your definition of main, then argc is always greater than or equal to 1, since argv[0] is the name of the program? I guess this doesn't have to be the case is you're not running the program from the command line. Does it?

If not, that's what I was going for (so that you know where the error is coming from a bit more -- I tend to write small programs and use a bash script to run a bunch of them in some sequence). Anyway, this might all be polluting this thread a bit...

Discussing this kind of stuff is never polluting ;)

Your experience is that argv[0] is the program name, and argc >= 1... you're right that this is generally the case but it is never guaranteed. Someone with bad intentions can run your program without argv[0], or replace it with something else. For example on Linux, if you printf( "%s\n", argv[0] ) then an attacker can replace argv[0] with a string that for example changes the colour of the terminal, or even execute another binary.

So, never assume anything - unless it is explicitly guaranteed, and even then be suspicious.

Cool, that is good to know. I'll stop doing it in the future then!

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.