-5

I'm very, very new to coding. Thought I'd start with C++, which I've been writing for about 3 days. In order to teach myself, I've been using a mix of Sololearn, Hackerrank and writing a Text-Based Adventure Game.

Herein lies my issue.

I'm in the process of writing start-scenarios for the 3 playables in my project. These are within separate if statements, with any decisions within those being nested if statements. To include character health, all of this is within a single while loop, which is true while Health >= 1.

However, I'm having a strange issue where, when selecting one of the 'options' (nested if statements below 'Orc'), when ran, it outputs more than one option, then repeats all text from character creation, up to that point, once.

This only happens when the if statement is a string. If I change the code so that there is no string for "Item", and set "Bucket", "Rug", and "Bars" as int 1,2,3, it works as I would expect it to. Is this an issue with strings in C++?

I have researched everything I can think of, including usung getline instead of cin for numerous cin statements, in case that was causing the issue, and yet no joy.

Please find my code below. Have I missed something? Also, being almost completely new to this, is there a better way in which I could do this?

Finally, please note that only the 'Orc' segment works correctly if you feel the need to run it to get a better idea of the issue that I'm having - 'Human' and 'Elf' currently create infinite loops because I haven't written far enough into them to break the while loop yet.

Thanks in advance for your help!

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;


int main()
{
//Name Entry
string Name;
cout << "Enter Name: " << endl;
getline (cin, Name);

//Age Entry
int Age;
cout << "Enter Age: " << endl;
cin >> Age;

//Race Entry
string Race;
cout << "Enter Race: " << endl;
cin.ignore();
getline (cin, Race);

//Stats
int Strength;
int Agility;
int Speech;
int Intellect;
int Social;
int Health=1;

//Racial Starts
while (Health >= 1) {

    //Human
    if (Race == "Human" || Race == "human") {

        Strength = 10;
        Agility = 10;
        Speech = 8;
        Intellect = 7;
        Social = 20;
        Health = 100;

        cout << "Human Selected." << endl;
        cout << "Starting Game!" << endl << endl;
        cout << "You are in a finely furnished bedroom." << endl;}

    //Elf
    if (Race == "Elf" || Race == "elf") {

        Strength = 8;
        Agility = 14;
        Speech = 12;
        Intellect = 13;
        Social = 10;
        Health = 80;

        cout << "Elf Selected." << endl;
        cout << "Starting Game!" << endl << endl;
        cout << "You are cleaning a finely furnished bedroom." <<endl;}

    //Orc
    if (Race == "Orc" || Race == "orc") {

        Strength = 18;
        Agility = 5;
        Speech = 6;
        Intellect = 3;
        Social = 5;
        Health = 200;

        cout << "Orc Selected" << endl;
        cout << "Starting Game!" << endl << endl;
        cout << "You are locked in a cell." << endl << "The cell is made up of three stone walls." << endl << "There are no windows." << endl;
        cout << "There is a rug on the cobbled stone floor, and a bucket in the corner." << endl << "Iron bars face into a guard's chamber." << endl << endl;
        cout << "The guard is sleeping." << endl << "A shortsword rests in a scabbard on the table in fromt of him." << endl;

        //Actions in this room
        int Item;
        cout << "Your escape lies with either the Bucket, the Rug or the Bars." << endl << "Pick an item." << endl;

        string Item;
        cin >> Item;


        if (Item == "bucket" || Item == "Bucket") {
            cout << "You place the Bucket on your head." << endl << "The bucket is full of your own excrement." << endl << "You die of disease." << endl;
            Health = Health-1000; }

        if (Item == "Rug" || Item == "rug") {
            cout << "You lie on the rug and fall asleep." << endl << "The guard wakes up." << endl << "His irrational hatred for you causes him to draw his sword, enter your cell and sever your head." << endl;
            Health = Health-1000; }

        if (Item == "Bars" || Item == "bars") {
            cout << "You try the bars." << endl << "You realise that this cell was not built to hold an Orc." << endl << "You bend the bars easily and step out of the cell." << endl; }





    }





}

cout << "Game Over";

system("pause");
return 0;
}
Ozzy
  • 1
  • 1
  • You have all your logic in a `while` loop, so it all repeats. Use the debugger to step through your game and examine the order of execution. – xxbbcc Sep 12 '17 at 21:23
  • 4
    "I've been using a mix of Sololearn, Hackerrank and writing a Text-Based Adventure Game." Strongly consider adding [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to that mix. C++ is a very complex language with a lot of little niggly details that will bite you and be impossible to look up online unless you know the right search keywords. – user4581301 Sep 12 '17 at 21:30

1 Answers1

1

All of your logic is in the while loop right now, so it will keep repeating over and over. It will keep saying you are an Orc and restarting the adventure over again. If you want to develop this in maybe a better way, I would suggest looking up the concept of "ticks" and state machines in video games or you will keep hitting this issue. Or, think about moving your character creation logic outside of the while loop since you dont want that to keep repeating.

OmegaNalphA
  • 610
  • 1
  • 6
  • 17
  • Another issue is that with this right now, you don't actually have a check on whether or not your health is <= 1. The while loop will only perform the check *at the end of the loop* which means that you could keep playing the entire way through with negative health and it would work. Make sure you understand while loop logic and when/why you want to use it, might be helpful. – OmegaNalphA Sep 12 '17 at 21:26