代码块中变量的全球化
Globalisation of variables within code block
我在使用代码的稍后点在第一个if语句(if(peacoy == 1))中创建的数组/矩阵时遇到麻烦。我想编辑数组(当我提到(选择== 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;
}
在内部块外移动变量:
bool matAlive = false;
std::vector<int> matrixA(0);
if (choice == 1) {
if (matNamechoice == 1) {
matAlive = true;
// ...
}
}
// ...
if (choice == 2 && (matAlive == true)) {
// ...
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 将包含C样式数组的对象初始化为成员变量(C++)
- 当vector是tje全局变量时,c++中vector的内存管理
- 通过多个头文件使用常量变量
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 执行函数时导致崩溃的变量
- 代码块中变量的全球化