Creating a C++ program to have the user input a number between 1 and 4. Must use switch statement.

I'm having a really hard time with one of my assignments. I've been working on it for a while and haven't been able to make much progress. The assignment is to create a C++ program that has the user input an integer between 1 and 4, and then output a statement based on the number. Using a switch statement to do this is required. I have two questions.

One. I'm not really sure what I'm supposed to do here. I'm assuming I'm supposed to have the user input either 2 or 3 and give them a statement based on those two integers. If they input any other integer, they'll get an 'error' message. So that's how I'm approaching this. If I'm not reading this assignment right, please let me know.

Two. Assuming I do know what I'm doing, my goal is to get the user to see the statement "Dos" if they input a 2, and "Tres" if they input a 3. That's not what I'm getting with the code I put in. I've tried editing it, but I'm not sure what I'm doing wrong. I can't find very many examples of a problem like this in my text book, so I don't have much to work with. I need to get this finished soon, so any help would be greatly appreciated. Thanks..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main()
{
   int integers;
   cout<<"Enter an integer between 1 and 4";
   cin>>integers;
   switch (integers)
   {
   case 2:cout<<"Dos"<<endl;
   case 3:cout<<"Tres"<<endl;
   default:cout<<"error"<<endl;
   }
   return 0;
}
Last edited on
I didnt compile it but it must work. And I hope you want 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
33
#include <iostream>

using namespace std;

int main()
{
   int integer;
   cout << "Enter an integer between 1 and 4" << endl << endl;
  
    do
   {

       cin >> integer;
   
       switch (integer)
       {
           case 2: 
                  cout << "Dos" << endl;
                  break;

           case 3:
                  cout << "Tres" << endl; 
                  break;

           default: 
                  cout << "error try again:";
                  break;
       }

    } while ( integer != 2 & integer != 3 );

   return 0;
}
Last edited on
I'm not sure to have translated the text well ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


   int integers;
   cout<<"Enter an integer between 1 and 4";
   cin>>integers;
   switch (integers)
   {
   case 2:
             cout<<"Dos"<<endl;
             break;
   case 3:
             cout<<"Tres"<<endl;
             break;
   default:
             cout<<"error"<<endl;
   }
   return 0;




EDIT : you can also enter a loop ..
Last edited on
i.e. you need to use break; to end a switch case.

The leakiness of the cases does allow you to perform the same action for multiple cases, e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main()
{
   int integers;
   cout<<"Enter an integer between 1 and 8";
   cin>>integers;
   switch (integers)
   {
   case 2:
   case 4:
   case 6:
      cout<<"even"<<endl;
      break;
   case 3:
   case 5:
   case 7:
       cout<<"odd"<<endl;
       break;
   default:
       cout<<"error"<<endl;
   }
   return 0;
}


or to chain them

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 integers;
   cout<<"Enter an integer between 1 and 4";
   cin>>integers;
   switch (integers)
   {
   case 4:cout<<"Cuatro"<<endl; // fall though
   case 3:cout<<"Tres"<<endl;   // fall though
   case 2:cout<<"Dos"<<endl;    // fall though
   case 1:cout<<"Uno"<<endl;
      cout<<"Cero - ¬°Despegamos!"<<endl;
      break;
   default:cout<<"error"<<endl;
   }
   return 0;
}


Andy
Is this what you want:

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

int main()
{
     int integers;
     std::cout << "Type in a number between 1 - 4: ";
     std::cin >> integers;
     
     switch (integers) {
            case 1:
                 std::cout << "Uno" << std::endl;
                 break;
            case 2:
                 std::cout << "Dos" << std::endl;
                 break;
            case 3:
                 std::cout << "Tres" << std::endl;
                 break;
            case 4:
                 std::cout << "Cuatro" << std::endl;
                 break;
            default:
                 std::cout << "Error, number between 1 and 4 please!" << std::endl;
     }
     
     return 0;
}


Last edited on
Thanks for the help, guys. I really appreciate it..
Topic archived. No new replies allowed.