Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

visual-c++

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

like image 863
Coaly boi Avatar asked Dec 23 '25 00:12

Coaly boi


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.

like image 126
Bathsheba Avatar answered Dec 24 '25 16:12

Bathsheba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!