void basic_commands()
{
if (input[0] == 's' || input[0] == 'S')
{
if ((*pl).race == "Warrior")
{
warrior_statstable();
}
else if ((*pl).race == "Mage")
{
mage_statstable();
}
else if ((*pl).race == "Cleric")
{
cleric_statstable();
}
}
else if (input[0] == 'i' || input[0] == 'I')
{
display_inventory();
}
cout <<"Now what?\n";
}
cin>>input;
while (input[0] != 'c' || input[0] != 'C')
{
if (input[0] == 'c' || input[0] == 'C')
cout << "You continue your adventure...";
basic_commands();
cin >>input;
}
cout <<"You continue your adventure. . .";
loopy troubles
this is probably a newb question but im makin an rpg and i created a basic commands for when the player isnt in battle so they can check inventory/stats, but i cant seem to get the game to continue when the player types continue. . . it just keeps up with the loop. C++ of course. thanks for hearin my newb problem! any suggestions are appreciated! thnx again
while (input[0] != 'c' || input[0] != 'C')
Take a look at this line. Let's suppose that input[0] == 'c'. Then, input[0] != 'C'! Or, if input[0] == 'C', then input[0] != 'c'! Or, if input[0] == <insert any character here>, then this while loop evaluates to TRUE since input[0] NEVER equals two things at once ;-)
Hence, I think you meant:
while (input[0] == 'c' || input[0] == 'C')
By the way, I should also mention that one way to avoid getting into these troubles is to use the toupper() command, for instance, while (toupper(input[0]) == 'C'), or while (toupper(input[0]) != 'C').
Take a look at this line. Let's suppose that input[0] == 'c'. Then, input[0] != 'C'! Or, if input[0] == 'C', then input[0] != 'c'! Or, if input[0] == <insert any character here>, then this while loop evaluates to TRUE since input[0] NEVER equals two things at once ;-)
Hence, I think you meant:
while (input[0] == 'c' || input[0] == 'C')
By the way, I should also mention that one way to avoid getting into these troubles is to use the toupper() command, for instance, while (toupper(input[0]) == 'C'), or while (toupper(input[0]) != 'C').
hmmmmm, im not quite catching what your throwing at me. if i set it to (input[0] == 'c' || input[0] == 'C') then it just tells me "you continue your adventure"
Quote:Original post by metal_kid43
hmmmmm, im not quite catching what your throwing at me. if i set it to (input[0] == 'c' || input[0] == 'C') then it just tells me "you continue your adventure"
while (input[0] != 'c' || input[0] != 'C')
Sorry, my first post was confusing and in fact incorrect. I think what we need to see here is that the C language uses short-circuit evaluation. Since you have an OR (||) there, then IF input[0] != 'c' (even if it == 'C'), then the while() will evaluate to TRUE. Hence, if you change the statement to while (input[0] != 'c' && input[0] != 'C'), then BOTH conditions must be true for the while() loop to repeat.
In English, this means "Repeat while the user does not enter 'c' and while he does not enter 'C'". Your current while statement says "If the user did not enter 'c', then repeat. Otherwise, if he did not enter 'C', then repeat". Sorry about that, the first time through I didn't look closely enough!
Try:
while (input[0] != 'c' && input[0] != 'C')
{
/* other code */
}
OR...as suggest by mnansgar:
while (toupper(input[0]) != 'C')
{
/* other stuff */
}
toupper() converts the char to it's uppercase equiv...I think tolower() also exists.
while (input[0] != 'c' && input[0] != 'C')
{
/* other code */
}
OR...as suggest by mnansgar:
while (toupper(input[0]) != 'C')
{
/* other stuff */
}
toupper() converts the char to it's uppercase equiv...I think tolower() also exists.
first off, if you're using C++, use the std:: namespace for cin/cout and you might know this already but i'll say it again, good consistent indentation is a must... so here's your code "patched":
now, onto the issues in your code. In basic_command you compare (*pl).race to a string literal using the == operator, is .race an std::string? if not, that test will never-ever return true.
also as i see it, your while loop will break before it gets to act on the user inputting 'c' so i would re-implement your while loop like this:
Cheers
-Danu
#include <iostream>#include <ctype.h>void basic_commands(){ if( tolower(input[0]) = 's' ) { if ((*pl).race == "Warrior") { warrior_statstable(); } else if ((*pl).race == "Mage") { mage_statstable(); } else if ((*pl).race == "Cleric") { cleric_statstable(); } } else if( tolower(input[0]) == 'i' ) { display_inventory(); } std::cout << "Now what?" std::endl; } std::cin >> input;while ( tolower(input[0]) != 'c' ){ if( tolower(input[0]) == 'c' ) std::cout << "You continue your adventure..."; basic_commands(); std::cin >> input; }std::cout << "You continue your adventure. . .";
now, onto the issues in your code. In basic_command you compare (*pl).race to a string literal using the == operator, is .race an std::string? if not, that test will never-ever return true.
also as i see it, your while loop will break before it gets to act on the user inputting 'c' so i would re-implement your while loop like this:
do{ std::cin >> input; if( tolower(input[0]) == 'c' ) std::cout << "You continue your adventure..."; basic_commands(); }while ( tolower(input[0]) != 'c' )
Cheers
-Danu
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement