Switch wont initialize

Write your question here.
I just started learning C++ so this is just a basic calculator program I was working on to get a feel for basic functions. The problem is when I run the program it just ends after the two inputs are requested completely skipping the switch step which is the whole point of the program. can you not use functions inside of a switch?


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
#include <iostream>
#include <conio.h>
#include <windows.h>
#define NEWLINE '\n'

using namespace std;

  int addition (int a, int b)
{
    int r;
    r=a+b;
    return(r);
}
int subtraction (int a, int b)
{
    int r;
    r=a-b;
    return(r);
}
int multiplication (int a, int b)
{
    int r;
    r=a*b;
    return(r);
}
int division(int a, int b)
{
      int r;
      r=a*b;
      return(r);
}

int main()
{
    int a, b, z;
    cout << "Enter two integers";
    Sleep(1000);
    cout << endl;
    cout << NEWLINE;
    cin >> a, b;
    cout << NEWLINE;
    cout << "For addition enter one, for subtraction two, for multiplication three and for division 4";
    cin >> z;
    cout << NEWLINE;
    //this is the point where the program just ends without continuing on to the switch
switch (z){
           case 1:
                cout << addition(a,b);
                getch();
                break;
           case 2:
                 cout << subtraction(a,b);
                 getch();
                 break;
           case 3:
                cout << multiplication(a,b);
                getch();
                break;
           case 4:
                cout << division(a,b);
                getch();
                break;
                }
I am using DEV C++ as my compiler if that helps
This line is incorrect:
 
    cin >> a, b;

change it to:
 
    cin >> a >> b;


Incidentally, as a matter of style, your functions can be simplified
1
2
3
4
5
6
int addition (int a, int b)
{
    int r;
    r=a+b;
    return(r);
}

This would be simpler:
1
2
3
4
int addition (int a, int b)
{
    return a + b;
}

this is the point where the program just ends without continuing on to the switch

What evidence do you have that it's not continuing on to the switch?

Isn't it far more likely that it is processing the switch statement, but the value of z doesn't correspond to any of the cases you've programmed? After all, you've neglected to add a default case.
Topic archived. No new replies allowed.