0

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.

6
  • 2
    You use goto ? Commented Nov 16, 2020 at 18:27
  • 2
    Search the internet for "C++ spaghetti code goto". Commented Nov 16, 2020 at 18:27
  • 3
    if you have a problem with goto the solution is to not use it. Seriously, valid use cases of goto are rare, and your code isn't one Commented Nov 16, 2020 at 18:29
  • 2
    I don't want to channel Reddit here, and I promise I'm not saying this to be mean, but admitting that you use goto with that username is asking for people to say "username checks out!" NEVER use goto unless you can justify its use because nothing else will work. Commented Nov 16, 2020 at 18:36
  • 1
    FYI, rather than using numbers for "Yes" or "No", the C++ language has some other modern data types like bool, char and string. The numbers were used in ancient time when languages didn't have character variables. Time to modernize your design and coding style. Commented Nov 16, 2020 at 18:40

2 Answers 2

2

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 gotos.

Sign up to request clarification or add additional context in comments.

2 Comments

I have now tried both options but neither of them have worked-I have got rid of goto's but the problem is still there
If my answer is useful, please click on the checkmark. You may want to use a debugger or post a new question.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.