Passing info to main

This is a discussion on Passing info to main within the C++ Programming forums, part of the General Programming Boards category; I have a simple function : Code: void Age_Class::enter_info () { cout << " Enter your age" << endl; cin ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Passing info to main

    I have a simple function :

    Code:
    void Age_Class::enter_info ()
        {
         cout << " Enter your age" << endl;
         cin >> age
       }
    I would like to send the info (age) to the main block for calculation, how can I do that

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, you can return the age, or you can store it in a private variable of the class and have a GetAge() function or something.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    I had
    return age;
    in the function. but I get errors, what is the proper way to return it.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    You need to declare the function as a function that actually returns a value. You need to change 'void' to 'int'.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    I ran to a similar problem with a different project. Here is part of the code:

    Code:
    // get_monthly_total Functions - calculates the total bill
    // Standard Plan Class
    void Standard_Plan_Class::get_monthly_total ()
         {
          // Local Variables
           double payment = 20.00;      // Basic Monthly Payment -> $ 20
           double extra_hours;             // Extra hours over the 20h monthly limit
           double over;                         // # of Extra Hours over 20h limit * 50 cents each
      
           if (hours_online > 20)
             {
               // Monthly Bill Calculation
               extra_hours = hours_online - 20;
               over = extra_hours * .50;
               total = payment + over;
             }
              else
                   {
                      total = payment;
                   }
         }
    
    
    
    
    
    // calculation Functions - gets "total" amount from get_monthly_total
    double Standard_Plan_Class::calculation ()
          {
           return total ;
          }
    
    
    
    
    // pc_output Functions - display information to the user
    // Standard Plan Class
    void Standard_Plan_Class::pc_output ()
       {
          cout << "Your total monthly payment is: $ " << calculation () << endl;
       }
    The problem is that no matter what number I type in (hours_online) the total monthly payment always comes out to 0.00.

    What is causing this problem ?

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Get rid of that 'calculation' function and simply return total at the end of get_monthly_total. Not only are both functions misleading (i.e. you would think get_monthly_total returns a value and calculation would actually calculate something) but separating calculation and returning the value is just pointless. Let me guess how you are using your code. You create the object and then call pc_output, right ? You'd need to use get_monthly_total which does not return anything but calculates, instead... Which should have been done by your calculation function which actually returns a value.. ;-) You fell into a trap you just set :P

  7. #7
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    write the function in main with return age ?
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Mmm.. I don't want to be mean, but man, you really have to read back about functions. Perhaps you are going too fast without thoroughly learning the basics. You shouldn't be using classes before actually understanding functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 03:12 PM
  2. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 10:46 PM
  3. void main
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-29-2002, 08:08 PM
  4. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM
  5. passing a file name to main()
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 09-06-2001, 05:08 PM

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