Here is the program that I want to create:
Calculate the grade point average for a student whose information is stored in a file named prog4Data.txt. The file contains the following information:
line 1: student’s name in format First Name Middle Name (optional) Last Name – names are separated by a blank(s)
2: grades- recorded as uppercase letters without spaces separating them (4 grades). Assume that no grade is an “F”
3: credits- recorded as integers without spaces separating the digits (4 digits)
Calculate the student’s GPA without using any selection statements (if, switch, etc.)
Example I/O
data file named prog4Data.txt contains the following
Lonesome Polecat
ABBC
4323
Your output should appear as follows and should appear both on the monitor and in a file named prog4outPut.txt.
Lonesome Polecat’s GPA = 3.08
Now, here is how I did it:
#include <fstream>
#include <iostream>
int gpa(int);
int gpa_1(int);
using namespace std;
int main()
{
char FirstName[30], LastName[30];
FILE *pFile;
int c,ch,sum=0,gsum=0,gr,p,q;
float grade;
ifstream Students("D:\\Essay\\USM!\\CSC 101\\prog4Data.txt");
Students >> FirstName >>LastName;
pFile=fopen ("D:\\Essay\\USM!\\CSC 101\\prog4outPut.txt","r");
do
{
c=fgetc(pFile);
while (ch==('A' || 'B' || 'C' || 'D'))
{
ch = fgetc (pFile);
p=gpa(ch);
q=gpa_1(p);
gr=ch * c;
sum=sum+gr;
}
gsum=gsum+c;
}while(c==(1 || 2 || 3 || 4));
fclose (pFile);
grade=sum/gr;
cout<<FirstName<<LastName<<"'s"<<"GPA="<<grade;
ofstream Student("D:\\Essay\\USM!\\CSC 101\\prog4outPut.txt", ios::out);
Student << FirstName<<LastName << "'s" <<"GPA="<< grade;
return 0;
}
int gpa(int c)
{
int a[4]={'A','B','C','D'};
int i=0;
for(i=0;i<4;i++)
{
if(a==c)
return i;
else
return 0;
}
}
int gpa_1(int i)
{
int a[]={4,3,2,1};
return a;
}
But, I get these messages from Microsoft Visual C++ 2010:
"d:\essay\usm!\csc 101\trying\trying\trying.cpp(26): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen' 1>d:\essay\usm!\csc 101\trying\trying\trying.cpp(31): warning C4805: '==' : unsafe mix of type 'int' and type 'bool' in operation 1>d:\essay\usm!\csc 101\trying\trying\trying.cpp(40): warning C4805: '==' : unsafe mix of type 'int' and type 'bool' in operation 1>d:\essay\usm!\csc 101\trying\trying\trying.cpp(42): warning C4244: '=' : conversion from 'int' to 'float', possible loss of data 1>d:\essay\usm!\csc 101\trying\trying\trying.cpp(31): warning C4700: uninitialized local variable 'ch' used 1>d:\essay\usm!\csc 101\trying\trying\trying.cpp(62): warning C4715: 'gpa' : not all control paths return a value." And when I want to run it, this happens:"Run-Time Check Failure#3- The variable 'ch' is being used without being initialized. So how can I make it work?
Please don't just suggest something as I've just started programing in c++. If you can fix the code and tell me what was wrong.
Thanks.