今天,当我尝试编写代码以仅对两个2 * 2矩阵进行加法和减法时,我使用了switch语句,但出现了一个错误:
函数main()中局部变量的大小写绕过初始化
代码
#include
#include
#include
int
main()
{
int mat1[2][2], mat2[2][2], mat3[2][2];
cout << "Enter the elements in the first matrix";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cin >> mat1[i][j];
}
}
cout << "\n\nEnter the elements of the second matrix";
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 2; l++) {
cin >> mat2[k][l];
}
}
cout << "\n\nsaved......";
int choice;
cout << "\n\n\nFor adding these two matrices,press 1";
cout << "\nFor subtracting these two matrices,press 2";
cin >> choice;
switch (choice) {
case 1:
cout << "The addition of the two matrices will yield";
for (int a = 0; a <= 1; a++) {
for (int b = 0; b <= 1; b++) {
mat3[a][b] = mat1[a][b] + mat2[a][b];
}
}
break;
case 2:
cout << "The subtraction of the two matrices will yield";
for (int c = 0; c <= 1; c++) {
for (int d = 0; d <= 1; d++) {
mat3[c][d] = mat1[c][d] - mat2[c][d];
}
}
break;
}
getch();
return 0;
}
我还发现我可以通过将案例代码放到花括号中来消除这种错误,
我的困惑是关于error ...
和大括号的要求, case
(我知道我没有使用新的编码约定,如 ,std名称空间等,因为我已经在Turbo C ++编译器中编写了它,所以谦虚地要求一个简单的答案。)