Performing equations using an Input.txt file

Hi,

I am looking for a hint in the right direction regarding my program. I don't just want the answer because I want to be able to understand it. The part in bold is the part I am need of assistance with. I'm using my input file that has a FirstName LastName and program/test scores. However, when trying to run my output my calculations aren't being taken accounted for?

Example from input file:
James Bond 77 87 76 82 92 96 90

#include <iostream>
#include <iomanip>
#include <fstream> //Step One
using namespace std;

int main (){
//START OF MAIN
system("color f0");

//Description Header
//Add Header

cout<<"Blah Blah Welcome \n"<<endl;
cout<<"Examine the input text file (input.txt) before you continue\n"<<endl;

ifstream fin; //Step Two
ofstream fout; //Step Two

//Start of Step Three
fin.open("input.txt");
fout.open("output.txt");
//End of Step Three

//Start of step 3B
if(!fin){
cout<<"Input Failure \n";
system("pause");
return 1;
}

if(!fout){
cout<<"Output Failure \n";
system("pause");
return 1;
}
//End of Step 3B
string FirstName;
string LastName;

//Program Grade Variables
int ProgramOne;
int ProgramTwo;
int ProgramThree;
int ProgramFour;
int ProgramFive;

int ProgAvg;
ProgAvg=(ProgramOne+ProgramTwo+ProgramThree+ProgramFour+ProgramFive)/5;

//Test Grade Variables
int TestAvg;
int TestOne;
int TestTwo;
TestAvg=(TestOne+TestTwo)/2;

int CourseAvg;
CourseAvg=(ProgramOne+ProgramTwo+ProgramThree+ProgramFour+ProgramFive+TestOne+TestTwo)/7;

char LetterGrade;

//do{ //While

cout<<"123456789012345678901234567890123456789012345678901234567890\n";
cout<<setw(60)<<setfill('=')<<"="<<endl;
cout<<left<<setfill(' ');

cout<<setw(15)<<"Student Name"
<<setw(10)<<"Program"
<<setw(10)<<"Test"
<<setw(10)<<"Course"
<<setw(10)<<"Letter"<<endl;
cout<<setw(15)<<" "
<<setw(10)<<"Average"
<<setw(10)<<"Average"
<<setw(10)<<"Average"
<<setw(10)<<"Grade"<<endl;



fin>>FirstName>>LastName>>ProgAvg>>CourseAvg;

cout<<left<<setw(5)<<FirstName<<LastName<<ProgAvg<<CourseAvg<<endl;
fout<<left<<setw(5)<<FirstName<<LastName<<ProgAvg<<CourseAvg<<endl;



cout<<fixed<<showpoint<<setprecision(2);

cout<<setw(60)<<setfill('=')<<"="<<endl;
//End of Step Four
if(fin.peek()=='\n') fin.ignore();

//}while(fin); //End of while

fin.close();
fout.close();

system("pause");

return 0;
}
//END OF MAIN
Last edited on
Hi,

I'm starting to think I'm going wrong way about doing equations with input files?
First of all, I think that you need to use #include<string> for this program.
And you are going to have to read in the data from the file before you can do any calculations on it. Basically, it looks like you did your "fin" too late. Read in all the data first and then you can do endless calculations on it. So start with something like this (I'm assuming that you haven't learned how to use arrays yet so I'll do it with several variables)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>

string fName, lName;   // store first name and last name in these
int scoreOne, scoreTwo, scoreThree, scoreFour, scoreFive, scoreSix, scoreSeven;  // store the 7 scores in these

ifstream fin;

fin.open("input.txt");

fin >> fName >> lName >> scoreOne >> scoreTwo >> scoreThree >> scoreFour >> scoreFive >> scoreSix >> scoreSeven;


Then you will have all the data and you can manipulate it using the variables in any way that you like. Hope this helps.
That worked great! Except for some reason the calculation is off? Sorry for all the questions haha I tried to do my fair share of research. And thanks for the tip on setting up my int variables more efficiently

James Bond 77 87 76 82 92 96 90 should = 85.71

fin>>FirstName>>LastName>>ProgramOne>>ProgramTwo>>ProgramThree>>ProgramFour>>ProgramFive>>TestOne>>TestTwo;

ProgAvg=(ProgramOne+ProgramTwo+ProgramThree+ProgramFour+ProgramFive)/5;

cout<<left<<setw(5)<<FirstName<<LastName<<ProgAvg<<endl;
fout<<left<<setw(5)<<FirstName<<LastName<<ProgAvg<<endl;
It looks to me like the program should be returning 82 since you are using an int data type for the average. The reason you are not getting the 85 you are looking for is because you told the program to just use the first 5 scores and divide them by 5. When you did the math yourself, you used all 7 scores.
That's embarrassing I got it now haha! And I changed my numbers to float to accommodate for the decimal places.

Thanks a bunch for your help!
Topic archived. No new replies allowed.