Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > C++

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 10-22-2009, 12:51 PM
fantasyfootball fantasyfootball is offline
Registered User
 
Join Date: Oct 2009
Posts: 9
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;
}
Reply With Quote
  #2  
Old 10-23-2009, 04:42 PM
jonnin jonnin is offline
Senior Member
 
Join Date: Dec 2003
Posts: 3,008
sorry, but what was the question? Did you need help with this?
Reply With Quote
  #3  
Old 10-24-2009, 03:35 AM
fantasyfootball fantasyfootball is offline
Registered User
 
Join Date: Oct 2009
Posts: 9
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.
Reply With Quote
  #4  
Old 10-24-2009, 10:25 AM
vijayan vijayan is offline
Registered User
 
Join Date: Dec 2007
Posts: 281
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 ;
}
steps 4 to 9:
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" ;
    }
}
and for good measure, step 10:
Code:
bool run_again()
{
    std::cout << "\nRun again (y/n)? " ;

    char again ;
    std::cin >> again ;

    return again == 'y' || again == 'Y' ;
}
now in main, all you have to do is run these functions in a loop:
Code:
int main()
{
    do
    {
        char grade = get_grade() ;

        display_message( grade ) ;

    } while( run_again() ) ;
}
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

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


All times are GMT -4. The time now is 12:52 AM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.