menu using goto function with variables

Hello, I am trying to make a menu for my program that goes to a label depending on the letter entered. Any help would be much appreciated.
Here is my code:

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
100
#include <iostream>
#include <windows.h>
#include <cmath>

using namespace std;
double x, y;
string z, s, d;
string l = ("wget.exe "), k = ("color ");
int main()
{
e:
    system("title OmniTool");
    system("cls");
    cout << ("Please choose a mode.") << endl;
    cout << ("Press m to goto Multiplication") << endl;
    cout << ("Press d to goto Division") << endl;
    cout << ("Press a to goto Addition") << endl;
    cout << ("Press s to goto Subtraction") << endl;
    cout << ("Type sqr to goto SquareRoot") << endl;
    cout << ("Press c to change color.") << endl;
    cout << ("Press w to goto Website Downloader.") << endl;
    cout << ("Press q to exit.") << endl;
    cout << ("Mode: ");
    cin >> d;
    goto + d;

m:
    system("cls");
    cout << ("Multiplication") << endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x*y << endl;
    Sleep(3000);
    goto e;

d:
    system("cls");
    cout << ("Division") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x/y << endl;
    Sleep(3000);
    goto e;

a:
    system("cls");
    cout << ("Addition") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x+y << endl;
    Sleep(3000);
    goto e;

s:
    system("cls");
    cout << ("Subtraction") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x-y << endl;
    Sleep(3000);
    goto e;

sqr:
    system("cls");
    cout << ("SquareRoot") << endl;
    cin >> x;
    sqrt(x);
    cout << ("The answer is:") << y << endl;
    Sleep(3000);
    goto e;
w:
    system("cls");
    cout << ("Enter the URL for the website that you wish to download to download:") << endl;
    cout << ("URL: ") << endl;
    cin >> z;
    l = l + z;
    system(l.c_str());
    goto e;

q:
    return 0;

c:
    system("cls");
    cout << ("Console ColorChanger.") << endl;
    cout << ("choose a console color:") << endl;
    cout <<   ("0 = Black       8 = Gray") <<endl;
    cout <<   ("1 = Blue        9 = Light Blue") <<endl;
    cout <<   ("2 = Green       A = Light Green") <<endl;
    cout <<   ("3 = Aqua        B = Light Aqua") <<endl;
    cout <<   ("4 = Red         C = Light Red") <<endl;
    cout <<   ("5 = Purple      D = Light Purple") <<endl;
    cout <<   ("6 = Yellow      E = Light Yellow")<<endl;
    cout <<   ("7 = White       F = Bright White") <<"\n";
    cout << ("color: ");
    cin >> s;
    k = k + s;
    system(k.c_str());
    goto e;
}


And this the code I need help with is the code that is underlined and bold:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
system("title OmniTool");
    system("cls");
    cout << ("Please choose a mode.") << endl;
    cout << ("Press m to goto Multiplication") << endl;
    cout << ("Press d to goto Division") << endl;
    cout << ("Press a to goto Addition") << endl;
    cout << ("Press s to goto Subtraction") << endl;
    cout << ("Type sqr to goto SquareRoot") << endl;
    cout << ("Press c to change color.") << endl;
    cout << ("Press w to goto Website Downloader.") << endl;
    cout << ("Press q to exit.") << endl;
    cout << ("Mode: ");
    cin >> d;
    goto + d; // get value for d and add it to the end of the "goto"  
// function 
Last edited on
What you've done here is attempt to replicate functions.

goto + d; // get value for d and add it to the end of the "goto"

This just makes no sense at all. goto is a keyword. It is not a function. It is not a variable. Trying to add something to it is completely nonsensical.

You're trying to mash features that already exist (functions) out of something wholly unsuitable. Why not just use functions?
You should forget that C/C++ has goto operator and never use it.
well is there a similar function that can be used to goto another part of the program that can added onto to allow user input to determine the destination?
is there a similar function


switch/case and functions spring to mind.

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
using namespace std;
double x, y;
char d;
string z, s;
string l = ("wget.exe "), k = ("color ");

void multiplication()
    {
    system("cls");
    cout << ("Multiplication") << endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x*y << endl;
    Sleep(3000);
    }

 void division()
{
    cout << ("Division") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x/y << endl;
    Sleep(3000);
  }

int main()
{
    system("title OmniTool");
    system("cls");
    cout << ("Please choose a mode.") << endl;
    cout << ("Press m to goto Multiplication") << endl;
    cout << ("Press d to goto Division") << endl;
    cout << ("Press a to goto Addition") << endl;
    cout << ("Press s to goto Subtraction") << endl;
    cout << ("Type sqr to goto SquareRoot") << endl;
    cout << ("Press c to change color.") << endl;
    cout << ("Press w to goto Website Downloader.") << endl;
    cout << ("Press q to exit.") << endl;
    cout << ("Mode: ");
    cin >> d;

    switch(d)
  {
    case 'm':
          multiplication();
          break;
     case 'd':
          division();
          break;
      // and so on
}
}


Last edited on
Thanks and this would work for all the parts of the program?
Yes
ok thanks. I will test it.
I got it to work 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
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
100
101
102
103
#include <iostream>
#include <windows.h>
#include <cmath>

using namespace std;
double x, y;
string z, s;
char d;
string l = ("wget.exe "), k = ("color ");
int main()
{
    system("title OmniTool");
    system("cls");
    cout << ("Please choose a mode.") << endl;
    cout << ("Press m to goto Multiplication") << endl;
    cout << ("Press d to goto Division") << endl;
    cout << ("Press a to goto Addition") << endl;
    cout << ("Press s to goto Subtraction") << endl;
    cout << ("Type r to goto SquareRoot") << endl;
    cout << ("Press c to change color.") << endl;
    cout << ("Press w to goto Website Downloader.") << endl;
    cout << ("Press q to exit.") << endl;
    cout << ("Mode: ");
    cin >> d;

    switch(d)
    {
    case 'm':
        system("cls");
        cout << ("Multiplication") << endl;
        cin >> x;
        cin >> y;
        cout << ("The answer is:") << x*y << endl;
        Sleep(3000);
        return 1;

    case 'd':
        system("cls");
        cout << ("Division") <<endl;
        cin >> x;
        cin >> y;
        cout << ("The answer is:") << x/y << endl;
        Sleep(3000);
        return 1;

    case 'a':
        system("cls");
        cout << ("Addition") <<endl;
        cin >> x;
        cin >> y;
        cout << ("The answer is:") << x+y << endl;
        Sleep(3000);
        return 1;

    case 's':
        system("cls");
        cout << ("Subtraction") <<endl;
        cin >> x;
        cin >> y;
        cout << ("The answer is:") << x-y << endl;
        Sleep(3000);
        return 1;

    case 'r':
        system("cls");
        cout << ("SquareRoot") << endl;
        cin >> x;
        sqrt(x);
        cout << ("The answer is:") << y << endl;
        Sleep(3000);
        return 1;
    case 'w':
        system("cls");
        cout << ("Enter the URL for the website that you wish to download to download:") << endl;
        cout << ("URL: ") << endl;
        cin >> z;
        l = l + z;
        system(l.c_str());
        return 1;

    case 'q':
        return 0;

    case 'c':
        system("cls");
        cout << ("Console ColorChanger.") << endl;
        cout << ("choose a console color:") << endl;
        cout <<   ("0 = Black       8 = Gray") <<endl;
        cout <<   ("1 = Blue        9 = Light Blue") <<endl;
        cout <<   ("2 = Green       A = Light Green") <<endl;
        cout <<   ("3 = Aqua        B = Light Aqua") <<endl;
        cout <<   ("4 = Red         C = Light Red") <<endl;
        cout <<   ("5 = Purple      D = Light Purple") <<endl;
        cout <<   ("6 = Yellow      E = Light Yellow")<<endl;
        cout <<   ("7 = White       F = Bright White") <<"\n";
        cout << ("color: ");
        cin >> s;
        k = k + s;
        system(k.c_str());
        return 1;

}
}
although I need the program to return to the main menu after the sleep function has completed. Is there any way to do this?

edit: never mind I figured it out.
Last edited on
I hope you didn't do it using goto or by calling main().
No, I did:
1
2
3
int main()
{
e:

then I put the rest of the code.
Topic archived. No new replies allowed.