I have a simple function :
I would like to send the info (age) to the main block for calculation, how can I do thatCode:void Age_Class::enter_info ()
{
cout << " Enter your age" << endl;
cin >> age
}
Printable View
I have a simple function :
I would like to send the info (age) to the main block for calculation, how can I do thatCode:void Age_Class::enter_info ()
{
cout << " Enter your age" << endl;
cin >> age
}
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.
I had
in the function. but I get errors, what is the proper way to return it.Quote:
return age;
You need to declare the function as a function that actually returns a value. You need to change 'void' to 'int'.
I ran to a similar problem with a different project. Here is part of the code:
The problem is that no matter what number I type in (hours_online) the total monthly payment always comes out to 0.00.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;
}
What is causing this problem ?
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
write the function in main with return age ?
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.