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