//Lesson 3 (Homework 2)
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
int choice = 0;
cout%26lt;%26lt;"Enter a letter from A to D: "; cin%26gt;%26gt;choice;
cout%26lt;%26lt;endl;
if(choice%26gt;='E')
{
cout%26lt;%26lt;"You've enter an invaild letter"%26lt;%26lt;endl;
cout%26lt;%26lt;endl;
return 0;
}
if(choice == 'A')
{
cout%26lt;%26lt;"You've entered letter A"%26lt;%26lt;endl;
cout%26lt;%26lt;"A for Apple"%26lt;%26lt;endl;
cout%26lt;%26lt;endl;
return 0;
}
if(choice == 'B')
{
cout%26lt;%26lt;"You've entered letter B"%26lt;%26lt;endl;
cout%26lt;%26lt;"B for Boy"%26lt;%26lt;endl;
cout%26lt;%26lt;endl;
return 0;
}
if(choice == 'C')
{
cout%26lt;%26lt;"You've entered letter C"%26lt;%26lt;endl;
cout%26lt;%26lt;"C for Cat"%26lt;%26lt;endl;
cout%26lt;%26lt;endl;
return 0;
}
if(choice == 'D')
{
cout%26lt;%26lt;"You've entered letter D"%26lt;%26lt;endl;
cout%26lt;%26lt;"D for Dog"%26lt;%26lt;endl;
cout%26lt;%26lt;endl;
return 0;
}
cout%26lt;%26lt;"You did not enter a letter";
cout%26lt;%26lt;endl;
return 0;
}
What's wrong?? i enter B but it say invalid !!!!?
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
char choice = '\0';
cout%26lt;%26lt;"Enter a letter from A to D: "; cin%26gt;%26gt;choice;
cout%26lt;%26lt;endl;
if(choice%26gt;='E')
{
cout%26lt;%26lt;"You've enter an invaild letter"%26lt;%26lt;endl;
cout%26lt;%26lt;endl;
return 0;
}
if(choice == 'A')
{
cout%26lt;%26lt;"You've entered letter A"%26lt;%26lt;endl;
cout%26lt;%26lt;"A for Apple"%26lt;%26lt;endl;
cout%26lt;%26lt;endl;
return 0;
}
if(choice == 'B')
{
cout%26lt;%26lt;"You've entered letter B"%26lt;%26lt;endl;
cout%26lt;%26lt;"B for Boy"%26lt;%26lt;endl;
cout%26lt;%26lt;endl;
return 0;
}
if(choice == 'C')
{
cout%26lt;%26lt;"You've entered letter C"%26lt;%26lt;endl;
cout%26lt;%26lt;"C for Cat"%26lt;%26lt;endl;
cout%26lt;%26lt;endl;
return 0;
}
if(choice == 'D')
{
cout%26lt;%26lt;"You've entered letter D"%26lt;%26lt;endl;
cout%26lt;%26lt;"D for Dog"%26lt;%26lt;endl;
cout%26lt;%26lt;endl;
return 0;
}
cout%26lt;%26lt;"You did not enter a letter";
cout%26lt;%26lt;endl;
return 0;
}
your problem is that you were declaring choice as int so cin was expecting a number now B is not a number.
Reply:small letters will still be considered invalid. Report It
Reply: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.
Reply:"choice" is an integer (0-9) but you entered A-D (non integers).
Declare it as a non-integer.
Reply: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.
Reply: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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment