Switch problem.

Hello I am kind of new to C++ and the programming stuff and I really want to learn it. So I went to the library and loaned some books and im halfway through the first book, and now I encountered a problem wich i've been struggeling for weeks with and now I finally chosed to register to this website and was looking for answers from other programmers.

Keep in mind (i figure this is a kind of newbie question so please dont flame me)


Im supposed to do a kind of a easy calculator thingie, and I stuck with this switch case where if I choose ex. "3"it should multiply ex. "6=3*2" get it? :P

maybe its better if I show.

this is how the code looks like.



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
#include<iostream>
#include<cstdlib>

using namespace std;

int main()
{
    int val,b,c; // val = switch variable and -b- and -c- (i figured u needed 2 variables for this) is whatever chosen to be added)
    
    cout <<"Meny"<<endl<<endl;
    cout <<"1=Addera"<<endl;
    cout <<"2=Subtrahera"<<endl;
    cout <<"3=Multiplicera"<<endl;
    cout <<"4=Dividera"<<endl;
    cout <<"Ange ditt val: "<<endl;
    
    cin >> val;
    
    switch (val)
    {
           case 1: c=b+b; // if chosen 1 ex. 2=1+1
           break;
           case 2: c=b-b; // if chosen 2 ex. 0=1-1
           break;
           case 3: c=b*b; // if chosen 3 ex. 8=4*2
           break;
           case 4: c=b/b; // if chosen 4 ex. 4=8/2
           break;
           }
           
           cout <<"Ange ett tal"<<endl; //this is where a number is to be put.
           cin >> b;
           cout <<"Ange ett tal till"<<endl; // and this is where its either multiplied or added or whatever.
           cin >> b;
           cout <<"Summan blir "<< c <<endl; // this is where it will show the sum of the both numbers.
           cout <<"Starta om programmet för att få se menyn igen";
           
           system ("PAUSE");
           return EXIT_SUCCESS;
           }
                


thats how I build it and when I try it out (in an console application that is) i get. this program is swedish so if you see some miss-spelled stuff dont flame me

"
Meny

1=Addera
2=Subtrahera
3=Multiplicera
4=Dividera

Ange ditt val: 1 (i choosed 1 for this)
1
Ange ett tal
5
Ange ett tal till
10
Summan blir 5242512
________________________________________________________________________________

The problem is the sum.. why do i get "5242512" as a sum when i clearly chosed "+" and i typed 5+10 its supposed to be 15??? right?


please help me and also type how to fix

and where to fix where the syntax were wrong =)

Thank you all so much for reading and thank you all for helping.

oh and one more thing. i would be pleased if you showed me the "right" code
Last edited on
closed account (3hM2Nwbp)
Hello, your variables 'val', 'b', and 'c' were never initialised, so they hold the value that was previously allocated at their memory location, which would explain your excessively large value. You will need to obtain values for variables 'b' and 'c' through user input (in your case std::cin). If you need more help don't hesitate to ask.

-edit-
You can use this as a guide
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
#include<iostream>
#include<cstdlib>

using namespace std;
int main()
{
	int operatorCode = 0;
	int b = 0;
	int c = 0;
	int result = 0; //new
	cout <<	"Enter the first operand (Ange ett tal)" << endl;
	cin >> b;
	while(cin.fail())
	{
		cin.ignore('\n', numeric_limits<streamsize>::max());
		cout << "Invalid entry - Enter the first operand (Ange ett tal)" << endl;
		cin >> b;
	}
	cout << "Enter the second operand (Ange ett tal till)" << endl;
	cin >> c;
	while(cin.fail())
	{
		cin.ignore('\n', numeric_limits<streamsize>::max());
		cout << "Invalid entry - Enter the second operand (Ange ett tal till)" << endl;
		cin >> c;
	}
	cout << "Which operation do you wish to perform? (Ange ditt val:)" << endl;
    cout <<	"Menu (English Version)"<<endl<<endl;
    cout <<	"1=Addition"<<endl;
    cout <<	"2=Subtraction"<<endl;
    cout <<	"3=Multiplication"<<endl;
    cout <<	"4=Division"<<endl;
	cin >> operatorCode;
    while(cin.fail() || operatorCode < 1 || operatorCode > 4)
	{
		cin.ignore('\n', numeric_limits<streamsize>::max());
		cout << "Invalid operator selection - Which operation do you wish to perform? (Ange ditt val)"<<endl;
		cin >> operatorCode;
	}
    switch (operatorCode)
    {
           case 1: result=b+c; // if chosen 1 ex. 2=1+1
           break;
           case 2: result=b-c; // if chosen 2 ex. 0=1-1
           break;
           case 3: result=b*c; // if chosen 3 ex. 8=4*2
           break;
           case 4: result=b/c; // if chosen 4 ex. 4=8/2
           break;
    }
	cout <<"Result: (Summan blir) "<< result <<endl; // this is where it will show the sum of the both numbers.
	cout <<"Press Enter to exit the program.(Starta om programmet för att få se menyn igen)"<<endl;
	cin.ignore('\n', numeric_limits<streamsize>::max());
	return 0;
}


Best regards,
Luc
Last edited on
Your doing math for the variables before you get their values. Just shift the switch statements down after your input from the user.

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
#include<iostream>
#include<cstdlib>

using namespace std;

int main()
{
    int val,b,c; // val = switch variable and -b- and -c- (i figured u needed 2 variables for this) is whatever chosen to be added)
    
    cout <<"Meny"<<endl<<endl;
    cout <<"1=Addera"<<endl;
    cout <<"2=Subtrahera"<<endl;
    cout <<"3=Multiplicera"<<endl;
    cout <<"4=Dividera"<<endl;
    cout <<"Ange ditt val: "<<endl;
    
    cin >> val;
           
    cout <<"Ange ett tal"<<endl; //this is where a number is to be put.
           cin >> b;
           cout <<"Ange ett tal till"<<endl; // and this is where its either multiplied or added or whatever.
           cin >> b;

    switch (val)
    {
           case 1: c=b+b; // if chosen 1 ex. 2=1+1
           break;
           case 2: c=b-b; // if chosen 2 ex. 0=1-1
           break;
           case 3: c=b*b; // if chosen 3 ex. 8=4*2
           break;
           case 4: c=b/b; // if chosen 4 ex. 4=8/2
           break;
           }

           cout <<"Summan blir "<< c <<endl; // this is where it will show the sum of the both numbers.
           cout <<"Starta om programmet för att få se menyn igen";
           
           system ("PAUSE");
           return EXIT_SUCCESS;
           }
               


You may also need an 'a' variable as you are setting b twice removing the first input from the user and only keeping the second so running your program with only this fix if you enter the same values you'd get 20, since b = 10 and 10+10 = 20. just make an int a, substitute it for the first cin >> b, and one be in every operation. Then its all good.
Thank you guys very much. You dont know how useful this have been! :)

I will study the codes later beacuse im a little late for school haha =).

Thank you again for this, I have been struggeling for weeks to get this right :D!

Great website I did good in registering here :D
Topic archived. No new replies allowed.