Friday, July 31, 2009

LITTLE C++ PROBLEM plz help :D?

for some reason it repeats the attack function 3 times


any ideas?








#include %26lt;cstdlib%26gt;


#include %26lt;iostream%26gt;





using namespace std;





int main(void)


{


int dayhealth = 100;


int plhealth = 50;


string name;


char answer;


int attack = 10;





cout %26lt;%26lt;"gahhh?" %26lt;%26lt; endl;


cin %26gt;%26gt; name;





cout %26lt;%26lt; name %26lt;%26lt; endl;





cout%26lt;%26lt; "player" %26lt;%26lt; plhealth %26lt;%26lt; "\n\nday" %26lt;%26lt; dayhealth %26lt;%26lt; endl;








do


{


cout%26lt;%26lt; "atk?" %26lt;%26lt; endl;


cin%26gt;%26gt; answer;





if (answer = 'yes')


{


cout %26lt;%26lt; "gahhh" %26lt;%26lt; endl;





dayhealth = dayhealth - attack;





cout %26lt;%26lt; dayhealth %26lt;%26lt; endl;


}


}while ( dayhealth != 0 );











system("PAUSE");


return 0;


}

LITTLE C++ PROBLEM plz help :D?
You need to change the following two lines:





char answer;


changes to


string answer;





if (answer = 'yes')


changes to


if (answer == "yes")





What you are doing is setting answer = 'yes' currently, which returns true for each char (letter), hence returning 3 times. By using == you perform a comparison, and since char is only 1 character, as opposed to string which is many characters, the comparison works correctly only on a "yes" answer. Feel free to give it a shot. I compiled and tested it and everything worked after the above changes.

hibiscus

No comments:

Post a Comment