hi pleas help me with this my cplusplus functions

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);

const float ENGLISH_MIDTERM_PERCENTAGE = .25;
const float ENGLISH_FINALEXAM_PERCENTAGE = .25;
const float ENGLISH_RESEARCH_PERCENTAGE = .30;
const float ENGLISH_PRESENTATION_PERCENTAGE = .20;
const float MATH_MIDTERM_PERCENTAGE = .50;
const float MATH_FINALEXAM_PERCENTAGE = .50;
const float SCIENCE_MIDTERM_PERCENTAGE = .40;
const float SCIENCE_FINALEXAM_PERCENTAGE = .40;
const float SCIENCE_RESEARCH_PERCENTAGE = .20;
int midterm = 0;
int finalExamGrade = 0;
int research = 0;
int presentation = 0;
float finalNumericGrade = 0;
char finalLetterGrade;
char response[256];
string moreGradesToCalculate="";

int main()
{

int 1response;

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);

finalNumericGrade =
(midterm * ENGLISH_MIDTERM_PERCENTAGE) +
(finalExamGrade * ENGLISH_FINALEXAM_PERCENTAGE) +
(research * ENGLISH_RESEARCH_PERCENTAGE) +
(presentation * ENGLISH_PRESENTATION_PERCENTAGE);

if (finalNumericGrade >= 93)
finalLetterGrade = 'A';
else
if ((finalNumericGrade >= 85) & (finalNumericGrade < 93))
finalLetterGrade = 'B';
else
if ((finalNumericGrade >=78) & (finalNumericGrade < 85))
finalLetterGrade = 'C';
else
if ((finalNumericGrade >= 70) & (finalNumericGrade < 78))
finalLetterGrade = 'D';
else
if(finalNumericGrade < 70)
finalLetterGrade = 'F';
}


void CalculateMathGrade()
{
cout << "Enter the midterm grade : ";
cin.getline(response,256);
midterm = atoi(response);
cout << "Enter the final Examination grade : ";
cin.getline(response,256);
finalExamGrade = atoi(response);

finalNumericGrade =
(midterm * MATH_MIDTERM_PERCENTAGE) +
(finalExamGrade * MATH_FINALEXAM_PERCENTAGE);

if (finalNumericGrade >= 90)
finalLetterGrade = 'A';
else
if ((finalNumericGrade >=80) & (finalNumericGrade < 90))
finalLetterGrade = 'B';
else
if ((finalNumericGrade >=76) & (finalNumericGrade < 80))
finalLetterGrade = 'C';
else
if ((finalNumericGrade >=65) & (finalNumericGrade < 76))
finalLetterGrade = 'D';
else
if (finalNumericGrade < 65)
finalLetterGrade = 'F';
}

void CalculatScienceGrade()
{
cout << "Enter the midterm grade : ";
cin.getline(response,256);
midterm = atoi(response);
cout << "Enter the final Exam grade : ";
cin.getline(response,256);
finalExamGrade = atoi(response);
cout << "Enter the research grade : ";
cin.getline(response,256);
research = atoi(response);


finalNumericGrade =
(midterm * SCIENCE_MIDTERM_PERCENTAGE) +
(finalExamGrade * SCIENCE_FINALEXAM_PERCENTAGE) +
(research * SCIENCE_RESEARCH_PERCENTAGE);

if (finalNumericGrade >= 90)
finalLetterGrade = 'A';
else
if ((finalNumericGrade >= 80) & (finalNumericGrade < 90))
finalLetterGrade = 'B';
else
if ((finalNumericGrade >= 70) & (finalNumericGrade < 80))
finalLetterGrade = 'C';
else
if ((finalNumericGrade >= 60) & (finalNumericGrade < 70))
finalLetterGrade = 'D';
else
if (finalNumericGrade < 60)
finalLetterGrade = 'F';
}



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




please help me now
A list of things before reading the program:

-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.


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
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.
Last edited on
Topic archived. No new replies allowed.