So, if you can see what I'm trying to do here, please help me out. I want to make a function so that I don't have to type out the options every time. this is supposed to be a menu, and the user types a number to choose their option.
I believe the way to do this is using strings as the function input, but I have NO idea. Please help me out! If possible, try to stick to the format I have it in (so i can understand it).
#include <iostream.h>
#define Shorti "option 1" // I've defined Shorti,
#define Junior "option 2" // Junior,
#define King "option 3" // and King as option 1, 2 and 3
usingnamespace std; // You forgot to use the std namespace
double options(string option1, string option2, string option3) // Declare options all in one line, and put it above the main function
{
cout << "1." << option1 << endl;
cout << "2." << option2 << endl;
cout << "3." << option3 << endl;
}
int main()
{
usingnamespace std;
double order;
cout << "Wawa Order" << endl;
cout << "Hoagie Size? ";
options(Shorti, Junior, King);
cin >> order;
/***********************************
** Here's the extra bit for the menu choice**
***********************************/
switch(order)
{
case 1:
{
// Do stuff for 1
}
case 2:
{
// Do stuff for 2
}
case 3:
{
// Do stuff for 3
}
}
return 0;
} // You forgot to end the main function
Another way to have options declared as a string and return the output so the main function would have:
I think that helps, but its not exactly what I want to do. What I want to do is make the options(Shorti, Junior, King); substituteable with anything. In other words, I want to be able to change that to Drink sizes, and when i put the input in options(), the drink sizes will come up
This might help.
options(shorti, junior, king)
1. shorti
2. junior
3. king
options(small, medium, large)
1. small
2. medium
3. large
so the function outputs what I enter next to the numbers. the shorti junior and king are not the variables, the option1 option2 option3 are the changeable variables
so I can just type #define XXXX "option1" at any point in the code
No. The preprocessor is a PRE processor. It expands all the macros before any code is read. You can place your macro anywhere in the program, but it will be expanded before anything.
By the way, in C++ you shouldn't use macros to define constants. Use the const keyword:
Oh, it does work how I want thanks Mochops for clearing it up. I just took out the #define totally and used options("xxx", "xxx") etc. Thanks alot guys!
#include <iostream>
usingnamespace std; // You forgot to use the std namespace
double options(string option1, string option2, string option3) // Declare options all in one line, and put it above the main function
{
cout << "1." << option1 << endl;
cout << "2." << option2 << endl;
cout << "3." << option3 << endl;
}
int main()
{
usingnamespace std;
// double order; can't use double in a switch.
int order;
cout << "Wawa Order" << endl;
cout << "Hoagie Size? \n";
options("Shorti", "Junior", "King"); // enter what ever you want in these double quotes that
//will appear on the screen
cin >> order;
/***********************************
** Here's the extra bit for the menu choice**
***********************************/
switch(order)
{
case 1:
{
// Do stuff for 1
}
case 2:
{
// Do stuff for 2
}
case 3:
{
// Do stuff for 3
}
}
return 0;
} // You forgot to end the main function