Runtime error ?

I am not quite sure where I am going wrong, I am still learning, but the problem is this. I get a run time error that” variable choice is being used without being initialized”. Where am I going wrong?

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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{

    int num, num2;
    int choice;

    switch(choice)
    {
case'1':cout << " Please enter your numbers to add" << endl;
        cin ;
        cout << "num + num2 = " << num + num2 << endl;
        break;

case'2':cout << " Please enter your numbers to subtract" << endl;
        cin ;
        cout << "num - num2 =" << num - num2 << endl;
        break;

case'3':cout << " Please enter your numbers to multipy" << endl;
        cin ;
        cout << "num * num2 = " << num * num2 << endl;
        break;

case '4':cout << " Please enter your numbers to divied" << endl;
         cin ;
         cout << " num / num2 =" << num / num2 << endl << endl;
	break;

default:cout << " That is an invalid input" << endl;
}
        return 0;

}
For example in this code snip

1
2
3
   int choice;

    switch(choice)


variable choice is used though it was not initialized.
It means that the variable has no value. You need to assign it a value before you can do a check on it.
 
cin ;

What is that?

1
2
switch(choice)
case '4'

Choice is an integer, so comparing it to a character literal isn't going to work as expected.

int choice;

You never assigned a value to choice so the compiler just gave it a 'garbage' value.

Fixed 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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{

    int num, num2;
    int choice;
    cin >> choice;

    switch(choice)
    {
case(1):cout << " Please enter your numbers to add" << endl;
        cin >> num >> num2;
        cout << "num + num2 = " << num + num2 << endl;
        break;

case(2):cout << " Please enter your numbers to subtract" << endl;
        cin >> num >> num2;
        cout << "num - num2 =" << num - num2 << endl;
        break;

case(3):cout << " Please enter your numbers to multipy" << endl;
        cin >> num >> num2;
        cout << "num * num2 = " << num * num2 << endl;
        break;

case(4):cout << " Please enter your numbers to divied" << endl;
         cin >> num >> num2;
         cout << " num / num2 =" << num / num2 << endl << endl;
    break;

default:cout << " That is an invalid input" << endl;
}
        return 0;

}
Last edited on
ok I understand now, I did not give the user the place to insert the action. Thanks guys. Just out of courisoity, would it be easier to use the if then statement with the do while instead of the switch?
You can use either if-else or switch. I would prefer to use switch when the condition is a comparision with a fixed set of integral constants.
Last edited on
yes, i think its up to you. if-else or switch-case functions the same..
Topic archived. No new replies allowed.