problem typecasting output inside of an end-of-file while loop

I am having difficulty achieving the correct output that I want. I can't figure out how to correctly cast my INT variables to FLOAT outputs. First I infile (using FIN as the fstream variable) the names and program/test scores from the text file, each line looks something like this:

Snow White 77 87 76 82 92 96 90
(the first five numbers are 'program' scores, the second two are the 'test' scores)

The program finds a program, test,and overall course average(& letter grade) and outputs the results to both the screen and the output file.

The results are supposed to look like:
Snow White 82.80 93.00 85.71 B

But come out:
Snow White 82 93 85 B

Obviously, these values are being output as INT variables. I'm not going to lie, the major problem is that my teacher won't let me initially declare the scores as FLOAT. I've tried a bunch of different methods of casting, but I wanted to stop altering the code before it got mangled. CAN SOMEONE PLEASE TELL A TOTAL NOOB HOW TO CAST THIS CORRECTLY INSIDE OF THE WHILE LOOP?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>
#include  <fstream>
using namespace std; 

int main ()
{
//Begin File Input-Output Process
ifstream fin;
ofstream fout;
fin.open("input3.txt");
fout.open("VESLEY_PROG3_OUTPUT.txt");
if(!fin){cout<<"input failure\n";system("pause");return 1;}
if(!fout){cout<<"output failure\n";system("pause");return 1;}

//Declarations
string fName,lName;
int score1,score2,score3,score4,score5,test1,test2;
int pAvg,tAvg,cAvg;
string lGrade;

//End-Of-File Controlled While Loop
while (!fin.eof())
{
fin>>fName>>lName>>score1>>score2>>score3>>score4>>score5>>test1>>test2;       
pAvg=(score1+score2+score3+score4+score5)/5;
tAvg=(test1+test2)/2;
cAvg=(score1+score2+score3+score4+score5+test1+test2)/7;
//NESTED IF for letter grade
if (cAvg>=90){lGrade="A";}
else if (cAvg>=80){lGrade="B";}
else if (cAvg>=70){lGrade="C";}
else {lGrade="F";}
//end of nested if
cout<<left<<setw(10)<<fName<<setw(10)<<lName<<setw(10)<<pAvg<<setw(10)<<tAvg<<setw(10)<<cAvg<<setw(10)<<lGrade<<endl;  
fout<<left<<setw(10)<<fName<<setw(10)<<lName<<setw(10)<<pAvg<<setw(10)<<tAvg<<setw(10)<<cAvg<<setw(10)<<lGrade<<endl;       
}//end of EOF while loop
fin.close();
fout.close();//End File Input-Output Process
system ("pause");
return 0;
}//end of main function  
You need to cast the int values to float or double, do this before you do any calcs on them. This is the C way of doing it:

1
2
double dScore1;
dScore1 = (double) score1;


Have a read of this:
http://www.cplusplus.com/doc/tutorial/typecasting/


You can use the static_cast.

1
2
double dScore1;
dScore1 = static_cast<idouble>(score1) ;

Topic archived. No new replies allowed.