Reputation: 5
I am writing a program that lets the user make their own person (as an object) with a series of user inputs and multiple choice. I have a confirmation system at the end using the goto function but when i go back to making making the person if they don't like it the user input for the fist name won't work but the rest do (the first name user input works the first time round perfectly fine)
Here is the code and an explanation:
Setting things up,making the class with a basic constructor function added on.
#include <iostream>
using namespace std;
class Person{
public:
string FirstName;
string Surname;
string Gender;
int Age;
double Money;
Person(string aFirstName, string aSurname, string aGender, int aAge, double aMoney){
FirstName = aFirstName;
Surname = aSurname;
Gender = aGender;
Age = aAge;
Money = aMoney;
}
};
Making the variables that will be put in the person at the end and getting them with user inputs and multiple choice:
int main(){
Error2:
string bFirstName;
string bSurname;
string bGender;
int bAge;
int Choice1;
int YesNo;
cout << "What is your character's First Name?" <<endl;
getline(cin, bFirstName);
cout << "What is your character's Surname?" <<endl;
getline(cin, bSurname);
Error1:
cout << "What is your character's Gender?" <<endl;
cout << "Press 1 for Male" <<endl;
cout << "Press 2 for Female" <<endl;
cout << "Press 3 for Other" <<endl;
cin >> Choice1;
switch(Choice1){
case 1:
bGender = "Male";
break;
case 2:
bGender = "Female";
break;
case 3:
cout << "Type in Gender." <<endl;
cin >> bGender;
break;
default:
cout << "Enter a valid choice next time" <<endl;
goto Error1;
break;
}
cout << "What is your character's Age?" <<endl;
cin >> bAge;
The confirmation system using goto functions so they can scrap the one they made and make a new one:
Error3:
cout << "Are you sure you want your character to have these attributes?" <<endl;
cout << "1 for yes, 2 for no" <<endl;
cout << "FirstName: " << bFirstName <<endl;
cout << "Surname: " << bSurname <<endl;
cout << "Gender: " << bGender <<endl;
cout << "Age: " << bAge <<endl;
cin >> YesNo;
if(YesNo == 1){
goto Error4;
} else if(YesNo == 2){
goto Error2;
} else{
goto Error3;
}
Error4:
Person Custom1(bFirstName, bSurname, bGender, bAge, 100);
return 0;
}
but if in the confirmation system I say I want to make a new one the goto function will work but asking for the first name will not work and it will immediately go to asking for the surname and at the end when it asks if i'm fine with the attributes first name will be empty.
Upvotes: 0
Views: 243
Reputation: 5
I have solved the problem-I have got rid of goto's but that wasn't the problem. I made YesNo a string and then made Error correction a do while loop and at the end I had to ask-Using getline-for the input of YesNo twice in the code but it only asked once when running I still have no idea what the problem was but I have fixed it.
Upvotes: 0
Reputation: 57688
I recommend using loops for error correction and functionality continuations:
bool continue_program = true;
while (continue_program)
{
//...
std::cout << "Continue program (Y/N)?";
char response;
std::cin >> response;
response = tolower(response);
if (response != 'y') continue_program = false;
}
You can also use do-while
for error correction:
int value = 25;
do
{
std::cout << "Enter the number 5: ";
std::cin >> value;
} while (value != 5);
This should eliminate a lot of your goto
s.
Upvotes: 2