I have a text file in this format:
Petroleum Engineering 94600 175500 Marine Engineering 73900 123200 Economics and Mathematics 60000 122900 Geophysics 54100 122200 Cognitive Science 54000 121900
What I have is course name, average early career pay, and mid career pay, all separated by tabs. The course names with multiple words are separated by spaces.
I want to read the course name and put it in one variable, the first pay in a second variable, and the third pay in a third variable.
int main(){
ifstream in;
in.open("Salaries.txt");
string course;
int mid;
int avg;
if (in.fail()){
cout<< "failed to open file";
exit(1);
}
while(!in.eof()){
in >> course >> avg >> mid;
cout << course<<endl;
}
in.close();
}
When I compile this code, it outputs nothing, and the program does not exit or terminate.
Someone in comments pointed out that using eof()
is not recommended, so I tried thus instead:
while(in >> course >> sm >> lg){
cout << course << endl;
}
The process exits without outputting anything to the screen. I tried it on a input file that looks something like this:
NCORES 1 NEW 0 CORE 100 INPUT 5000 CORE 20
And it takes the string and puts it into one variable, and takes the number and puts it into another variable, and prints the correct output. So the problem is the white space between the words in the cours name in the original file, and I don't know how to account for that.