0

I am working with some C++ code. I have a while-loop set up to allow me to run through some code x-number of times. The while loop terminates once the user indicates that they do not want to run through the code again.

#include <iostream>
#include <string>
using namespace std;

char request;

int main() {    
    while (request != 'N')
    {
        string getCode = "";
        while (getCode.length() != 3)
        {
            cout << "Please enter your container's region code (A or B followed by two-number identification)" << endl;
            getline(cin, getCode);

            if (getCode.length() != 3)
            {
                cout << "Error" << endl;
            }
        }

        //clear the screen
        system("cls");

        //get letter

        if (getCode.at(0) == 'A' || getCode.at(0) == 'B')
        {
            if ((getCode.at(1) >= '0' && getCode.at(1) <= '9') && (getCode.at(2) >= '0' && getCode.at(2) <= '9'))
            {
                if (getCode.at(0) == 'A')
                {
                    cout << "The shipping charge is $25" << endl;
                }
                else if (getCode.at(0) == 'B')
                {
                    cout << "The shipping charge is $30" << endl;
                }
            }
            else
            {
                cout << "Error" << endl;
            }
        }
        else
        {
            cout << "Error...Please enter the code as A or B followed by two numbers" << endl;
        }

        //Again?
        cout << "Would you like to enter in another shipping identification number?" << endl;
        cin >> request;
    } 

    cout << "Thank you" << endl;
    //End Program
    system("pause");
    return 0;
}

When I indicated that yes (entering 'Y' to the 'Would you like to enter in another shipping identification number question') I would like to run through the code again, the program outputs an unwanted 'Please enter your container's region code (A or B followed by two-number identification' and 'error' statement. Also please note, the code is inside 'int main()' and that I have properly formatted my 'include' statements.

  • 1
    It runs "Please enter you're container's region code" sounds like expected behaviour. What else did you want? – SOFe Aug 11 '19 at 01:37
  • SOFe when I indicate 'Y' to the prompt 'Would you like to enter in another shipping identification number?', it outputs the following: 'Please enter your container's region code (A or B followed by two-number identification)' 'error' 'Please enter your container's region code (A or B followed by two-number identification' . When I input 'Y' I only want it to output 'Please enter your container's region code (A or B followed by two-number identification)'...I only want it to output once –  Aug 11 '19 at 01:42
  • @bvin444 What would you like to happen differently? –  Aug 11 '19 at 01:44
  • I got you now. See https://stackoverflow.com/a/25476169/10957435 –  Aug 11 '19 at 01:46
  • 1
    This code can't possibly compile. `request` is an undeclared identifier. – Igor Tandetnik Aug 11 '19 at 01:46
  • Igor Tandetnik, you're right. I am not sure why it didn't copy over correctly. Before int main() there should be a 'char request;' –  Aug 11 '19 at 01:49
  • @bvin444 Note: `int main()` was missing too. I added it in. –  Aug 11 '19 at 01:50
  • Chipster...yeah I was having difficulty copying my code over to StackOverflow so I just left that part out. My bad. –  Aug 11 '19 at 01:51

2 Answers2

0

Your question is to understand why this is happening, so here's the explanation. The code you wrote states thusly:

    string getCode = "";
    while (getCode.length() != 3)
    {
        cout << "Please enter your container's region code...

As you see, getCode is always initialized to an empty string. Immediately afterwards, if its length is not 3, this question is outputted.

You need to understand that your computer will always do exactly what you tell it to do. Your computer will not do what you want it to do, but only what you tell it to do. The above is what you told your computer to do, and your computer will always obediently follow its strict instructions, every time it runs this code. That's pretty much the explanation, and there's nothing more to understand.

This section of code is inside another loop, and you indicated that you do not wish the prompt to appear on second and subsequent iteration of the loop, only on the initial one.

However, there's nothing in your instructions to your computer, above, that specify this. You didn't tell your computer that this is what it should do, so why do you expect your computer to do that, entirely on its own? Every time your computer executes these statements shown above, this is exactly what will happen. Nothing more, nothing less. Whether it's the first time inside the outer while loop, or on each subsequent time the while loop iterates, it doesn't matter. The code always does exactly the same thing: getCode gets created and set to an empty string, and because its length is not 3, the inner while loop runs, prints the prompt and calls std::getline to read a line of text from std::cin. At the end of your while loop, if your instructions to your computer indicate that it should run the code in the while loop again, from the beginning (because that's what the while loop does), then the above instructions get executed.

If you now understand why your computer does this (because that's what you told it to do), then you should easily figure out what to tell your computer so it doesn't do this. If you want your computer to print the prompt only the first time it executes the while loop, then this is exactly what you need to tell your computer: set a flag before the while loop, print the prompt only if the flag is set (with all other existing logic remaining the same), and then clear this flag afterwards, so the next time the while loop runs, your computer will do exactly what you told it to do, and not print the prompt.

Sam Varshavchik
  • 84,126
  • 5
  • 57
  • 106
0

when I indicate 'Y' to the prompt 'Would you like to enter in another shipping identification number?', it outputs the following: 'Please enter your container's region code (A or B followed by two-number identification)' 'error' 'Please enter your container's region code (A or B followed by two-number identification' . When I input 'Y' I only want it to output 'Please enter your container's region code (A or B followed by two-number identification)'...I only want it to output once

Now that I understand your question, what's happening is an newline (\n) is getting added to the std::cin buffer at these lines right here:

    //Again?
    cout << "Would you like to enter in another shipping identification number?" << endl;
    cin >> request;

This makes even more sense especially when combined with your other comment:

Before int main() there should be a 'char request;

So request a single char. That means when you type something like this:

Y

The newline is added to std::cin as well. That can't be stored in a single char, and the >> may not remove it either. That means it's just sitting here.

What this does is when you get to your if statement at the beginning of the loop again:

    while (request != 'N')
    {
        string getCode = "";
        while (getCode.length() != 3)
        {
            cout << "Please enter your container's region code (A or B followed by two-number identification)" << endl;
            getline(cin, getCode);

            if (getCode.length() != 3)
            {
                cout << "Error" << endl;
            }
        }

getline() sees the newline you added previously and instantly returns an empty string. Empty strings have a length of 0, so it fails your if statement, which prints the error.

The solution is simple, just tell std::cin to ignore the newline:

    //Again?
    cout << "Would you like to enter in another shipping identification number?" << endl;
    cin >> request;
    cin.ignore(1, '\n');