gpt4 book ai didi

c - 如何使用 while 循环返回启动程序?

转载 作者:行者123 更新时间:2023-11-30 21:49:06 25 4
gpt4 key购买 nike

当我选择它时,我遇到了一个问题,它总是退出我的程序,当我输入错误的选择时,我必须退出吗?

我的循环出了什么问题?请帮助我,因为循环我遇到了严重的问题。这是一个基本概念,但我的大脑已经陈旧且卡住了。

你能帮我理解循环以返回到程序的开头吗?

谢谢

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
char choice,name[40],date[40],note[500],;
int password;
int ch;
printf("\n\n\t\tMAIN MENU:");
printf("\n\n\tADD RECORD\t[1]");
printf("\n\tVIEW RECORD\t[2]");
printf("\n\tEDIT RECORD\t[3]");
printf("\n\tDELETE RECORD\t[4]");
printf("\n\tEDIT PASSWORD\t[5]");
printf("\n\tEXIT\t\t[6]");
printf("\n\n\tENTER YOUR CHOICE:");
scanf("%d",&ch);

switch(ch)
do{
{
opcase 1:
printf("\n\tEDIT RECORD\t[3]");
printf("\n\tEDIT RECORD\t[3]");
printf("\n\tEDIT RECORD\t[3]");
printf("\n\tEDIT RECORD\t[3]");
break;

case 2:
printf("\n\tEDIT RECORD\t[3]");
break;

case 3:
printf("\n\tEDIT RECORD\t[3]");
break;

case 4:
printf("\n\tEDIT RECORD\t[3]");
break;

case 5:
printf("\n\tEDIT RECORD\t[3]");
break;

case 6:
printf("\n\n\t\tTHANK YOU FOR USING THE SOFTWARE BY:\n\n\tBIJAY PURI\n\n\tBHANU POUDEL\n\n\tNRIPASH AYER...");
getch();
exit(0);

default:
printf("\nYOU ENTERED WRONG CHOICE..");
printf("\nPRESS ANY KEY TO TRY AGAIN");
getch();
break;
}
}
while(ch!=6);
return 0;
getch();
}

最佳答案

将您的 do..while 循环放在 switch 案例之外。

提示:如果您正确缩进了代码,您可能已经自己解决了这个问题。

正确的语法:

do 
{

//do something
switch (variable)
{
//case
break;
}
}
while (condition)
<小时/>

编辑:

下面是您的代码的工作版本[在Linux上]。请仔细注意更改。

#include <stdio.h>
#include <stdlib.h>

int main()
{
char choice,name[40],date[40],note[500];
int password;
int ch;

printf("\n\n\t\tMAIN MENU:");
printf("\n\n\tADD RECORD\t[1]");
printf("\n\tVIEW RECORD\t[2]");
printf("\n\tEDIT RECORD\t[3]");
printf("\n\tDELETE RECORD\t[4]");
printf("\n\tEDIT PASSWORD\t[5]");
printf("\n\tEXIT\t\t[6]");


do{
printf("\n\n\tENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)

{
case 1:
printf("\n\tEDIT RECORD\t[3]\n");
printf("\n\tEDIT RECORD\t[3]\n");
printf("\n\tEDIT RECORD\t[3]\n");
printf("\n\tEDIT RECORD\t[3]\n");

break;
case 2:
printf("\n\tEDIT RECORD\t[3]");
break;
case 3:
printf("\n\tEDIT RECORD\t[3]");
break;
case 4:
printf("\n\tEDIT RECORD\t[3]");
break;
case 5:
printf("\n\tEDIT RECORD\t[3]");
break;
case 6:
printf("\n\n\t\tTHANK YOU FOR USING THE SOFTWARE BY:\n\n\tBIJAY PURI\n\n\tBHANU POUDEL\n\n\tNRIPASH AYER...");
exit(0);
default:
printf("\nYOU ENTERED WRONG CHOICE..");
printf("\nPRESS ANY KEY TO TRY AGAIN");
break;
}
} while(1); //no need to check conditions here, redundant.

return 0 ;
}

关于c - 如何使用 while 循环返回启动程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27062541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com