Getting an error at the while statement (line 40) where 'input is not declared in this scope' c++

Viewed 68

Not sure why the while loop is not working although I think I specified what input was previously in the code.

#include <iostream>

using namespace std;

int main()
{
    do
    {
        cout<<"1. \tOpen File"<<endl;
        cout<<"2. \tClose File"<<endl;
        cout<<"3. \tExit"<<endl;

        int input;
        cin >> input;

        cout<<"Select and option:   "<<endl;

        switch(input)
        {
        case 1:
            cout<<"Opening File"<<endl;
            break;
        case 2:
            cout<<"Closing File"<<endl;
            break;
        case 3:
            cout<<"Exiting..."<<endl;
            break;
        default:
            cout<<"Invalid Entry"<<endl;
        }
    }
    while (input < 1 || input > 3);

    return 0;
}

I expect the program to ask for my input and if a type a wrong input the program would repeat

1 Answers

input is localised in the loop body, so it cannot be used in the stopping conditional expression.

Declare it outside the loop, i.e. before do.

Related