切换循环和案例
Switch loop and cases
#include <stdio.h>
#include <stdlib.h>
#define N 2
struct customer
{
int accno ;
char name[30] ;
float balance ;
}e,m;
struct trans
{
int accno;
char trans_type ;
float amount ;
}e2,m1;
int main()
{
int k,i,p;
char another='y',n;
FILE *fp,*fr,*tp;
//fp=fopen("customer.exe","rb");
//fr=fopen("transaction.exe","wb");
while(1){
printf("\t\tCustomer Transactions:\n");
printf("\t\t*********************\n\n\n");
printf("\t1: Add customer information:\n\n");
printf("\t2: Add transaction information:\n\n");
printf("\t3: List customer information:\n\n");
printf("\t4: List transaction information:\n\n");
printf("\t5: Perform transaction:\n\n");
printf("\t0: Exit:\n\n\n");
printf("your choice ");
scanf("%d",&p);
switch (p){
case 1:
fp=fopen("customer.exe","wb");
while(another=='y')
{
printf("\nEnter account number,Enter name,Enter balance");
scanf("%d %s %f",&e.accno,&e.name,&e.balance);
fwrite(&e,sizeof(e),1,fp);
printf("Enter another y/n?");
scanf(" %c",&another);
}
fclose(fp);
break;
case 2:
fr=fopen("transaction.exe","wb");
while(another=='y'){
printf("\nEnter account number,Enter transaction type(w/d),Enter Amount");
scanf("%d %c %f",&e2.accno,&e2.trans_type,&e2.amount);
fwrite(&e2,sizeof(e2),1,fr);
printf("\n\nEnter another y/n?");
scanf(" %c",&another);
}
fclose(fr);
break;
case 3:
fp=fopen("customer.exe","rb");
printf("all account holders are\n\n");
while(fread(&e,sizeof(e),1,fp)==1){
printf("%d %s %f\n\n",e.accno,e.name,e.balance);
}
fclose(fp);
break;
case 4:
fr=fopen("transaction.exe","rb");
printf("\n\nEnter account number\n\n");
scanf("%d",&m1.accno);
while(fread(&e2,sizeof(e2),1,fr)==1){
printf("%d %c %f\n\n",e2.accno,e2.trans_type,e2.amount);
}
fclose(fr);
break;
case 0:
exit(1);
}
}
return 0;
}
大家好,
所以我正在解决这个问题,但我被困在开关循环中
所以发生的事情是我可以自由 select 任何情况只有我编译和 运行,但是如果我在任何情况下,特别是 1 或 2,我不能回到 1 或 2.
例如,假设我输入 p 作为 1 个宾果游戏,我在案例 1 中,但现在在案例 1 执行后,如果我现在输入 2,则没有任何反应。陷入 while 循环但疯狂的是,我仍然可以自由 select 其他情况。(除了 1/2)
当您退出 case-1
时,您正在输入 another
成为 'n'
。然后,在您再次考虑切换案例之前,您永远不会重置它。就是这个问题。
在 while(1)
块内 - 在开头进行赋值 another = 'y'
。这基本上会使两个 while
循环的条件为真。
while(1){
...
another = 'y'; <----
scanf("%d",&p);
switch (p){
case 1:
fp=fopen("customer.exe","wb");
while(another=='y')
{
这将使您的代码进入 case-1
或 case-2
中的 while
块。
除了所有这些一般建议:检查所用函数的 return 值 - 例如 scanf
、fopen
等。它会节省你来自可能由于这些功能失败而发生的错误情况。
并在启用警告的情况下编译您的代码。 gcc -Wall -Werror progname.c
.
以字符串为输入时需要限制scanf
的输入。例如,建议使用(如您所知,name
包括 [=26=]
将有 30 个字符)。
if( scanf("%29s",e.name) != 1){
fprintf(stderr,"Error in input using scanf\n");
exit(1);
}
#include <stdio.h>
#include <stdlib.h>
#define N 2
struct customer
{
int accno ;
char name[30] ;
float balance ;
}e,m;
struct trans
{
int accno;
char trans_type ;
float amount ;
}e2,m1;
int main()
{
int k,i,p;
char another='y',n;
FILE *fp,*fr,*tp;
//fp=fopen("customer.exe","rb");
//fr=fopen("transaction.exe","wb");
while(1){
printf("\t\tCustomer Transactions:\n");
printf("\t\t*********************\n\n\n");
printf("\t1: Add customer information:\n\n");
printf("\t2: Add transaction information:\n\n");
printf("\t3: List customer information:\n\n");
printf("\t4: List transaction information:\n\n");
printf("\t5: Perform transaction:\n\n");
printf("\t0: Exit:\n\n\n");
printf("your choice ");
scanf("%d",&p);
switch (p){
case 1:
fp=fopen("customer.exe","wb");
while(another=='y')
{
printf("\nEnter account number,Enter name,Enter balance");
scanf("%d %s %f",&e.accno,&e.name,&e.balance);
fwrite(&e,sizeof(e),1,fp);
printf("Enter another y/n?");
scanf(" %c",&another);
}
fclose(fp);
break;
case 2:
fr=fopen("transaction.exe","wb");
while(another=='y'){
printf("\nEnter account number,Enter transaction type(w/d),Enter Amount");
scanf("%d %c %f",&e2.accno,&e2.trans_type,&e2.amount);
fwrite(&e2,sizeof(e2),1,fr);
printf("\n\nEnter another y/n?");
scanf(" %c",&another);
}
fclose(fr);
break;
case 3:
fp=fopen("customer.exe","rb");
printf("all account holders are\n\n");
while(fread(&e,sizeof(e),1,fp)==1){
printf("%d %s %f\n\n",e.accno,e.name,e.balance);
}
fclose(fp);
break;
case 4:
fr=fopen("transaction.exe","rb");
printf("\n\nEnter account number\n\n");
scanf("%d",&m1.accno);
while(fread(&e2,sizeof(e2),1,fr)==1){
printf("%d %c %f\n\n",e2.accno,e2.trans_type,e2.amount);
}
fclose(fr);
break;
case 0:
exit(1);
}
}
return 0;
}
大家好, 所以我正在解决这个问题,但我被困在开关循环中 所以发生的事情是我可以自由 select 任何情况只有我编译和 运行,但是如果我在任何情况下,特别是 1 或 2,我不能回到 1 或 2.
例如,假设我输入 p 作为 1 个宾果游戏,我在案例 1 中,但现在在案例 1 执行后,如果我现在输入 2,则没有任何反应。陷入 while 循环但疯狂的是,我仍然可以自由 select 其他情况。(除了 1/2)
当您退出 case-1
时,您正在输入 another
成为 'n'
。然后,在您再次考虑切换案例之前,您永远不会重置它。就是这个问题。
在 while(1)
块内 - 在开头进行赋值 another = 'y'
。这基本上会使两个 while
循环的条件为真。
while(1){
...
another = 'y'; <----
scanf("%d",&p);
switch (p){
case 1:
fp=fopen("customer.exe","wb");
while(another=='y')
{
这将使您的代码进入 case-1
或 case-2
中的 while
块。
除了所有这些一般建议:检查所用函数的 return 值 - 例如
scanf
、fopen
等。它会节省你来自可能由于这些功能失败而发生的错误情况。并在启用警告的情况下编译您的代码。
gcc -Wall -Werror progname.c
.以字符串为输入时需要限制
scanf
的输入。例如,建议使用(如您所知,name
包括[=26=]
将有 30 个字符)。if( scanf("%29s",e.name) != 1){ fprintf(stderr,"Error in input using scanf\n"); exit(1); }