Help with switch statement

Hello, I am having trouble with a switch statement to check in an entered integer is a multiple of a number. I am just starting to learn C++ and any help would be appreciated.

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
  #include <iostream>
using namespace std;

int main()
{
	
	int num;
  cout<< "Enter a number: ";
  cin >> num;

  int remainder = num % 3;

  switch(remainder)
  {
    case 0:
      cout << "multiple of 3";
      break;

    case 1:
      cout << "multiple of 3 plus 1";
      break;
    
	case 2:
      cout << "multiple of 3 plus 2";
  getchar();
  getchar();
	return 0;
}
Last edited on
I see your problem. You never actually closed the switch statement.

After case 2, you just go straight to the outskirts of the statement. Solution -
1
2
3
4
5
6
7
8
9
10
11
	case 2:
		cout << "multiple of 3 plus 2";
		break;	// <not required> break statement here, signifying end of the case
	}	// Close the switch statement here

	// Other stuff (not sure if this was supposed to be in case 2 - couldn't tell :|

	getchar();
	getchar();
	return 0;
}


Thank you for your reply and solution!

I fixed it a little bit, but now I have a new problem.
When I input 0, it says that it is a multiple of 3.
How would I go about fixing that?

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
#include <iostream>
using namespace std;

int main()
{
	
int x;
  cout << "Please enter a number: ";
  cin >>x;

  int remainder =x % 3;

  switch(remainder)
  {
    case 0:
      cout <<x << " is a multiple of 3";
      break;

    case 1:
      cout <<x << " is not a multiple of 3";
      break;

    default:
      cout <<x << " is not a multiple of 3";
  }

  getchar();
  getchar();

	return 0;
}
Topic archived. No new replies allowed.