menu-driven program (function issue)

i keep getting a warning for case 3.

40 C:\Documents and Settings\Admin\Desktop\me cpu\Untitled4.cpp [Warning] the address of `double memberCost(int, int, int)', will always evaluate as `true'

anyone know why? im trying to pass information from case 2 to case 3 to compute cost



#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

void menu();
void info();
void charges();
void userInfo (int monthsPaid, int sessionAmt, int age);
double memberCost (int monthsPaid, int sessionAmt, int age);
int main ()

{
int response, monthsPaid, sessionAmt, age;

do
{
menu ();
cin >> response;
cout << endl;


switch (response)
{

case 1:
cout << "Here is some basic gym information, as well as prices and discounts" << endl << endl;
info ();
charges ();
break;
case 2:

cout << "Please enter the following information with spaces in between." << endl << "Months of membership, Sessions, and Age." << endl << endl;
userInfo(monthsPaid, sessionAmt, age);
break;
case 3:
memberCost (monthsPaid, sessionAmt, age);

cout<< "Your membership cost is =$" << memberCost << endl << endl;
break;
case 4:
cout << "End Program" << endl;
break;
default:
cout << "Invalid input" << endl;
break;
return 0;
}
}
while (response != 4);
return 0;
}
void menu ()
{

cout << "This program is menu-driven, either by pressing"
<< " 1, 2, 3 or 4. " << endl << endl;
cout << "1: You will be able to obtain pertinent"
<<" information about the gym and its \n prices." << endl << endl;
cout << "2: You will be asked to enter your "
<< "personal information and services required." << endl << endl;
cout << "3: You will be able to view your membership "
<< "cost from your \n information provided." << endl << endl;
cout << "4: To terminate the program." << endl;

}
void info ()
{
cout<< "The gym is open 7 days a week from 8am - 8pm" << endl << endl;
}

void charges ()
{
cout << "Prices \n" << endl << "1 month of membership = $10 \n" << "1 session = $10 " << endl << endl;
cout << "Discounts \n" << endl << "Purchase of 12 or more months = 15% per month \nPurchase of more than 5 sessions = 20% per session \n"
<< "Senior citizen discount (60 and older) = 30% off membership cost" << endl << endl;
}

void userInfo (int monthsPaid, int sessionAmt, int age)
{

cin >> monthsPaid >> sessionAmt >> age;
cout << endl;
}

double memberCost (int monthsPaid, int sessionAmt, int age)
{
double month, session, rate, memberCost, monthCost, sessCost;

if (monthsPaid >= 12)
month = 8.50;
else
month = 10.00;

monthCost = monthsPaid * month;

if (sessionAmt > 5)
session = 8.00;
else
session = 10.00;

sessCost = sessionAmt * session;

if (age >= 60)
rate = .7;
else
rate = 1.0;

memberCost = rate * (sessCost + monthCost);


return memberCost;
}
Last edited on
1
2
3
4
void info (string gymInfo)
{
string gymInfo = cout <<  "The gym is open 7 days a week from 8am - 8pm";
}


in main,

info (gymInfo);
should just be info();

You had gymInfo as an undeclared variable being used as a parameter. It was confusing you since you had the function of the same name. The function is fine.

Make sure to change the prototype to reflect the change.
Last edited on
in order to pass information in and out of functions without using global variables, do i just include a "&" after each data type?
That depends on the purpose of the function.
For example, say you want a function to merely display data, like the one you have.

1
2
3
4
5
void myFunction (int myVariable)
{ 
   //just displaying it
   cout << myVariable << " is my variable.";
}


On the other hand, you can use functions to modify data that you have in your main function.

1
2
3
4
5
void myFunction (int & myVariable)
{
   //changing variable in main
   ++myVariable;
}


This function increments the variable sent from the main function.

You will want to include the '&' in the function if you intend to change a value in the main function. Or else you can just return a value as well, an assign that to a variable in your main function.
Last edited on
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

void menu();
void info();
void charges();
void userInfo (int& monthsPaid, int& sessionAmt, int& age);
double memberCost ();
int main ()

{
int response;

do
{
menu ();
cin >> response;
cout << endl;


switch (response)
{

case 1:
cout << "Here is some basic gym information, as well as prices and discounts" << endl << endl;
info ();
charges ();
break;
case 2:
cout << "Please enter the following information with spaces in between." << endl << "Age, Months of membership, and Sessions required." << endl << endl;
userInfo(int& monthsPaid, int& sessionAmt, int& age);
break;
case 3:
double memberCost (int monthsPaid, int sessionAmt, int age);
break;
case 4:
cout << "program terminate" << endl;
break;
default:
cout << "Invalid input" << endl;
break;
return 0;
}
}
while (response != 4);
return 0;
}
void menu ()
{

cout << "This program is menu-driven, either by pressing"
<< " 1, 2, 3 or 4. " << endl;
cout << "1: You will be able to obtain pertinent"
<<" information about the gym and its \n prices." << endl;
cout << "2: You will be asked to enter your "
<< "personal information and services required." << endl;
cout << "3: You will be able to view your membership "
<< "cost from your \n information provided." << endl;
cout << "4: To terminate the program." << endl;

}
void info ()
{
cout<< "The gym is open 7 days a week from 8am - 8pm" << endl << endl;
}

void charges ()
{
cout << "Prices \n1 month of membership = $10 \n" << "1 session = $10" << endl << endl;
cout << "Discounts \n Purchase of 12 or more months = 15% \n Purchase of more than 5 sessions = 20% \n Senior citizen discount (60 and older) = 30%"
<< endl << endl;
}

void userInfo (int& monthsPaid, int& sessionAmt, int& age)
{

cin >> monthsPaid >> sessionAmt >> age;
cout << endl;
}

double memberCost (int monthsPaid, int sessionAmt, int age)
{
double month, session, rate, memberCost, monthCost, sessCost;

if (monthsPaid >= 12)
month = 8.50;
else
month = 10.00;

monthCost = monthsPaid * month;

if (sessionAmt > 5)
session = 8.00;
else
session = 10.00;

sessCost = sessionAmt * session;

if (age >= 60)
rate = .7;
else
rate = 1.0;

memberCost = rate * (sessCost + monthCost);


return memberCost;
}


im trying to take the data from "2" user info and get it into "3" to determine membership cost. right now im getting an error

34 C:\Documents and Settings\Admin\Desktop\me cpu\Untitled4.cpp expected primary-expression before "int"
Because in your main function, you should have just the variable name.

userInfo(int& monthsPaid, int& sessionAmt, int& age);

In the function definition, include the type and reference symbol.

And make sure that the variables are already declared in main.
Last edited on
is it considered a global variable if i declare them inside the main?
No. That variable will be localized to main. Unless you pass it to a function of course..
then it becomes global? or is it just localized to another function?
i updated the original post at the top, can anyone help?
userInfo takes arguments by value. It needs to take its arguments by reference.

memberCost returns a value. Perhaps you should use the value it returns.

1
2
 double cost = memberCost (monthsPaid, sessionAmt, age) ;
 cout<< "Your membership cost is =$" << cost << endl << endl; 
Last edited on
ok i changed my function to this:

void userInfo (int& monthsPaid, int& sessionAmt, int& age);

i have return at the bottom of my double function, but i still get the same warning
did you ever solve the problem and get it to work?
userInfo takes arguments by value. It needs to take its arguments by reference.
That was my mistake. I had a lapse in memory and told him to change it the wrong way.
can you show that?
i have return at the bottom of my double function, but i still get the same warning
1
2
3
memberCost (monthsPaid, sessionAmt, age);

cout<< "Your membership cost is =$" << memberCost << endl << endl;


When you call the function (first line) you're not using the value it returns


I'm not sure how the compiler interprets memberCost, but the second time you're not calling the function. To do that it should be
cout<< "Your membership cost is =$" << memberCost(monthsPaid, sessionAmt, age) << endl << endl;
Topic archived. No new replies allowed.