Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 代码块内变量的全球化_C++_Global - Fatal编程技术网

C++ 代码块内变量的全球化

C++ 代码块内变量的全球化,c++,global,C++,Global,我在代码的后面使用在第一个if语句(if(choice==1))中创建的数组/矩阵时遇到问题。我想编辑数组(当我提到if(choice==2)时),但是在第一个“if”代码块中创建的数组是本地化的。我如何避免这个问题 #include "pch.h" #include <iostream> #include <vector> int main() { if (choice == 1) { std::cout <

我在代码的后面使用在第一个if语句(if(choice==1))中创建的数组/矩阵时遇到问题。我想编辑数组(当我提到if(choice==2)时),但是在第一个“if”代码块中创建的数组是本地化的。我如何避免这个问题

#include "pch.h"
#include <iostream>
#include <vector>

int main()
{
        if (choice == 1)
        {
            std::cout << "Choose a letter from A to C, where A = 1" << std::endl;
            short matNamechoice;
            std::cin >> matNamechoice;

            if (matNamechoice == 1)
            {
                bool matAlive = true;
                std::vector<int> matrixA(0);
                std::cout << "How many elements would you like to add to Matrix" << matNamechoice << "?" << std::endl;
                short matAelements;
                std::cin >> matAelements;
                std::cout << "For the " << matAelements << "elements that you have created, please choose \
                    each element value: " << std::endl;
                for (int n = 0; n < matAelements; ++n)
                {
                    std::cout << "ELEMENT NO " << n << ":" << std::endl;
                    int elementValue = 0;
                    std::cin >> elementValue;
                    matrixA.push_back(elementValue);
                }
                std::cout << matAelements << " elements have been appended to matrixA." << std::endl;
            }


            if (matNamechoice == 2)
            {

                bool matBLive = true;
                std::vector<int> matrixB(0);
                std::cout << "How many elements would you like to add to Matrix " << matNamechoice << "?" << std::endl;
                short matBelements;
                std::cin >> matBelements;
                std::cout << "For the " << matBelements << "elements that you have created, please choose \
                        each element value: " << std::endl;
                for (int n = 0; n < matBelements; ++n)
                {
                    std::cout << "ELEMENT NO " << n << ":" << std::endl;
                    int elementValue = 0;
                    std::cin >> elementValue;
                    matrixB.push_back(elementValue);
                }
                std::cout << matBelements << " elements have been appended to matrixB." << std::endl;
            }


            if (matNamechoice == 3)
            {

                bool matCLive = true;
                std::vector<int> matrixC(0);
                std::cout << "How many elements would you like to add to Matrix" << matNamechoice << "?" << std::endl;
                short matCelements;
                std::cin >> matCelements;
                std::cout << "For the " << matCelements << "elements that you have created, please choose \
                each element value: " << std::endl;
                for (int n = 0; n < matCelements; ++n)
                {
                    std::cout << "ELEMENT NO " << n << ":" << std::endl;
                    int elementValue = 0;
                    std::cin >> elementValue;
                    matrixC.push_back(elementValue);
                }
                std::cout << matCelements << " elements have been appended to matrixC. \n \n" << std::endl;
            }
        }

        if (choice == 2 && (matAlive == true))
            std::cout << "LOADING MATRICIES..." << std::endl;
            for (int n = 10; n <= 100; n += 10)
            {
                std::cout << n << "%" << std::endl;

            }
            std::cout << "MATRIX DATA LOAD SUCCESSFUL..." << std::endl;
            std::cout << "Enter 1 to edit a Matrix. Enter 2 to print a Matrix. Enter 3 to clear a Matrix." << std::endl;
            short userChoice = 0;
            std::cin >> userChoice;
    }

    int x = 0;

    if (x < 100)
    {
        int y = 1;
    }

    return 0;
}
#包括“pch.h”
#包括
#包括
int main()
{
如果(选项==1)
{
std::cout matname选择;
如果(matNamechoice==1)
{
bool matAlive=true;
std::向量矩阵(0);

std::cout将变量移出内部块:

bool matAlive = false;
std::vector<int> matrixA(0);

if (choice == 1) {
    if (matNamechoice == 1) {
        matAlive = true;
        // ...
    }
}
// ...
if (choice == 2 && (matAlive == true)) {
    // ...
}
bool matAlive=false;
std::向量矩阵(0);
如果(选项==1){
如果(matNamechoice==1){
matAlive=true;
// ...
}
}
// ...
如果(选项==2&&(matAlive==true)){
// ...
}

请注意,您应该有其他错误,因为未定义/声明
选项,…这只是代码的一部分,但在第一个“if”代码块中创建的数组被本地化了那么你为什么不把它移动到更大的范围内呢?@bruno我应该把它移动到哪里?在更高的层次上,让它可以在你需要的地方访问。你看到Jarod42 BTW的答案了吗?