I am currently working on an assignment for my cs1 class and I would just like some help on figuring out how to set up a loop system. I need to create a console app to calculate student grades. I figured out how to create a file and input info and then close the file, but I don't want to have to keep typing the same information over and over when I can set up a loop system to make it easier. please help guide me along to at least come close to setting it up. I would much appreciate it.
So far this is what I have:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <fstream>
usingnamespace std;
void main()
{
ofstream outfile;
outfile.open("C:/Users/Ben/My Documents/students.txt");
string lastName, firstName, lastName2, firstName2;
cout << "Please type in the first student's last name: ";
cin >> lastName;
outfile << lastName << ", ";
cout << "Please type in the first student's first name: ";
cin >> firstName;
outfile << firstName << ": ";
float score11, score12, score13, score21, score22, score23;
cout << "Please type in the the first student's first score: ";
cin >> score11;
cout << "Please type in the the first student's second score: ";
cin >> score12;
cout << "Please type in the the first student's third score: ";
cin >> score13;
float avg, result;
avg = score11 + score12 + score13;
result = avg / 3;
cout << "The first student's name and average score is: " << firstName << " " << lastName << ": " << result << endl;
outfile << result << endl;
cout << "Please type in the second student's last name: ";
cin >> lastName2;
outfile << lastName2 << ", ";
cout << "Please type in the second student's first name: ";
cin >> firstName2;
outfile << firstName2 << ": ";
cout << "Please type in the the second student's first score: ";
cin >> score21;
cout << "Please type in the the second student's second score: ";
cin >> score22;
cout << "Please type in the the second student's third score: ";
cin >> score23;
float avg2, result2;
avg2 = score21 + score22 + score23;
result2 = avg2 / 3;
outfile << result2 << endl;
cout << "The second student's name and average score is: " << firstName2 << " " << lastName2 << ": " << result2 << endl;
outfile.close();
char ch;
cin >> ch;
}
Looks like you've got a slightly different assignment than was in this other thread, but there's a for loop for entering grades there. Does that help at all?
while the user has more student names and grades to enter (start loop)
- collect name data
- collect grade info (accumulate into one bucket unless you specifically need to keep separate grades for a student?)
- calculate average and output it to screen
- write data to file
- ask if more grades are to be entered (if no, exit loop, close file)