Creating a function using a string?

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).

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream.h>
#include <iomanip.h>
double options(string, string, string);
int main()
{
	using namespace std;
	double order;
	cout << "Wawa Order" << endl;

	options(Shorti, Junior, King);
        cout << "Hoagie Size?: ";
	cin >> order;
return 0; 

options(string option1, string option2, string option3)
{
	cout << "1." << option1 << endl;
	cout << "2." << option2 << endl;
	cout << "3." << option3 << endl;
}

Last edited on
This code works with a few adjustments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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

using namespace 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()
{
	using namespace 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:
 
cout << options(Shorti, Junior, King);

Does that help you?
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
Last edited on
What I want to do is make the options(Shorti, Junior, King); substituteable with anything.


They are. Make your substitutions in this part of the code:

1
2
3
#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 


Or alternatively, write them into the function call:

options("shorti", "junior", "king");

options("small", "medium", "large");
Last edited on
so I can just type #define XXXX "option1" at any point in the code
I think he means something more like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

void options(string x[]);

int main(int argc, char* argv[])
{
    std::string x[3];

    std::cout << "Enter 3 words..." << std::endl;
    for(int i = 0;i < 3;i++)
    {
        std::cout << "Word " << i << ": ";
        std::cin >> x[i];
    }

    options(x);

    std::cin.get();
    std::cout << "Press any key to continue...";
    std::cin.get();

    return 0;
}

void options(string x[])
{
    for(int i = 0;i < 3;i++)
    {
        std::cout << i << ". " << x[i] << std::endl;
    }
}
Last edited on
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:

const std::string foo = "bar";
Last edited on
so I can just type #define XXXX "option1" at any point in the code

It sounds like you're trying to use #define like you would set a variable. Have this at the start of your program:

1
2
3
string option1;
string option2;
string option3;


And set them when you need them:

1
2
3
string option1 = "Shorti";
string option2 = "Junior";
string option3 = "King";


And then when you need to output them:

 
options(option1, option2, option3);


I don't see what the problem is. You just gave us a chunk of code and it does what you want it to do. Or am I missing the point?
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!
programmer47 (43)

Why use #define here is a simpler way to get the job done.
And there is a mistake in the switch.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using namespace 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()
{
	using namespace 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 


Wawa Order
Hoagie Size? 
1.Shorti
2.Junior
3.King


Topic archived. No new replies allowed.