c语言 case语句用大括号,c++ - 关于“ switch”中“ case”语句中的花括号 - 堆栈内存溢出...

今天,当我尝试编写代码以仅对两个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 ++编译器中编写了它,所以谦虚地要求一个简单的答案。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值