Ive just modified one of my program by adding functions into it. I'll show the code here first:
Code:
#include <iostream>
#include <string>
using namespace std;
//declaring functions
int WhatKindOfStudent();
void CalculateEnglishGrade();
void CalculateMathGrade();
void CalculateScienceGrade();
//Functions Overload
void DisplayGrade(short midterm, short finalExamGrade, short research, short presentation, float finalNumericGrade, char finalLetterGrade);
void DisplayGrade (short midterm, short finalExamGrade, float finalNumericGrade, char finalLetterGrade);
void DisplayGrade (short midterm, short finalExamGrade, short research, float finalNumericGrade, char finalLetterGrade);
//end of functions overload
//end of declaring functions
//Declaring Global Variables + Const
//declaring english students fixed percentage of marks
const float ENGLISH_MIDTERM_PERCENTAGE = 0.25;
const float ENGLISH_FINALEXAM_PERCENTAGE = 0.25;
const float ENGLISH_RESEARCH_PERCENTAGE = 0.30;
const float ENGLISH_PRESENTATION_PERCENTAGE = 0.20; //end of english students fixed percentage
//declaring maths students fixed percentage of marks
const float MATH_MIDTERM_PERCENTAGE = 0.50;
const float MATH_FINALEXAM_PERCENTAGE = 0.50; //end of math students fixed percentage
//declaring science students fixed percentage of marks
const float SCIENCE_MIDTERM_PERCENTAGE = 0.40;
const float SCIENCE_FINALEXAM_PERCENTAGE = 0.40;
const float SCIENCE_RESEARCH_PERCENTAGE = 0.20; //end of science students fixed percentage
//declaring variables
short midterm = 0;
short finalExamGrade = 0;
short research = 0;
short presentation = 0;
float finalNumericGrade = 0;
char finalLetterGrade;
char response[256];
string moreGradesToCalculate;
//end of variables declaration
//End of declaration of global variables + const
int main()
{
//declaring a local variable
int iresponse;
//end of declaration of local variable
//prompt user "whether they want to calculate grades"
cout<< "Do you want to calculate a grade? Yes/No ";
cin>> moreGradesToCalculate;
cin.ignore();
//start of for loop for changing userinput to uppercase
for (unsigned short a = 0; a < moreGradesToCalculate.length(); a++)
{
moreGradesToCalculate[a] = toupper (moreGradesToCalculate[a]);
}
//end of for loop
//end of promting user
//start of while loop
while (moreGradesToCalculate == "YES")
{
iresponse = WhatKindOfStudent();
//After user had input a valid student type, the following is to calculate the student's grade
//start of switch statement
switch (iresponse)
{
//case 1 is english student
case 1:
CalculateEnglishGrade();
DisplayGrade(midterm, finalExamGrade, research, presentation, finalNumericGrade, finalLetterGrade);
break;
//case 2 is a math student
case 2:
CalculateMathGrade();
DisplayGrade(midterm, finalExamGrade, finalNumericGrade, finalLetterGrade);
break;
//case 3 is a Science student
case 3:
CalculateScienceGrade();
DisplayGrade(midterm, finalExamGrade, research, finalNumericGrade, finalLetterGrade);
break;
//start of default for the switch statement
default:
cout<< response<< " - is not a valid student type";
return 1;
}
//end of switch statement
cout<< endl<< endl<< "Do you have another grade to calculate? Yes/No ";
cin>> moreGradesToCalculate;
cin.ignore();
//start of for loop
for (unsigned short a = 0; a < moreGradesToCalculate.length(); a++)
{
moreGradesToCalculate[a] = toupper (moreGradesToCalculate[a]);
}
//end of for loop
}
//end of while loop
cout<< "Thanks for using the Grades Calculation program! ";
return 0;
}
//end of main
//start of int WhatKindOfStudent's function
int WhatKindOfStudent()
{
//promting user for the type of students to calculate
cout<< "*** 1) English, 2) Math, 3) Science *** ";
cin.getline (response, 256);
if (strlen(response) == 0)
{
cout<< "You must choose an option";
exit(1); //error #1
}
else if ((atoi(response) < 1) || (atoi(response) > 3))
{
cout<< " - is not a valid option.";
exit(2);//error #2
}
//return an int of atoi(known as ASCII to Interger)
return atoi(response);
}
//end of WhatKindOfStudent's function
//start of void CalculateEnglishGrade()
void CalculateEnglishGrade()
{
//Promt user for "midterm" grade
cout<< "Please enter the Midterm Grade: ";
cin.getline (response,256);
midterm = atoi(response);
//end of "midterm" grade
//Promt user for "finalExamGrade"
cout<< "Please enter the Final Examination Grade: ";
cin.getline (response,256);
finalExamGrade = atoi(response);
//end of "finalExamGrade"
//promt user for "research" grade
cout<< "Please enter the research Grade: ";
cin.getline (response,256);
research = atoi(response);
//end of "research"
//Prompt user for "presentation" grade
cout<< "Please enter the Presentation Grade: ";
cin.getline (response,256);
presentation = atoi(response);
//end of "presentation"
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))
{
finalNumericGrade = 'D';
}
else if (finalNumericGrade < 70)
{
finalNumericGrade = 'F';
}
}
//end of void CalculateEnglishGrade()
//start of void CalculateMathGrade()
void CalculateMathGrade()
{
//Prompt user for "midterm" grade
cout<< "Enter the Midterm Grade: ";
cin.getline(response,256);
midterm = atoi(response);
//end of "midterm" grade
//Promt user for "finalExamGrade"
cout<< "Enter the Final Examination Grade: ";
cin.getline(response,256);
finalExamGrade = atoi(response);
//end of "finalExamGrade"
finalNumericGrade = (midterm * MATH_MIDTERM_PERCENTAGE) +
(finalExamGrade * MATH_FINALEXAM_PERCENTAGE);
if (finalNumericGrade >= 90)
{
finalLetterGrade = 'A';
}
else if ((finalNumericGrade >= 83) && (finalNumericGrade < 90))
{
finalLetterGrade = 'B';
}
else if ((finalNumericGrade >= 76) && (finalNumericGrade < 83))
{
finalLetterGrade = 'C';
}
else if ((finalNumericGrade >= 65) && (finalNumericGrade < 76))
{
finalNumericGrade = 'D';
}
else if (finalNumericGrade < 65)
{
finalNumericGrade = 'F';
}
}
//end of void CalculateMathGrade's function
//start of void CalculateScienceGrade()
void CalculateScienceGrade()
{
//Prompt user for "midterm" grade
cout<< "Enter the Midterm Grade: ";
cin.getline(response,256);
midterm = atoi(response);
//end of "midterm" grade
//Promt user for "finalExamGrade"
cout<< "Enter the Final Examination Grade: ";
cin.getline(response,256);
finalExamGrade = atoi(response);
//end of "finalExamGrade"
//Prompt user for "research" grade
cout<< "Enter the Research Grade: ";
cin.getline(response, 256);
research = atoi(response);
//end of "research"
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))
{
finalNumericGrade = 'D';
}
else if (finalNumericGrade < 60)
{
finalNumericGrade = 'F';
}
}
//end of void CalculateScienceGrade()
//start of functions overloading DisplayGrade();
//function display for english student
void DisplayGrade (short midterm, short finalExamGrade, short research, short presentation, float finalNumericGrade, char finalLetterGrade)
{
//display student scores' informations
cout<< "*** ENGLISH STUDENT ***" <<endl <<endl;
cout<< "Midterm Grade is "<< midterm<< endl;
cout<< "Final Exame is: "<< finalExamGrade<< endl;
cout<< "Research is: "<< research<< endl;
cout<< "Presentation is: "<< presentation<< endl<< endl;
cout<< "Final Numeric Grade is: "<< finalNumericGrade<< endl;
cout<< "Final Letter Grade is: "<< finalLetterGrade;
//end of english student
}
//end of function overload display for english student
//function display for math student
void DisplayGrade (short midterm, short finalExamGrade, float finalNumericGrade, char finalLetterGrade)
{
//display student scores' informations
cout<< "*** MATH STUDENT ***" <<endl <<endl;
cout<< "Midterm Grade is "<< midterm<< endl;
cout<< "Final Exame is: "<< finalExamGrade<< endl;
cout<< "Research is: "<< research<< endl;
cout<< "Presentation is: "<< presentation<< endl<< endl;
cout<< "Final Numeric Grade is: "<< finalNumericGrade<< endl;
cout<< "Final Letter Grade is: "<< finalLetterGrade;
//end of math student
}
//end of display for math student
//function display for science student
void DisplayGrade (short midterm, short finalExamGrade, short research, float finalNumericGrade, char finalLetterGrade)
{
//display student scores' informations
cout<< "*** SCIENCE STUDENT ***" <<endl <<endl;
cout<< "Midterm Grade is "<< midterm<< endl;
cout<< "Final Exam is: "<< finalExamGrade<< endl;
cout<< "Research is: "<< research<< endl;
cout<< "Presentation is: "<< presentation<< endl<< endl;
cout<< "Final Numeric Grade is: "<< finalNumericGrade<< endl;
cout<< "Final Letter Grade is: "<< finalLetterGrade;
//end of science student
}
//end of Display for science student
//end of display for science student's function }
First of all, I must say that Im sorry for the long piece of code. I jsut want to show the source code to you guys, so that you guys can correct about what Im doubting about, declaring extra variable.
In these codes here:
Code:
//start of while loop
while (moreGradesToCalculate == "YES")
{
iresponse = WhatKindOfStudent();
//After user had input a valid student type, the following is to calculate the student's grade
//start of switch statement
switch (iresponse)
I have the iresponse variable. Should I add another iresponse variable in another functions, which is used to be called, from the main() here:
Code:
int WhatKindOfStudent()
{
int iresponse; //IN THIS VARIABLE A MUST TO BE DECLARED?
//promting user for the type of students to calculate
cout<< "*** 1) English, 2) Math, 3) Science *** ";
cin.getline (response, 256);
if (strlen(response) == 0)
{
cout<< "You must choose an option";
exit(1); //error #1
}
else if ((atoi(response) < 1) || (atoi(response) > 3))
{
cout<< " - is not a valid option.";
exit(2);//error #2
}
//return an int of atoi(known as ASCII to Interger)
return atoi(response);
}
I actually tried to add in 'extra variable' iresponse in the function WhatKindOfStudent(). It works out just fine. It actually works the same even if I did not add the variable iresponse inside the WhatKindOfStudent's function. Can someone tell me is it a must to add? Is there any advantage to it? If there is advantage, what are thsoe advantage?
------------------------------------------------------------------------------------------------------------------
Another question:
why can't I declare variable like this way:
If I declare a variable like the above mentioned, I will get error result like this:
Code:
invalid suffix "1response" on interger constant
in my codeblock's IDE + compiler.
Can someone help me out? Thanks in advance! I love you guys ^^