Using for statements to read from a file

Hello, I'm pretty new to c++ and was wondering if anyone could help me out!
I have to read multiple students data from a file using the for loop and the output it to a file. This is currently what I have. I just don't understand how to get the for loop to work. Any help would be appreciated! Thanks in advance.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{

int students;
cout << "How many students do you have?" << endl;
cin >> students;
if (students > 5 || students < 3)
{
cout << "Please re-enter a valid number." << endl;
cin >> students;
}
ifstream inFile;
inFile.open("Project3_A04671859_Input.txt");
if (inFile.fail())
{
cout << "Error while opening the file.\n";
}
else
{
cout << "The input file has opened.\n";
}

string firstname,lastname,address;
string socialsecuritynumber,telephonenumber, course;
char lettergrade;
double test1;
double test2;
double test3;
double test4;
double finalexam;
int age;
int years;
const int A_score = 90, B_score = 80, C_score = 70, D_score = 60;
double CNG;


for...(idk)
inFile >> firstname;
inFile >> lastname;
inFile >> age;
inFile.ignore();
getline(inFile,address);
inFile >> years;
inFile.ignore();
getline(inFile,telephonenumber);
inFile.ignore();
getline(inFile,socialsecuritynumber);
for...(idk)
inFile >> course;

for...(idk)
inFile >> test1;
inFile >> test2;
inFile >> test3;
inFile >> test4;
inFile >> finalexam;


inFile.close();
ofstream outFile;
outFile.open("Project3_A04671859_Output.txt");
outFile << "\tStudent Grade Sheet" << endl;
outFile << "Name of Student:\t" << firstname << " " << lastname << endl;
outFile << "Age:\t" << age << endl;
outFile << "Address:\t" << address << endl;
outFile << "Number of years at Texas State:\t" << years << endl;
outFile << "Telephone Number:\t" <<telephonenumber << endl;
outFile << "Student Soc. Security#:\t" << socialsecuritynumber << endl;
outFile << "Course Number:\t" << course << endl;
outFile << "Test#1:\t" << test1 << endl;
outFile << "Test#2:\t" << test2 << endl;
outFile << "Test#3:\t" << test3 << endl;
outFile << "Test#4:\t" << test4 << endl;
outFile << "Final Exam:\t" << finalexam << endl;
CNG = ((test1*.10)+(test2*.15)+(test3*.15)+(test4*.20)+(finalexam*.40));
outFile << "Numerical Grade:\t" << setprecision(3) <<CNG << endl;
if (CNG >= A_score)
outFile << "Letter Grade: A";
else if (CNG >= B_score)
outFile << "Letter Grade: B";
else if (CNG >= C_score)
outFile << "Letter Grade: C";
else if (CNG >= D_score)
outFile << "Letter Grade: D";
else
outFile << "Letter Grade: F";
if (CNG >=95)
outFile << "Commend: Congratulations, Your performance is Excellent.";
if (CNG < 70)
outFile << "Warn: Your grade is too low and needs improvements.";

outFile.close();



return 0;
}
Last edited on
You need somewhere to store all that stuff you're going to read in loops. Have you thought about where you might put it?

Have you been introduced to collections in class?
No I have not. I just don't understand how to use the loops to read this:
( txt file)
Hunter Perez
18
6612 Quinton Dr., Austin, TX 78747
1
512-350-3374
555-55-5555
CS1428
80.3
90.2
85.9
46.2
82.2
Math1315
95.2
98.8
92.3
94.3
88.4
POSI2310
75.5
56.4
88.9
90.5
87.6
Anna Bushong
17
512 Pony Chase Ln., Austin, TX 78727
0
512-427-5596
111-11-1111
CS1428
95.5
89.9
87.9
98.8
93.6
ENG1310
75.0
80.0
0.0
80.0
80.0
US1100
100.0
100.0
100.0
0
100.0
Scott Lamb
21
664 Windsor Rd., Austin, TX 78703
3
512-426-93253
444-44-4444
CS1428
89.9
80.5
88.6
95.5
97.8
POSI2310
85.6
95.2
85.7
88.9
75.3
In order to use for loops, you need to know how many of a particular item there are. *
So a for loop is not the best choice for reading students since you don;t know in advance how many students there are.

This can be solved easily by using a while loop that tests infile.
1
2
 
  while (infile >> firstname) // will go false when eof is reached  


The next problem you have is that you don't know how many courses each student has. Some have three, the last student has two. There is no easy way to fix this without modifying the input file to put some type of separator between each student.

* = That's not strictly true as you can put complex conditions in the termination clause of a for loop, but the normal for loop idiom is to know how many times you want to iterate the loop.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
I don't really understand the file format because ...

Before I get into that, the format seems to be a sequence of records of format:
1
2
3
4
5
6
7
8
Name (string)
Age (int)
Address (string)
some number (int)
phone number (string)
ss (string)
(array of 3): course name (string)
  (array of 5): scores (int)

... except that last record is missing a course.

So, you need to define a record (struct) that represents a course, because you're going to have a collection of them.

Each of those records will have a collection of scores.

So, a course record and code to read one from a stream might look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Course
{
    std::string name;
    std::vector<int> scores;
};

std::istream& operator>>(std::istream& is, Course& course)
{
    std::getline(is, course.name);

    int score;
    for (int i = 0; (i < 5) && (is >> score); ++i)
        scores.push_back(score);

    return is;
}

Anyway, that's quite a lot to digest, it even has your for loop. I'll stop here.
Last edited on
Topic archived. No new replies allowed.