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.
#include <iostream>
usingnamespace 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;
}
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;
}
#include <iostream>
usingnamespace 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;
}