Default Arguments Question...

This is a discussion on Default Arguments Question... within the C++ Programming forums, part of the General Programming Boards category; This is a question I'm stuck with from the book "C++ Programming 101" : Write a program that computes net ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    27

    Cool Default Arguments Question...

    This is a question I'm stuck with from the book "C++ Programming 101" :

    Write a program that computes net pay based on the values the user types, Ask the user for the hours worked, the rate per hour, and the tax rate. Because most employees work 40 hours per week and earn $5.00 per hour, use these values as default values in the function that computes the net pay. If the user presses Enter in response to your questions, use the default values.


    Here is what I got so far: I don't know what to do with the Enter.. How do I do that if the user presses Enter the default values will be sent?

    Code:
    #include <iostream>
    using namespace std;
    
    float compute_net(float tax, int hours = 40, int rate = 5); //prototype
    
    main()
    {
    	int hours;
    	int rate;
    	float tax;
    	float ans;
    cout << "How many hours did you work? \n";
    cin >> hours;
    
    cout << "How much do you make per hour? \n";
    cin >> rate;
    
    cout << "How much tax do you pay? \n";
    cin >> tax;
    
    ans = compute_net(tax, hours, rate);
    cout << ans;
    
    return 0;
    }
    
    float compute_net(float tax, int hours, int rate)
    {
    float net;
    net= (hours*rate) - (hours*tax);
    return(net);
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If programming were easy, it wouldn't be worth doing. Here's a really good hint:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string line;
      int x = 40;
    
      std::cout<<"Enter x: ";
      std::getline ( std::cin, line );
    
      if ( line != "" )
        x = std::atoi ( line.c_str() );
    
      std::cout<< x <<'\n';
    }
    I urge you to keep something on the backburner though: atoi is easy, but sucks ass when the string doesn't represent a valid integer. A better solution is strtol, and even better for C++ is stringstream (see the FAQ).
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    That is a difficult thing to do for a beginner, I'm surprised the book asked you to do it. When the user hits enter, the operator>> treats it like whitespace and keeps waiting for more input.

    One way to solve it would be to use the noskipws manipulator. Another would be to use getline to read into a string and check for an empty string before converting it to the number you need. Both are slightly more advanced options than the code in your program.

    Also, you should read in the rate as a floating point number as well, since somebody making 5.50 an hour would not be able to use the program you have now.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    27
    You know what... I didn't even learn std and all the functions that come with it... Is there a way to do it more simple? The focus is on Default Arguments.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    I would skip the part about checking to see if the user hit enter until you learn more about user input and cin.

    To test your default arguments, try asking the user for a number - 1, 2, or 3. If they give you 1, then ask only for the tax and call the function by only passing in the tax. If they hit 2, ask for the tax and hours and call the function by only passing in the tax and hours. If they hit 3, ask for the tax, hours, and rate and call the function by passing in all three values.

    Hopefully you have learned about if statements at this point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member as default argument
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2009, 08:09 AM
  2. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 02:52 PM
  3. Question about Templates and passing arguments
    By supaben34 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2002, 01:32 AM
  4. Variable Arguments Question
    By moonwalker in forum C Programming
    Replies: 8
    Last Post: 08-04-2002, 09:08 AM
  5. Switching Default Buttons :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2002, 04:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21