hi people i am very happy to talk here once again, i have a problem i want you to help me with. i have been trying to seperate the main function from my own functions, before that the program seemed to work fine but after i grouped them into my own functions it stop working,
here is the program.
/*Grades calculation project. now we get a working c++ program*/
//Grades.cpp
#include <iostream>
#include <string>
using namespace std;
int WhatKindOfStudent();
void CalculateEnglishGrade();
void CalculateMathGrade();
void CalculateScienceGrade();
void DisplayGrade(int midterm, int finalExamGrade, int research,
int presentation,float finalNumericGrade, char finalLetterGrade);
void DisplayGrade( int midterm, int finalExamGrade,
float finalNumericGrade, char finalLetterGrade);
void DisplayGrade( int midterm, int finalExamGrade, int research, float finalNumericGrade, char finalLetterGrade);
cout << "Do you want to calculate a grades? ";
getline(cin, moreGradesToCalculate);
for (int i = 0; i<moreGradesToCalculate.length();i++){
moreGradesToCalculate[i] = toupper(moreGradesToCalculate[i]);
}
while (moreGradesToCalculate == "Y"){
1response = WhatKindOfStudent();
switch (1response)
{
case 1:
CalculateEnglishGrade();
DisplayGrade(midterm,finalExamGrade,research,presentation,
finalNumericGrade,finalLetterGrade);
break;
case 2:
CalculateMathGrade();
DisplayGrade(midterm,finalExamGrade, finalNumericGrade,finalLetterGrade);
break;
case 3:
CalculateScienceGrade();
DisplayGrade(midterm, finalExamGrade, research,finalNumericGrade,finalLetterGrade);
break;
} //end of the switch statement
cout<<endl<<endl<< "Do you have another grade to calculate? ";
getline (cin, moreGradesToCalculate);
for (int i = 0; i <moreGradesToCalculate.length();i++){
moreGradesToCalculate[i] = toupper(moreGradesToCalculate[i]);
} //end of for
} //end of while statement
cout << "Thanks for using the Grades Calculation Program!"<<endl<<endl;
system ("pause");
return 0;
}
int WhatKindOfStudent();
{
1response;
//What type of student are you calculating?
cout << "Enter student type "<< "(1=English, 2=Math, 3=Science): ";
cin.getline(response,256);
if (strlen(response) == 0){
cout << "You must select a student type..."<<endl<<endl;
exit(1);
}
if ((atoi(response)< 1) | (atoi(response)>3)){
cout << response<< " -is not a valid student type"<<endl<<endl;
exit(2);
}
return atoi(response);
}
void CalculateEnglisGrade()
{
cout << "Enter the midterm grade : ";
cin.getline(response,256);
midterm = atoi(response);
cout << "Enter the finalExamination grade : ";
cin.getline(response,256);
finalExamGrade = atoi(response);
cout << "Enter the research grade : ";
cin.getline(response,256);
research = atoi(response);
cout << "Enter the presentation grade : ";
cin.getline(response,256);
presentation = atoi(response);
void DisplayGrade(int midterm, int finalExamGrade, int research, int presentation,float finalNumericGrade, char finalLetterGrade);
{
cout <<endl<<endl<< "********ENGLISH STUDENT*******"<<endl<<endl;
cout << "Midterm grades is : "<<midterm<<endl;
cout << "finalExam grade is : "<<finalExamGrade<<endl;
cout << "research grade is : " <<research<<endl;
cout << "presentation grade is : "<<presentation<<endl<<endl;
cout << "finalNumericGrade is : "<<finalNumericGrade<<endl;
cout << "finalLetter grade is : "<<finalLetterGrade<<endl<<endl;
} //end of DisplayGrade for English
void DisplayGrade( int midterm, int finalExamGrade, float finalNumericGrade, char finalLetterGrade);
{
cout <<endl<<endl<< "******MATH STUDENT******"<<endl<<endl;
cout << "midterm grade is : "<<midterm<<endl;
cout << "final Exam grade is : "<<finalExamGrade<<endl;
cout << "final Numeric grade is : "<<finalNumericGrade<<endl;
cout << "final Letter grade is : " << finalLetterGrade<<endl<<endl;
} //end of DisplayGrade for math
void DisplayGrade( int midterm, int finalExamGrade, int research, float finalNumericGrade, char finalLetterGrade);
{
cout <<endl<<endl<< "*****SCIENCE STUDENT*****"<<endl<<endl;
cout << "midterm grade is : "<<midterm<<endl;
cout << "final Exam grade is : " <<finalExamGrade<<endl;
cout << "research grade is : " <<research<<endl;
cout << "final Numeric grade is : "<<finalNumericGrade<<endl;
cout << "final Letter grade is : "<<finalLetterGrade<<endl<<endl;
} //end of DisplayGrade for Science
-You don't specify what is going wrong exactly, narrow down your problem and come back to us with a more in-depth problem
-Put your code in CODE tags(<> button), it helps organize the code and makes it easier for us to read. Make sure the code has proper indentation format
-post less code, people aren't going to spend an hour shifting through hundreds of lines of code just to catch a simple error in your program(which is most likely the cause). Narrow down the amount of code you need to post, only show us the part of the program that is causing trouble.
As for catching the error:
Put cout statements everywhere and see where they end, the last statement that was printed will be near where your problem occurs.
I agree with Need4Sleep, your formatting of code makes it really hard to read. Does the above code compile? I don't currently have access to a compiler so I assume it does compile and I assume the problem lies with the program not behaving as you expect instead of any runtime errors/crashes. If thats the case then the problem lies in your main function and the way you have decided to tackle some of the problems (such as input).
The first thing you do is ask if the user (who has just run the grades calculation program) if they want to calculate any grades. Assume they do since they just ran the program. Instead of a while loop use a Do, While loop and check at the end if they want to calculate another grade. I reccomend the following.
char input;
int main()
{
int studentType;
do {
studentType = WhatKindOfStudent();
if (studentType)
{
switch (studentType)
{
case 1:
CalculateEnglishGrade();
DisplayGrade(midterm,finalExamGrade,research,presentation,
finalNumericGrade,finalLetterGrade);
break;
case 2:
CalculateMathGrade();
DisplayGrade(midterm,finalExamGrade, finalNumericGrade,finalLetterGrade);
break;
case 3:
CalculateScienceGrade();
DisplayGrade(midterm, finalExamGrade, research,finalNumericGrade,finalLetterGrade);
break;
} //end of the switch statement
}
cout<<endl<<endl<< "Do you have another grade to calculate? (y/n)";
cin >> input;
} while (input == 'y' || input == 'Y') //end of while statement
cout << "Thanks for using the Grades Calculation Program!"<<endl<<endl;
system ("pause");
return 0;
}
Change your WhatKindOfStudent() function to something more like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int WhatKindOfStudent();
{
//What type of student are you calculating?
cout << "Enter student type "<< "(1=English, 2=Math, 3=Science): ";
cin >> input;
switch (input)
{
case 1:
case 2:
case 3:
return (int)input;
default:
cout << "You must select a student type, please start again..." << endl << endl;
return 0;
}
}
It will return the number they entered or if they entered something invalid return 0 which will then make the if statement in the main function evaluate to false thereby skipping the bulk of your program to the end of the while loop.
The rest of the program seems okay but as I said. I dont have access to a compiler currently.