In: Computer Science
In this assignment, you shall create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student.
Below is an example of what the calculation would be for the program.
score1 * .3 + score2 * .3 + score3 * .4
use input.open ("studentstxt")
//Read data
input score1
input score2
input score3
The program will then determine the letter grade for that student and write the user ID and the letter grade to a file "studentGrade.txt"
The standard grading scale will be used:
A --> 90 – 100
B --> 80 - 89.99
C --> 70 - 79.99
D --> 60 - 69.99
F --> below 60
Also, do not forget that you will need to include the "fstream" library along with the other ones that you normally include.
Inside the main function definition, declare your variables being sure to give them a descriptive name where it is easy for the reader of the code to know what is stored in each. Along with variables for the values you will be reading out of the file you will also need one set up to use with the input file and the output file. These also need to be descriptive so it is obvious as to which file is being used.
In this program the user will not be entering in any data as it will all come to from the file. Likewise, all output will be written to a file so the user will not see anything come to the screen. To keep from the user being confused as if anything happened or not it is always good to give them a ending statement i.e. Something like the following:
Program has completed and the output can be found in 'studentGrade.txt'
Save the contents of the file
Use your IDE to compile the contents of your source code file
If any syntax errors are detected by the g++ compiler, or any warning messages appear, correct the errors, save the contents of your source code file, and recompile the file. Continue this step until all syntax errors in the program are corrected and no warning messages appear
Link the program. (The g++ compiler automatically performs this link step for you after you compile the program if your program contains no syntax errors)
Test your program by running it several times with different student information to check that you have implemented your algorithm correctly. If any logical errors occur (i.e., program does not fulfill the requirements or produces incorrect output), make corrections in your source code file, save the file, compile and link the file, and run the program again. Continue to do this step until the logical errors are corrected.
Design and Implementation Constraints
Follow the coding standards listed in assignment 1
Use no C++ code features in your program that have not been covered yet in the course
StudentInfo.txt
2437 61 92 79
9065 82 63 95
9816 71 92 59
4607 83 91 95
9747 69 99 61
2481 92 86 59
3893 62 70 71
5735 65 91 68
3735 98 77 68
8085 90 63 97
3247 72 96 89
6548 56 52 56
5859 100 99 68
4740 89 80 97
4424 72 72 69
5494 67 87 70
6114 71 87 96
5700 100 64 79
9890 93 92 92
6786 73 79 97
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
bool isFloat(string str)
{
istringstream iss(str);
float f;
iss >> noskipws >> f; // noskipws considers leading whitespace invalid
// Check the entire string is float
return iss.eof() && !iss.fail();
}
int main()
{
vector<string> id;
vector<string> name;
vector<vector<int>> score;
float gpa;
ifstream inFile("studentInfo.txt");
while (inFile)
{
string s;
//Read until no more lines in text file to read
while (getline(inFile, s))
{
istringstream ss(s);
string token;
int counter = 0;
vector<int> s;
//Separate string based on spaces
while (getline(ss, token, ' '))
{
// id
if (counter == 0)
{
id.push_back(token);
counter++;
}
// name
else if (counter == 1)
{
name.push_back(token);
counter++;
}
// GPA
else if (counter >= 2)
{
if (isFloat(token))
{
s.push_back(stof(token));
counter++;
}
}
}
score.push_back(s);
}
}
// print data
for (int i = 0; i < id.size(); i++)
{
cout << "Id : " << id.at(i) << ", name : " << name.at(i)
<< ", Score is : ";
float sum = 0, avg;
for (int j = 0; j < score.at(i).size(); j++)
{
cout << (score.at(i)).at(j) << " ";
sum += (score.at(i)).at(j);
}
avg = sum / 3;
cout << ", Average is " << avg << endl;
}
if (!inFile.eof())
{
cerr << "Fooey!\n";
}
return 0;
}
studentInfo.txt:
11 rCobrie 92 85 79
12 dMalbri 96 88 72
13 dRmaddo 99 88 72
14 sTest1 96 88 79
15 iJully 94 88 76
Output: