Designing Menu/Including (void) function/Troubleshoot


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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>                                                       //HEADERFILE I/O
#include <unistd.h>                                                       //HEADERFILE sleep() 
#include <cstdlib>
#include <ctime>
using namespace std;

void ohmslaw(void)                                                        //DECLARE/DEFINE FUNCTION (ohmslaw)
{                                                                         //START_DEFINE FUNCTION (ohmslaw)
    char calculation = ' ';
    float volts = 0, current = 0, resistance = 0;
    do
    {                                                                     //START_DO
        system("clear");
        cout << "\t****OHMSLAW MENU****\n\n"
        << "(V) calculate voltage\n"
        << "(I) calculate current\n"
        << "(R) calculate resistance\n\n"
        << "\tENTER CALCULATION: ";
        cin  >> calculation;
        
        switch (calculation)
        {                                                                 //START_SWITCH
            case 'v':
            case 'V':
                cout << "\n\t\t\tenter current value: ";
                cin  >> current;
                cout << "\t\t\tenter resistance value: ";
                cin  >> resistance;
                volts = current * resistance;                             //CALCULATE VOLTS
                cout << "\n\t\t\t\t\t\tVolts = "
                << volts << endl;
                sleep(3);
                break;
            case 'i':
            case 'I':
                cout << "\n\t\t\tenter volts value: ";
                cin  >> volts;
                cout << "\t\t\tenter resistance value: ";
                cin  >> resistance;
                current = volts / resistance;                             //CALCULATE CURRENT
                cout << "\n\t\t\t\t\t\tCurrent = "
                << current << endl;
                sleep(3);
                break;
            case 'r':
            case 'R':
                cout << "\n\t\t\tenter volts value: ";
                cin  >> volts;
                cout << "\t\t\tenter current value: ";
                cin  >> current;
                resistance = volts / current;                             //CALCULATE RESISTANCE
                cout << "\n\t\t\t\t\t\tresistance = "
                << resistance << endl;
                sleep(3);
                break;
            case 'q':
            case 'Q':
                return;
                break;
            default:
                cout << "\n\t----INVALID INPUT----\n"
                << endl;
                sleep(2);
                break;
        }                                                                 //END_SWITCH
    }                                                                     //END_DO
    while (true);
}                                                                         //END_DEFINE FUNCTION (ohmslaw)


void game(void)                                                           //DECLARE/DEFINE FUNCTION (game)
{                                                                         //START_DEFINE FUNCTION (game)
    char option = ' ';
    
    do
    {                                                                     //START_DO
        system("clear");
        cout << "\t****GAME OPTION MENU****\n\n"
             << "(S) Start Game\n"
             << "(Q) Quit Game\n\n"
             << "Enter Selection: ";
        cin  >> option;
        
        switch (option)
        {                                                                 //START_SWITCH
            case 's':
            case 'S':
                int num, guess;
                bool isGuessed = false;
                srand(time(0));
                num = rand() % 100;
                
                
                while (!isGuessed)
                {                                                         //START_WHILE
                    cout << "Enter an integer greater than or equal"
                         << " to 0 and less than 100: ";
                    cin >> guess;
                    cout << endl;
                    
                    if (guess <0 || guess >100)
                    {                                                     //START_IF
                        cout << "Invalid Input" << endl;
                        return;
                    }                                                     //END_IF
            
                    else if (guess == num)
                    {                                                     //START_IF
                        cout << "You guessed the correct number" << endl;
                        isGuessed = true;
                    }                                                     //END_IF
                    
                    else if (guess < num)
                    {                                                     //START_ELSE IF
                        cout << "Your guess is lower than"
                        << " the number.\nGuess again!" << endl;
                    }                                                     //END_ELSE IF
                    
                    else if (guess > num)
                    {                                                     //START_ELSE IF
                        cout << "Your gues is higher than the number."
                        << "\nGuess again!" << endl;
                    }                                                     //END_ELSE IF
                }                                                         //END_WHILE
                break;
            case 'q':
            case 'Q':
                    cout << "Exiting Program in\n\n";
                    sleep(1);
                    cout << "3...\n";
                    sleep(1);
                    cout << "2...\n";
                    sleep(1);
                    cout << "1...\n";
                    sleep(1);
                    cout << "...Peace Out\n";
                    return;
                    break;
            default:
                    cout << "invalid input" << endl;
                    break;
        }                                                                 //END_SWITCH
    }                                                                     //END_DO
}                                                                         //END_DEFINE FUNCTION (game)


int main ()
{                                                                         //START_MAIN
  char option = ' ';                                         
    do
    {                                                                     //START_DO
        system("clear");                                      
        cout << "\t****MENU OPTIONS****\n\n"
             << "(A) ohms law\n"
             << "(B) guessing game\n"
             << "(C) exit program\n\n"
             << "\tENTER OPTION: ";
        cin  >> option;
        
        switch (option)
            {                                                             //START_SWITCH
            case 'o':
            case 'O':
                ohmslaw();                                                //CALLING FUNCTION (ohmslaw)
                break;
            case 'g':
            case 'G':
                game();
                break;
            case 'q':
            case 'Q':
                cout << "Exiting Program in\n\n";
                sleep(1);                                  
                cout << "3...\n";
                sleep(1);                                   
                cout << "2...\n";
                sleep(1);                                   
                cout << "1...\n";
                sleep(1);                                    
                    cout << "...Peace Out\n";
                return 0;                                    
                break;
            default:
                cout << "\n\t----INVALID INPUT----\n" << endl;
                break;
            }                                                             //END_SWITCH
        sleep(3);                                          
    }                                                                     //END_DO
    while (true);
    return 0;                                              
}                                                                         //END_MAIN      
[/code]
The program wants me to build a menu that incorporates call functions. There are three selections in the menu (Ohm's Law, Number Guessing Game, and the option to End Program). I've declared and defined my two functions "game" and "ohmslaw" before my source code as cplusplus.com has instructed me to do from the reference tab in the section titled (Functions I).

I'm having trouble defining the function "game". I'm receiving three errors in lines 90, 144 and 191 . I've tried to define the function "game" using a switch, like I defined the function "ohmslaw" to be. This is not compiling I believe I'm using the wrong control structure for the function "game".

I've successfully compiled the function "ohmslaw" so i know it works. Therefore, my problem is in the function "game". I got this code for the guessing game out of an example in my book. It doesn't use it as a call function though. So i'm completely lost how to define the function "game" so it runs smoothly and exits smoothly.

I've tired to be as technical and organized to make troubleshooting more efficient. I apologize in advance if it's not to your standard. I'm a student and this is one of the labs for this week. I'm just having a hard time and here it's 4:19AM and i'm still at it.

The code is constructed in Xcode and the environment is OS X if this is important to know. Thank you for your time and any advice.
Last edited on
You are missing the while in the do-while loop in game.

When you have defined and initialized a variable in one statement you are not allowed to jump (by using a switch-case or goto) over it to skip that statement to a place where the variable is still in scope. If it was possible, doing so would allow you to access variables that has not been initialized even though they have constructors that would normally do the initialization.

So when defining variables inside a switch the trick is to limit the scope of the variables by using a pair of { }, so that the scope doesn't cross any case labels.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch (option)
{
	case 's':
	case 'S':
	{
		int num, guess;
		bool isGuessed = false;
		
		...
		
		break;
	}
	case 'q':
	case 'Q':
	
		...	

}



Don't call srand every time you call rand. It will make it less random. Instead call srand once and only once in your program (at the beginning of main is a good place).
Topic archived. No new replies allowed.