This question already has an answer here:
- Reading string with spaces in c++ 3 answers
Ok, so i got this mini character creation and it has a loop incase the player wants to change anything. Also, I got one extra if statement here in case the player decides to write nonsense inside the loop but it doesn't work if i write something that has a space in it.. i have tried using char but then any answer i write inside the loop does the same error as the one i am asking about right now
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdlib.h> //for using the function sleep
#include <Windows.h>
using namespace std;
struct player //begin player object definition
{
string name;
int age;
string race;
};
int main()
{
player o; //created a player object named o
printf("Hello, welcome to Alfheim Online!\n");
printf("Please put in your Name!\n");
cin >> o.name; //input name
printf("Great, now please put in your age!\n ");
cin >> o.age; //input age
printf("Please choose a race\n");
printf(" Cait Sith\n Gnome\n Imp\n Leprechaun\n Pooka\n Salamander\n Spriggan\n Sylph\n Undine\n");
cin >> o.race; //input race
printf("Name=%s\nage=%d\nrace=%s\nIs that correct?\n", o.name.c_str(), o.age, o.race.c_str());
//outputs character data
string answer; //this is if player wants to change anything
cin >> answer;
while (answer == "no"||answer == "No") //heres a loop if player wants to change something
{
printf("What would you like to change?\n");
string change; //declared string variable change
cin >> change; //person says what he wants to change here
if (change == "race" || change == "Race")
{
printf("Please choose a race\n");
printf(" Cait Sith\n Gnome\n Imp\n Leprechaun\n Pooka\n Salamander\n Spriggan\n Sylph\n Undine\n");
cin >> o.race; //input race
}
else if (change == "name" || change == "Name")
{
printf("Please put in your Name!\n");
cin >> o.name; //input name
}
else if (change == "age" || change == "Age")
{
printf("Please put in your Age!\n");
cin >> o.age; //input age
}
else printf("command unrecognized\n"); //!!this is if you write some nonsense...
//but it doesnt work if i wire something with space? it writes the printf below but ignores cin below and just continues out of the loop?
printf("Name=%s\nage=%d\nrace%s\nIs that correct?\n", o.name.c_str(), o.age, o.race.c_str());
cin >> answer;
}
printf("Generating character appearance\n");
Sleep(1000);
printf("Generating character appearance.\n");
Sleep(1000);
printf("Generating character appearance..\n");
Sleep(1000);
printf("Generating character appearance...\n");
Sleep(1000);
printf("Generating character appearance....\n");
Sleep(1000);
printf("Character creation complete!\n");
Sleep(3000);
printf("Prepare for your adventure!\n");
std::cin.ignore();
}
Aucun commentaire:
Enregistrer un commentaire