English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

//Lesson 3 (Homework 2)

#include
using namespace std;

int main()
{
int choice = 0;

cout<<"Enter a letter from A to D: "; cin>>choice;
cout<
if(choice>='E')
{
cout<<"You've enter an invaild letter"< cout< return 0;
}
if(choice == 'A')
{
cout<<"You've entered letter A"< cout<<"A for Apple"< cout< return 0;
}
if(choice == 'B')
{
cout<<"You've entered letter B"< cout<<"B for Boy"< cout< return 0;
}
if(choice == 'C')
{
cout<<"You've entered letter C"< cout<<"C for Cat"< cout< return 0;
}
if(choice == 'D')
{
cout<<"You've entered letter D"< cout<<"D for Dog"< cout< return 0;
}
cout<<"You did not enter a letter";
cout< return 0;
}

2007-01-18 17:21:34 · 5 answers · asked by capphire 2 in Computers & Internet Programming & Design

5 answers

#include
using namespace std;

int main()
{
char choice = '\0';

cout<<"Enter a letter from A to D: "; cin>>choice;
cout<
if(choice>='E')
{
cout<<"You've enter an invaild letter"< cout< return 0;
}
if(choice == 'A')
{
cout<<"You've entered letter A"< cout<<"A for Apple"< cout< return 0;
}
if(choice == 'B')
{
cout<<"You've entered letter B"< cout<<"B for Boy"< cout< return 0;
}
if(choice == 'C')
{
cout<<"You've entered letter C"< cout<<"C for Cat"< cout< return 0;
}
if(choice == 'D')
{
cout<<"You've entered letter D"< cout<<"D for Dog"< cout< return 0;
}
cout<<"You did not enter a letter";
cout< return 0;
}




your problem is that you were declaring choice as int so cin was expecting a number now B is not a number.

2007-01-18 17:28:42 · answer #1 · answered by iammisc 5 · 0 0

Are you entering an upper case "B" or a lower case "b"? The lower case letters come after the upper case ones in the ASCII table, so your check for whether or not the character is greater than 'E' is true for all lower case letters.

iammisc, in C a char is usally the same as an unsigned int, with the value being the character's ASCII value.

2007-01-18 17:37:08 · answer #2 · answered by watsonc64 3 · 1 0

change your first "if" statement to: "if (choice == 'A' || choice =='a')", and make the rest "else if" statements, checking for cap and small letters . Then at the end make an "else" statement to catch invalid entries. OR you can use the
"toupper() " (i think thats what it is) function to convert all characters to upper case first. Then you just look for the upper case letters.

2007-01-19 01:21:02 · answer #3 · answered by justme 7 · 0 0

"choice" is an integer (0-9) but you entered A-D (non integers).

Declare it as a non-integer.

2007-01-18 22:37:06 · answer #4 · answered by TonyB 6 · 0 1

Maybe you have dust in your keyboard or a virus in your computer. Have you tried to reboot your PC?
Give it a go, what have you got to lose.

2007-01-18 17:29:36 · answer #5 · answered by Venessa M 4 · 0 2

fedest.com, questions and answers