|
#1
|
|||
|
|||
|
modify the program to use sub-functions
I already did this work but my teacher told me to post it on this site and see if anybody else got the same thing. Follow the steps
// Inclass functions // Modify the program to use sub-functions // 1. code step 3 in a function to prompt and get a grade; you may use // either value returning function or pass by reference // 2. code steps 4-9 in a sub-function to display a proper greeting // based on the grade // Use proper function names, return types, and parameters. // Modify the spec to show the algorithm in main. /*************************************************************************************** * Naritive: The program will display a greeting message based on the grade entered. It * * should keep on running as long as user chooses to. * * INPUT: grade - single letter * * repeat or not - y or n * * OUTPUT: a message depend on grade entered * * CONSTRAINTS: grade must be a, b, c, d, e * * CONSTANTS: display messages, see below * * OPERATION: * * 1. set repeat to y * * 2. as long as repeat is y, do the following step 3-10 * * 3. prompt and read grade * * 4. if grade is a or A, display Excellent job! * * 5. else if grade is b or B, display Very good! * * 6. else if grade is c or C, display Keep up the good work! * * 7 else if grade is d or D, display Try harder next time. * * 8. else if grade is e or E, display Don't give up! * * 9. else display Invalid input. * * 10. prompt and read wether to repeat or not * ****************************************************************************************/ #include <iostream> #include <conio.h> using namespace std; int main() { char grade; // letter grade char again = 'y'; // again starts at y so the while loop will run // as long as contents of again is y or Y // everything within the while block will repeat while(toupper(again) == 'Y') { cout << "\nEnter your grade: "; cin >> grade; // switch statement must have a block // switch can only be used for intergral types // switch follows the same as nested if-else-if switch (grade) { // case 'a': and case 'A': means: // grade equals to 'a' or 'A'? case 'a': case 'A': cout << "\nExcellent job!\n"; // break is not required by compiler // it's there to prevent fall-through // if grade is 'a', it'll break out of the swtch block break; case 'b': case 'B': cout << "\nVery good!\n"; break; case 'c': case 'C': cout << "\nKeep up the good work!\n"; break; case 'd': case 'D': cout << "\nTry harder next time.\n"; break; case 'e': case 'E': cout << "\nDon't give up!\n"; break; // default case is like the final else clause // default runs when all the above cases are not true default: cout << "\nInvalid input.\n"; } // prompt for whether to run again or not // it must be done within the loop so that // contents in again is updated cout << "\nRun again (y/n)? "; cin >> again; } _getch(); return 0; } |
|
#2
|
|||
|
|||
|
sorry, but what was the question? Did you need help with this?
|
|
#3
|
|||
|
|||
|
sub functions
I need to modify this program to use sub-functions, code step 3 in a function to get a grade, code steps 4-9 in a sub-function to display a proper greeting based on grade, and I have to use proper function names, return types, and parameters.
|
|
#4
|
|||
|
|||
|
Well, you have written all the code that gets the work done. All that you need to do now is refactor it into separate functions.
step 3: Code:
char get_grade()
{
std::cout << "Enter your grade: " ;
char grade ;
std::cin >> grade ;
return grade ;
}
Code:
void display_message( char grade )
{
switch( grade )
{
case 'A' : case 'a' :
std::cout << "Excellent job!\n" ; break ;
case 'B' : case 'b' :
std::cout << "Very good!\n" ; break ;
case 'C' : case 'c' :
std::cout << "Keep up the good work!\n" ; break ;
case 'D' : case 'd' :
std::cout << "Try harder next time.\n" ; break ;
case 'E' : case 'e' :
std::cout << "Don't give up!\n" ; break ;
default :
std::cout << "Invalid input.\n" ;
}
}
Code:
bool run_again()
{
std::cout << "\nRun again (y/n)? " ;
char again ;
std::cin >> again ;
return again == 'y' || again == 'Y' ;
}
Code:
int main()
{
do
{
char grade = get_grade() ;
display_message( grade ) ;
} while( run_again() ) ;
}
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Connecting to a Server Program HELP | sedricbenson@ho | C++ | 2 | 11-07-2006 08:58 AM |
| Help I Need a Genius For A Struct Based Program in Visual C++ | sailinghunter | C++ | 5 | 08-09-2006 04:30 PM |
| processing additional data when program is running | Ervin Rodriguez | Database | 1 | 05-16-2003 09:45 AM |
| problem in program in c++ | mheasen | Architecture and Design | 0 | 03-20-2002 10:24 AM |