Getting user input from one function to another

Hi, pretty new to C++ and I have a question.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	void DisplayMenu();
	{
		/*	Function: Displays on the screen the selectin of addition, subtraction and exit.
			Precondition:
			Postcondition:
		*/

			cout << "Please choose from one of the following: ";
			cout << endl;
			cout << "1. Get Arabic number";
			cout << endl;
			cout << "2. Get Roman numeral";
			cout << endl;
			cout << "3. Convert from Arabic to Roman";
			cout << endl;
			cout << "4. Convert from Roman to Arabic";
			cout << endl;
			cout << "5. Print Arabic number";
			cout << endl;
			cout << "6. Print Roman Numberal";
			cout << endl;
			cout << "7. Quit";
			cout << endl;
	}

	int GetCommand();
	{
		/*	Function: Gets the choice from menu
			Preconditon:
			Postcondition:
		*/

		int userChoice;
		
		cout << endl;
		cout << "Please type in your choice: ";
		cin >> userChoice;

		return userChoice;
	}

	void RunChoice();
	{
		/*	Function: Run the command the user selected.
			Precondition:
			Postcondition:
		*/

		int choice;

		choice = GetCommand();

		if(choice==1){
			cout << "You chose #1";
			//run "Get Arabic Number"
		}
		else {
			if (choice==2){
				//run "Get Roman Numeral"
			}
			else {
				if (choice==3){
					//run "Convert from Arabic to Roman"
				}
				else {
					if (choice==4) {
						//run "Convert from Roman to Arabic"
					}
					else {
						if (choice==5) {
							//run "Print Arabic Number"
						}
						else {
							if (choice==6) {
									//run "Print Roman Numeral"
							}
							else {
								if (choice==7) {
									//run "Quit"
								}
							}
						}
					}
				}
			}
		}

		
	}

	return 0;
}


I have this code here and I wanted to retrieve the user input from the GetCommand function over to RunChoice function. However when I try to compile the code it gives me an error Roman Numerals.obj : error LNK2019: unresolved external symbol "int __cdecl GetCommand(void)" (?GetCommand@@YAHXZ) referenced in function _wmain.

Any help would be great. Thanks.
First: don't define your other function(s) inside of main (or "_tmain" in this case; main is a function too...)
Last edited on
Thanks, I've moved it out of the main function. This is the error I receive now (error C2447: '{' : missing function header (old-style formal list?))

Updated code list
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

void DisplayMenu();
	{
		/*	Function: Displays on the screen the selectin of addition, subtraction and exit.
			Precondition:
			Postcondition:
		*/

			cout << "Please choose from one of the following: ";
			cout << endl;
			cout << "1. Get Arabic number";
			cout << endl;
			cout << "2. Get Roman numeral";
			cout << endl;
			cout << "3. Convert from Arabic to Roman";
			cout << endl;
			cout << "4. Convert from Roman to Arabic";
			cout << endl;
			cout << "5. Print Arabic number";
			cout << endl;
			cout << "6. Print Roman Numberal";
			cout << endl;
			cout << "7. Quit";
			cout << endl;
	}

	int GetCommand();
	{
		/*	Function: Gets the choice from menu
			Preconditon:
			Postcondition:
		*/

		int userChoice;
		
		cout << endl;
		cout << "Please type in your choice: ";
		cin >> userChoice;

		return userChoice;
	}

	void RunChoice();
	{
		/*	Function: Run the command the user selected.
			Precondition:
			Postcondition:
		*/

		int choice;

		choice = GetCommand();

		if(choice==1){
			cout << "You chose #1";
			//run "Get Arabic Number"
		}
		else {
			if (choice==2){
				//run "Get Roman Numeral"
			}
			else {
				if (choice==3){
					//run "Convert from Arabic to Roman"
				}
				else {
					if (choice==4) {
						//run "Convert from Roman to Arabic"
					}
					else {
						if (choice==5) {
							//run "Print Arabic Number"
						}
						else {
							if (choice==6) {
									//run "Print Roman Numeral"
							}
							else {
								if (choice==7) {
									//run "Quit"
								}
							}
						}
					}
				}
			}
		}
	}
Sorry to bump this, but any help would be greatly appreciated. Been working on this for a couple hours now and have been googling frantically, but still can't figure it out! Lol.
It works as you told it to do so... What following lines tell to OS(=operating system) to do?

1
2
3
4
int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}


Well... you run command to start program and it starts from first command:
return 0;
-> this function main() (main program) is done
-> QUIT
Thanks for the reply. Okay so how would I approach this? I first off want to create a display menu function which shows the user what to pick. Then I need to create a function that gets the command from the user. Then another that runs the command.

I think I have the basic code correct, but I don't know how to use the variable I got from GetCommand() to RunCommand().

Another error I ran into by moving the functions out of the main() is error C2447: '{' : missing function header (old-style formal list)

Thanks again.
Remove the ; from the end of your function definitions' first lines.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int GetCommand()//; <- these guys are only needed in function headers, i.e. if you aren't supplying the body yet
{
	/*	Function: Gets the choice from menu
		Preconditon:
		Postcondition:
	*/

	int userChoice;

	cout << endl;
	cout << "Please type in your choice: ";
	cin >> userChoice;

	return userChoice;
}
Thanks so much!

It now compiles with no errors.

However, what would I have to put in the main to call the DisplayMenu to begin the program? I hit CTRL+F5 and it shows me the "Press now to continue"

I tried adding the line DisplayMenu(); but it throws an error.
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

void DisplayMenu()
	{
		/*	Function: Displays on the screen the selectin of addition, subtraction and exit.
			Precondition:
			Postcondition:
		*/

			cout << "Please choose from one of the following: ";
			cout << endl;
			cout << "1. Get Arabic number";
			cout << endl;
			cout << "2. Get Roman numeral";
			cout << endl;
			cout << "3. Convert from Arabic to Roman";
			cout << endl;
			cout << "4. Convert from Roman to Arabic";
			cout << endl;
			cout << "5. Print Arabic number";
			cout << endl;
			cout << "6. Print Roman Numberal";
			cout << endl;
			cout << "7. Quit";
			cout << endl;
	}

int GetCommand()
	{
		/*	Function: Gets the choice from menu
			Preconditon:
			Postcondition:
		*/

		int userChoice;
		
		cout << endl;
		cout << "Please type in your choice: ";
		cin >> userChoice;

		return userChoice;
	}

void RunChoice()
	{
		/*	Function: Run the command the user selected.
			Precondition:
			Postcondition:
		*/

		int choice;

		choice = GetCommand();

		if(choice==1){
			cout << "You chose #1";
			//run "Get Arabic Number"
		}
		else {
			if (choice==2){
				//run "Get Roman Numeral"
			}
			else {
				if (choice==3){
					//run "Convert from Arabic to Roman"
				}
				else {
					if (choice==4) {
						//run "Convert from Roman to Arabic"
					}
					else {
						if (choice==5) {
							//run "Print Arabic Number"
						}
						else {
							if (choice==6) {
									//run "Print Roman Numeral"
							}
							else {
								if (choice==7) {
									//run "Quit"
								}
							}
						}
					}
				}
			}
		}
	}
Last edited on
Your main function doesn't do anything. It just exits immediately:

1
2
3
4
int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}


The other functions you wrote won't run unless you call them:

1
2
3
4
5
6
int _tmain(int argc, _TCHAR* argv[])
{
	DisplayMenu(); // calls DisplayMenu -- runs all the code inside that function
	// .. etc
	return 0;
}


Of course if you do that you'll get an error that DisplayMenu is undefined. This is because it is delcared after main.

You can solve this one of two ways:

1) move main so that it's defined after your other functions.

or

2) Prototype your other functions above main
Thank you so much! Everything works.

Thanks for all your patience.
Topic archived. No new replies allowed.