| | |
help for program involving switch loops and file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2004
Posts: 2
Reputation:
Solved Threads: 0
I'm a novice programmer, I'm writing a program called GPA that reads characters and numbers from a file. The numbers from the file are the amount of credits for a course, the characters on the program represent the grade for the course. I'm supposed to find the amount of credit points by multiplying the credits by the amount of points a grade is worth, for example A=4, B=3 C=2, D=1, F=0. I have to do this by using a file a loops and a switch statement.
email: xadeolax@cs.com
email: xadeolax@cs.com
Last edited by frosticex; Apr 18th, 2004 at 12:13 am. Reason: adding email address
•
•
Join Date: Apr 2004
Posts: 2
Reputation:
Solved Threads: 0
I left an attachement with the post, but here it is again. Please help.
#include<fstream.h>
void main()
{ifstream gpa;
char letterGrade;
int credits=0;
int creditsArray[]={credits};
int totCredits=0;
int creditPoints=0;
int totCreditPoints=0;
int count=0,Points;
gpa.open("C:/Multimedia Files/My Documents/c++/files/gpa1.txt");
if(gpa.fail())
cout<<"Couldn't open the file!!!\n";
else
{
gpa>>letterGrade;
gpa>>credits;
while(!gpa.eof())
{count++;
totCredits+=credits;
cout<<letterGrade;
cout<<credits<<"\n";
gpa>>letterGrade;
gpa>>credits;
switch(letterGrade)
{
case 'A': Points=4;creditPoints=Points*credits;break;
case 'B': Points=3;creditPoints=Points*credits;break;
case 'C': Points=2;creditPoints=Points*credits;break;
case 'D': Points=2;creditPoints=Points*credits;break;
case 'F': Points=0;creditPoints=Points*credits;break;
default: cout<<"Invalid letter grade in file\n";
}//switch
totCreditPoints+=creditPoints;
}//while(!gpa.eof())
gpa.close();
cout<<"Total credits "<<totCredits<<endl;
cout<<"total credit points "<<totCreditPoints<<endl;
if(totCredits=0) cout<<"The GPA can't be calculated!!!\n";
}//else
}//main
#include<fstream.h>
void main()
{ifstream gpa;
char letterGrade;
int credits=0;
int creditsArray[]={credits};
int totCredits=0;
int creditPoints=0;
int totCreditPoints=0;
int count=0,Points;
gpa.open("C:/Multimedia Files/My Documents/c++/files/gpa1.txt");
if(gpa.fail())
cout<<"Couldn't open the file!!!\n";
else
{
gpa>>letterGrade;
gpa>>credits;
while(!gpa.eof())
{count++;
totCredits+=credits;
cout<<letterGrade;
cout<<credits<<"\n";
gpa>>letterGrade;
gpa>>credits;
switch(letterGrade)
{
case 'A': Points=4;creditPoints=Points*credits;break;
case 'B': Points=3;creditPoints=Points*credits;break;
case 'C': Points=2;creditPoints=Points*credits;break;
case 'D': Points=2;creditPoints=Points*credits;break;
case 'F': Points=0;creditPoints=Points*credits;break;
default: cout<<"Invalid letter grade in file\n";
}//switch
totCreditPoints+=creditPoints;
}//while(!gpa.eof())
gpa.close();
cout<<"Total credits "<<totCredits<<endl;
cout<<"total credit points "<<totCreditPoints<<endl;
if(totCredits=0) cout<<"The GPA can't be calculated!!!\n";
}//else
}//main
Last edited by frosticex; Apr 19th, 2004 at 10:35 am. Reason: inseting code
![]() |
Similar Threads
- Program displaying and writing symbols to my file (C++)
- Trouble with multi-file program (C++)
- Code Snippet: program to create a simple txt file. (Java)
- Trying to get a program to extract data from a text file (C++)
- joining two chars together and converting to INT (C++)
- Error message when trying to open an external file in C++ program (C++)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C++ Forum
- Previous Thread: ASCII to BINARY, & VICA VERCA
- Next Thread: Circle class
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





