[英]“for” loop inside a switch case doesn't work
我有一个程序理论上应该在scelta1 = 1的情况下打开一个特定的函数,它重新分配一个最初初始化的数组。 问题是,程序进入开关情况,看到scelta1 = 1但它没有重新分配它只显示“插入数组的元素[1] [1]”“插入数组的元素[1] [2]”让我插入它们,虽然它最初确实如此,所以我将假设问题不在函数中。
总结一下:我想知道为什么在这种情况下它不会循环并让我在函数正确时插入数组的新数字。 谢谢
这是该计划:
#include <stdio.h>
#include <stdlib.h>
#include "matrici.h"
#define N 3
void leggi_matr(int MAT[N][N], int nRighe, int nColonne) // The function
{
int i;
int j;
for (i=0 ; i<nRighe ; i ++)
for (j=0 ; j<nColonne ; j ++)
{
printf("\nInserisci l'elemento [%d][%d] da inserire nella matrice : ", i+1,j+1);
scanf("%d", & MAT[i][j]);
}
}
int A[N][N];
int B[N][N];
int C[N][N];
int main (){
int nRighe = 3;
int nColonne = 3;
int scelta = 1;
char scelta1 = 0;
printf ("Inserimento matrice A\n\n");
leggi_matr(A, nRighe, nColonne);
printf ("\n\nInserimento matrice B\n\n");
leggi_matr(B, nRighe, nColonne);
switch (scelta) /* I know in this case it doesn't need a switch, i need it for
{ another thing */
case 1:
printf ("\n\nDo you want to insert array A or B? ");
scanf ("%c", &scelta1);
if (scelta1 == 'A')
{
leggi_matr(A, nRighe, nColonne);
}
else
{
leggi_matr(B, nRighe, nColonne);
}
break;
}
printf("\n\n\n");
system("PAUSE");
return 0;
}
我认为问题在于 -
scanf ("%d", &scelta1);
if (scelta1 == A)
%d
是整数的说明符。
正如Jonathan在评论中正确指出的那样, scelta is 0
所以当你到达switch (scelta)
,你将不会执行case 1: code
您可能想要更改值
int scelta = 0;
至
int scelta = 1;
还尝试将scanf更改为接受如下字符: -
scanf ("%c", &scelta1);
然后比较它
if (scelta1 == 'A')
编辑:-
另外最好采用char scelta1
而不是int scelta1
因为你想获得一个字符文字。
我注意到的第一件事:
int scelta = 0;
switch (scelta)
{
case 1:
...
}
它不会成功。 的值是0
并且仅存在一个的情况下1
。
使用像Net-beans这样的好IDE。 它显示了大部分这类问题。 使用此代码段更改。
scanf("%c", &scelta1);
if(scelta1 == 'A')
{
}
不
if (scelta1 == A)
{ //code
}
您正在比较数组指针和整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.