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;
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.
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.
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.
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.
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.