-1
i=0;
while(i<=20)
{
    nullchoice:
    system("CLS");
    //Advanced Information
    cout << "Your Statistics: " << endl;
    cout << endl << endl;
    cout << "   1  Strength: " << str << endl;
    cout << endl;
    cout << "   2  Vitality: " << vit << endl;
    cout << endl;
    cout << "   3  Agility: " << agi << endl;
    cout << endl;
    cout << "   4  Thaumaturgy: " << tha << endl;
    cout << endl << endl;
    cout << "Points Remaining: " << lvlskills << endl;
    cout << endl;
    cout << "Enter the number of the skill you wish to increase: ";

    //Applying points to skill attributes

    if(i<=19)
    {
          cin >> input;

          if(input==1)
                str+=1;
          else if(input==2)
               vit+=1;
          else if(input==3)
               agi+=1;
          else if(input==4)
               tha+=1;
          else
               goto nullchoice;

         lvlskills-=1; 
    }    
    i++;
    cout << endl << endl;                     
}

So essentially, I am creating a game in C++, a text-based RPG. It's fairly basic, with stats (Strength, Vitality, etc.) you might expect from such a game. In the beginning, as shown here, the player is allowed to distribute some points to skills they choose.

Here's where the problem arises. Right now, the player must enter a number (1, 2, 3, or 4. If it's none of these numbers it will goto the nullchoice), then press ENTER. It's unwieldy and just plain wrong to have the player do this, so is there any simple way that I can code it so they only have to press the number?

I'd imagine I would use this very much throughout the rest of my game. Thanks for reading!

1 Answers1

0

You just need to store the input and compare it to the required case. Put a think on it...

do
{
    system("CLS");
    //Advanced Information
    cout << "Your Statistics: " << endl;
    cout << endl << endl;
    cout << "   1  Strength: " << str << endl;
    cout << endl;
    cout << "   2  Vitality: " << vit << endl;
    cout << endl;
    cout << "   3  Agility: " << agi << endl;
    cout << endl;
    cout << "   4  Thaumaturgy: " << tha << endl;
    cout << endl << endl;
    cout << "Points Remaining: " << lvlskills << endl;
    cout << endl;
    cout << "Enter the number of the skill you wish to increase: ";

    //Applying points to skill attributes
          char input;
          switch(input=getch()){
              case '1':
                str+=1;
                lvlskills-=1;
                break;
          case '2':
               vit+=1;
               lvlskills-=1;
               break;
          case '3':
               agi+=1;
               lvlskills-=1;
               break;
          case '4':
               tha+=1;
               lvlskills-=1;
         } 
    cout << endl << endl;                     
} while(lvlskills>0);

Avoid goto in your code while you can solve it by other options. It's a good practice..

deb_rider
  • 570
  • 2
  • 12