Thread: Question with Functions

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    Question with Functions

    I'm working on a program that needs to calculate the total cost of tuition.

    Enter amount of credits -- multiply credits by 100.00 -- add 20.00 as student activity fee -- print final number.

    I can get the program to work the following way:

    Code:
    #include <iostream>
    using namespace std;
    
    
    
    int main()
    {
       int iCredits;
       double fTotal;
        cout << "Please enter a the number of credits you are taking this semester: ";
    	cin >> iCredits;
    	
    	
        
     cout << "$ " << iCredits * 100 + 20 << endl;		
            
        system("PAUSE");
        return 0;
    }
    When I try to break down the processes in functions I get some number with E in it.

    Can anyone help me with a suggestion to break down the above program using multiple functions? One to compute credits * 100.00, one to add the extra 20.00 and finally one to print out the final total.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What did you try?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    #include <iostream>
    using namespace std;
    // pass the parameter by reference, so that
    // the changes will be remembered
    void input(int& iCredits);
    int main()
    {
       int iCredits;
       double fTotal;
        input(iCredits);
         
     cout << "$ " << iCredits * 100 + 20 << endl;    
             
        system("PAUSE");
        return 0;
    }
    
    void input(int& iCredits)
    {
            cout << "Please enter a the number of credits you are taking this semester: ";
            cin >> iCredits;
    }
    Continue with the rest
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Quote Originally Posted by std10093 View Post
    Code:
    #include <iostream>
    using namespace std;
    // pass the parameter by reference, so that
    // the changes will be remembered
    void input(int& iCredits);
    int main()
    {
       int iCredits;
       double fTotal;
        input(iCredits);
         
     cout << "$ " << iCredits * 100 + 20 << endl;    
             
        system("PAUSE");
        return 0;
    }
    
    void input(int& iCredits)
    {
            cout << "Please enter a the number of credits you are taking this semester: ";
            cin >> iCredits;
    }
    Continue with the rest

    Thanks, I went with this:

    Code:
    #include <iostream>
    using namespace std;
    
    double dCalcTotal ();
    double PrintTotal (double dTotal);
    int iCredits;
    
    
    
    int main()
    {
        
        cout << "Please enter a the number of credits you are taking this semester: ";
    	cin >> iCredits;
    	
    	PrintTotal (dCalcTotal());
    	
    	
        
    
            
        system("PAUSE");
    	return 0;
    }
    
    double dCalcTotal ()
    {
     double dTotal;
     dTotal =iCredits * 100.00 + 20.00;
     return dTotal;
    }
    
    double PrintTotal (double dTotal)
    {
     cout << "$ " << dTotal << endl;
     return 0;
    }

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are welcome. You said that you wanted the multiplication to be done in one function and the addition in another one. In your implementation you do both in one function. This is ok, but if the requirements are to split these two operations into two functions, then you should do it. It is easy.

    When a function does not return something, like in your print one, you write void. So, your print function should be like this
    Code:
    void rintTotal (double dTotal)
    {
        cout << "$ " << dTotal << endl;
    }
    I do not like that you put iCredits as a global variable. Now your program is very small, but as the programs get bigger and bigger and more programmers work on the same projects, global variables, can really be problematic and prone to conflicts (variable shadowing).

    Declare it in the main and pass it as a parameter, like this
    Code:
    double dCalcTotal (int iCredtis)
    {
     double dTotal;
     dTotal =iCredits * 100.00 + 20.00;
     return dTotal;
    }
    Don't forget to change the prototype and call the function with the iCredits as a parameter in the main.

    I really like what you did in line 16.

    Also, notice, that next time you start a thread, it would be nice to post your try first
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 09:39 AM
  2. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM
  3. Question about sub functions
    By warthog89 in forum C Programming
    Replies: 5
    Last Post: 09-28-2006, 05:49 AM
  4. Question about functions
    By richdb in forum C Programming
    Replies: 2
    Last Post: 01-22-2006, 01:18 AM
  5. Functions question
    By Lurker in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2003, 11:24 AM