(Total Beginner) Unsure why goto does not work in program

Viewed 68

Hey so I only started C++ 2 days ago, so please try to keep the answer simple, otherwise probably won't understand it, thanks!

I tried to make a basic program where a program asks for a word, then counts the letters in said word. It then tells asks if you want to know the letter in any given position of the letter. Im making this because I thought I would learn better if I just tried making something basic rather than endlessly watching videos on it.

I ran into a problem with the asking the user part of the code. I want to have it check whether the user typed Y or N, and if neither, repeat asking until either Y or N is inputted. I tried using goto, but I could not get it to work despite checking online tutorials on how it should work. If anyone could help that would be greatly appreciated :).

When run, it does the following: enter image description here

The code is below, thank you for reading:

#include <string>

using namespace std;
int main(){
    string text; //variable for the word to be measured
    int letter; //variable for the placement of the letter in word
    string confirmation; //variable for Y or N
    cout << "This program counts letters in a word \n\n";
    cout << "   Please type a word for me to count: \n\n";
    cin >> text;
    cout << "\nYour word has " << text.length() << " letter";
    if (text.length()>1){
        cout << "s"; // Checks whether to put an s at the end of the prev. sentence for a plural or not
    }
    cout << "\n\nThis is what you typed by the way: " << text << "\n\n";
    cout << "Would you like me to find the letter in any given position in the word? \n\n   If yes, type Y. If no, type N: \n\n";
    cin >> confirmation;
    check: 
        if (confirmation == "Y"){ //Loops until one of these are fulfilled
            cout << "What position's letter would you like me to find? \n\n";
            cin >> letter;
            cout << "\n" << text[letter-1] << "\n\n";
            cout << "Thanks for using me, have a nice day";
        } else if (confirmation == "N"){
            cout << "\nAlright have a nice day";
        } else {
            goto check;
        }
    return 0;
}
1 Answers

First, goto-Syntax is something you do not need to learn as a beginner. In 99.99% there are better alternatives than using goto, so until you are very advanced, just pretend that goto does not exist in C++.

Second, the goto in your code works. It is just that if the user answers something different than "Y" or "N", your code will infinitely loop between the label check: and the goto check statement, as there is no way that confirmation can change in between.

Last, here is an example how to better do this, using a while-loop.

    cout << "Would you like me to find the letter in any given position in the word? \n\n   If yes, type Y. If no, type N: \n\n";
    cin >> confirmation;
    while (confirmation != "Y" && confirmation != "N") {
        cout << "Please answer Y or N.\n";
        cin >> confirmation;
    }
    if (confirmation == "Y"){ //Loops until one of these are fulfilled
        cout << "What position's letter would you like me to find? \n\n";
        cin >> letter;
        cout << "\n" << text[letter-1] << "\n\n";
        cout << "Thanks for using me, have a nice day";
    } else {
        cout << "\nAlright have a nice day";
    }

Related