简体   繁体   中英

error: variable was not declared in this scope c++

I want to write the programm, that takes two numbers m and n as input and gives n-th digit of m number. Example m=1358 n=2 Output: 5

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

int main(){
    while(true){
        int m = 0,n = 10;
        char check;
        while(true){
            cout << "Enter please number m and which digit you want to select";
            cin >> m >> n;
            string m_new = to_string(m);
            if(m_new.length() > n)
                break;
            else
                cout << "n must be less than or equal to m";    
        }
        cout << "The position" << n << "Of integer" << m << "is:" << m_new.substr(n,1);
        cout << "Do you want to try again?(y/n)\n";
        cin >> check;
        while(check != 'y'&& check != 'n'){
            cout <<"Please enter y or n\n";
            cin >> check;
        }
        if(check == 'n'){
            break;
        }
    }
    return 0;
}

But i got an error: 'm_new' was not declared in this scope. Why do I get this error and how to fix it?

The m_new variable is local to nested while loop and can't be used outside of its scope. Scope represented by braces {} determines visibility:

int main() {
    // can't be used here
    while (true) {
        // can't be used here
        while (true) {
            string m_new = to_string(m);
            // can only be used here
        }
        // can't be used here
        while (check != 'y'&& check != 'n') {
            // can't be used here
        }
        // can't be used here
    }
    // can't be used here
}

Rethink the design to use only one while loop:

int main(){
    char check = 'y';
    while (std::cin && choice == 'y') {
        int m = 0, n = 10;
        std::cout << "Enter please number m and which digit you want to select";
        std::cin >> m >> n;
        string m_new = to_string(m);
        // the rest of your code
        std::cout << "Please enter y or n\n";
        std::cin >> check;
    }
}

Now the m_new variable is visible to everything inside the while loop.

m_new is declared inside the while loop. Anything declared inside a {...} block will only exist inside that block. The final use of it:

    cout << "The position" << n << "Of integer" << m << "is:" << m_new.substr(n,1);

is outside the block, and therefore the variable no longer exists.

The variable "m_new" was just in the "while(true)" scope, and when your're asking this variable out of loop, it throws a compile-time error.

while(true){
    ...
    string m_new = to_string(m);
    ...
}
...
cout << "The position" << n << "Of integer" << m << "is:" << m_new.substr(n,1);
                                                             ^
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM