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;
}