Terminate and Read Certain Strings

I'm trying to figure out how to terminate within a loop so that it starts the loop over. I want the program, when reading the input file, to terminate when it reads -1 in the input file. Here is my code.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main(void)
{

string name;
ifstream fin;
ofstream fout;
string File_Name, File_Out;

cout << "Enter input file name : ";
cin >> File_Name;
fin.open(File_name.c_str()) ;
cout << "Enter output file name : ";
cin >> File_Out;


fin.open("input.txt");

if (fin.fail())
cout << "Unable to open file.\n";
else
{
fin >> grade;

while (! fin.eof())
{

getline (fin,grade);
cout << grade;

}
return 0;
}
}

My input file looks something similar to this:
Mark
12 34 65 34 -1
Sammy
4 66 34 99 -1
Rodney
-1



Therefore, can someone point me in the right direction? Thanks in advance.
It looks like you're using the value -1 for double duty: to terminate a list of grades for one student, and to denote end of file. I think you want two nested loops: the outer will process a student, and the inner will process the grades for that student. I'm not sure your loops should be "for" loops, though; given your data format, "while" loops may be better.

mzimmers, what do you mean? make the names all variables and use while Mark etc etc?
No...if I understand the "protocol" to your file, it's a series of records stored in this order:

- a name
- a series (of indeterminate length) of scores
- the value -1 (to serve as a terminator)


So, your logic should look something like (in pseudo-code):

1
2
3
4
5
6
7
8
9
read a token // a name
while (!eof) {
   read a token // a score or a -1
   while (token != -1) {
      process token // do whatever you want to do with the score
      read a token
   }
   read a token // another name
}


If I understand your file structure, this kind of logic should do what you want it to.
Last edited on
Okay sweet. One last question. How do I read an input file with different types of data in it. My input file looks something similar to this:
Mark
12 34 65 34 -1
Sammy
4 66 34 99 -1
Rodney
-1
I tried getline (fin, Name) but all it doesnt is read the first set of numbers. It doesn't read the name NOR any other line. I've literally been tinkering around with my code for 3 hours and I can't seem to figure out how to read the data in the input and store them in variables. If someone can answer this question, I'd be very grateful.
Remember that getline() only reads character strings; it doesn't automatically convert them into integers.

Post your code, and we'll take a look at it.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main(void)
{

string Name;
ifstream fin;
ofstream fout;
string File_Name, File_Output;
int grade;



cout << "Enter input file name : ";
cin >> File_Name;
fin.open(File_Name.c_str()) ;
cout << "Enter output file name : ";
cin >> File_Output;

if ( fin.fail())
cout <<"\nBad input file name, program terminated.\nHave a nice day.\n\n\n";
else
{
fin >> Name;

while (! fin.eof())
{
getline (fin,Name);
cin.ignore(10,'\n');
fin >> Name;
}

fin >> Name;

}
fout.open(File_Output.c_str());
fout << "=======================================================" << endl;
fout <<left<<setw(15)<<"Name"<<setw(20)<<"Number of Tests"<<setw(20)<<"Average"<<setw(15)<<"Grade";
fout << Name;
fout.close();
return 0;
}

My goal here is to read an this input file: Introduction to Computer Programming I
Mark
98 45 78 -1
Murphy
45 56 67 89 -1
Mally
-1
Maggy
89 88 56 -1
Mickey
100 56 81 100 -1
Minney
50 56 30 98 -1
Mable
90 98 78 98 80 40 -1
Moe
76 79 66 50 -1
Morrison
-1
Michael
100 45 99 56 97 -1

And to output it onto another file. I want to record the test scores of every person and average them out. I just don't understand the mechanics behind storing each name and variable and outputting them back onto another file. I don't need a direct answer, a direction would be just as helpful. Thanks in advance.
Why are you doing a cin.ignore()?

And, what does it mean when a line contains just a -1...no scores for that person?
Im using cin.ignore() to skip a line to read the next name... I think? I don't exactly know, my professor kept emphasizing its use. Also, the -1 is to terminate the loop and go back into the input file to read the next like but I don't know how to do that yet either...
OK...programming is hard enough as it is. When you're using tools without knowing how or why, it becomes nearly impossible. I think you need to get some clarity from your professor on why he wants to do it that way, and how you're supposed to use it.

You can still use the nested while loops as I demonstrated above. But, the inner loop shouldn't do a getline(); it should probably use the << operator. That way, you'll only get one numeric value at a time, and when you hit the -1, you can exit the inner loop, and go back to reading the next name at the outer loop.
Topic archived. No new replies allowed.