Jump to content
  • Advertisement
Sign in to follow this  
metal_kid43

loopy troubles

This topic is 4884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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. . .";

Share this post


Link to post
Share on other sites
Advertisement
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').

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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":


#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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!