ok. This program should display the string "Dog" when the animal variable catains the letter D (in any case); otherwise, it should display the string "Cat".
However, when i run it, it only displays the string "Cat" no matter what letter i type in it. WHY? this is my code:
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
//declare variable
char animal = ' ';
string ID = "";
//enter input
cout %26lt;%26lt; "Enter an animal ID: ";
cin %26gt;%26gt; ID;
//display ID
if (animal == 'D' || animal == 'd')
cout %26lt;%26lt; "Dog" %26lt;%26lt; endl;
else
cout %26lt;%26lt; "Cat" %26lt;%26lt; endl;
//end if
return 0;
} //end of main function
Can someone please check my C++ program? please!!!?
You never put anything in animal?
Your if needs to be checking ID == D or ID == d not animal. where does animal get its value? So right now animal doesn't have anything in it, and thus null ==D is false and the else statement is fired.
Reply:I agree with the ones above that say animal is still empty.
You can try printing it out to verify:
I think you are trying to check the first letter of ID, but you never copy it to your animal variable. Add the following line after your cin line:
animal = ( ID . c_str ( ) ) [0] ;
This copies the char to animal.
Reply:not if animal
use ID == 'D' || ID == 'd'
Reply:You don't need ID (and you can't test it's value using == anyway). Change:
cin %26gt;%26gt; ID;
to:
cin %26gt;%26gt; animal;
Reply:It seems to me you have your input set to the variable ID and then your if statement is using the variable animal which you have never assigned a value to.
So since animal = "" then the else case is always true and you always get cat.
Change the line cin %26gt;%26gt; ID to cin %26gt;%26gt; animal
Reply://Display ID section...
you;re using the variable ID to hold what letter they enter...
then at the point where you are going to display the result you're checking the variable animal not ID
change that to:
if (ID == 'D' || ID == 'd')
nobile
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment