Switch statement used correctly?

The goal of this program is simple, if the user inputs a number between 0 and 99, the output should be the English word.

This is for a Branching Project in which I'm focusing on Switch statements and If-Else.

Am I writing this properly? Case is a keyword which should be followed by a literal Integer, correct? Am I utilizing the Switch statement properly? Any advice would be great as I write the rest of this code.

I wrote case 0 and case 1 differently, would either accomplish this task?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main() {


	int first_number, second_number; // natural numbers
	cout << "Enter a number between 0 and 99: ";
	cin >> first_number >> second_number; 

	if( first_number == '1')
	switch( second_number){
	
	
	case 0:
		cout << "Ten" << endl;
		break;

	case 1:
	    cout << "Eleven ";
	    second_number = 1;
	    break;
Without bracket following the if, only the next statement executes when the conditional is true. Therefore that code shouldn't compile. I think that you need to go and read the tutorial section on conditionals. Also why are you telling the user to enter one number but then the cin will ask the user for two numbers? Are you thinking that the first digit will go into first_number, and the second digit into second_number? That is not how it will work. The integer typed before the user presses enter will be assigned to the first, and then the program will wait for a second number to be entered. The user would probably sit there and wonder why the program didn't do anything.
Sorry, but inputting to int just doesn't work that way, I know it would have been nice if it did since you could then make short-cuts like if the first digit is two output the word "twenty" and if the second digit is 3 output the word "three". That might have saved a lot of coding, but using int kind of has you stuck with this;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main() {


	int number; // natural numbers
	cout << "Enter a number between 0 and 99: ";
	cin >> number; 
	switch( number){
	
	
	case 0:
		cout << "Zero" << endl;
		break;

	case 1:
	    cout << "One ";
	    break;


But if you used a char array like char[3] then you would be able to look at each digit individually;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main() {


	char number[2];
	cout << "Enter a number between 0 and 99: ";
	cin >> number; 

	if( number[0] == '1')
	switch( number[1]){
	
	
	case 0:
		cout << "Ten" << endl;
		break;

	case 1:
	    cout << "Eleven ";
	    second_number = 1;
	    break;
Last edited on
@kempofighter - In the instructions, I'm told that the user will place a space between the number. So for "11" they would enter "1 1".

It's a beginner's exercise.

@Newbie - Thank you, I'm going to start over and work with "Char"
Actually using a char array was just a way to not need that space in between the numbers, if your teacher says in the instructions to have the user place a space in between the numbers then they were expecting you to use int.

Your original program would have worked just fine and is probably the way to go. Just remove the single quotes from line 11 of your original code and it should work.
Topic archived. No new replies allowed.