使用goto后如何使程序正常运行?
How to make program working properly after using goto?
在任何选择之后,我想询问用户是否要再次使用该程序,或者退出。但是,如果输入 y,代码将无法再次正常工作。我尝试了其他解决方案,如清除内存等,但我不是很有经验,所以我不知道我在做什么。也对我的语言感到抱歉。这是我的代码的一部分。我有 13 个不同的选择,每个选择都一样
char str[100];
int selection;
char selection2;
int i;
begin:
printf("Welcome to the Character/String Specification program\n");
printf("Please enter whatever you want \n");
scanf(" %s", str);
printf("\nSelect the specification you want\n");
printf("1) is your input a letter?\n");
printf("2) is your input a whitespace?\n");
printf("3) is your input a decimal digit?\n");
printf("4) is your input a hexadecimal digit?\n");
printf("5) is your input an upper-case letter?\n");
printf("6) is your input a lower-case letter?\n");
printf("7) is your input a letter or a decimal digit?\n");
printf("8) is your input a control character?(ACSII 0..31 and 127)\n");
printf("9) is your input a not a letter, digit, whitespace, or invisible control character?\n");
printf("10)is your input a printable (ASCIII ' '..'~')\n");
printf("11)is your input have a graphical representation\n");
printf("12)Do you want to see your input as all upper characters?\n");
printf("13)Do you want to see your input as all lower characters?\n\n");
scanf(" %d",&selection);
printf("\n");
while(true)
{
if(selection == 1)
{
while(str[i])
{
if(isalpha(str[i]))
{
printf("%c is an alphabet\n",str[i]);
}
else
{
printf("%c is not an alphabet\n",str[i]);
}
i++;
}
printf("Do you want to go back to the start point? (y) for yes,(n) for no\n");
scanf(" %c",&selection2);
if(selection2=='y')
{
goto begin;
}
else if(selection2=='n')
{
printf("Goodbye\n");
break;
}
而不是使用 goto,我会做这样的事情:
int value = 1;
do {
//enter your code here
if(selection2=='n')
{
printf("Goodbye\n");
value = 0;
break;
}
} while(value)
这将导致代码 运行 至少一次并根据用户输入继续 运行ning。
Goto 不是最佳做法,因为它会增加阅读难度。
在任何选择之后,我想询问用户是否要再次使用该程序,或者退出。但是,如果输入 y,代码将无法再次正常工作。我尝试了其他解决方案,如清除内存等,但我不是很有经验,所以我不知道我在做什么。也对我的语言感到抱歉。这是我的代码的一部分。我有 13 个不同的选择,每个选择都一样
char str[100];
int selection;
char selection2;
int i;
begin:
printf("Welcome to the Character/String Specification program\n");
printf("Please enter whatever you want \n");
scanf(" %s", str);
printf("\nSelect the specification you want\n");
printf("1) is your input a letter?\n");
printf("2) is your input a whitespace?\n");
printf("3) is your input a decimal digit?\n");
printf("4) is your input a hexadecimal digit?\n");
printf("5) is your input an upper-case letter?\n");
printf("6) is your input a lower-case letter?\n");
printf("7) is your input a letter or a decimal digit?\n");
printf("8) is your input a control character?(ACSII 0..31 and 127)\n");
printf("9) is your input a not a letter, digit, whitespace, or invisible control character?\n");
printf("10)is your input a printable (ASCIII ' '..'~')\n");
printf("11)is your input have a graphical representation\n");
printf("12)Do you want to see your input as all upper characters?\n");
printf("13)Do you want to see your input as all lower characters?\n\n");
scanf(" %d",&selection);
printf("\n");
while(true)
{
if(selection == 1)
{
while(str[i])
{
if(isalpha(str[i]))
{
printf("%c is an alphabet\n",str[i]);
}
else
{
printf("%c is not an alphabet\n",str[i]);
}
i++;
}
printf("Do you want to go back to the start point? (y) for yes,(n) for no\n");
scanf(" %c",&selection2);
if(selection2=='y')
{
goto begin;
}
else if(selection2=='n')
{
printf("Goodbye\n");
break;
}
而不是使用 goto,我会做这样的事情:
int value = 1;
do {
//enter your code here
if(selection2=='n')
{
printf("Goodbye\n");
value = 0;
break;
}
} while(value)
这将导致代码 运行 至少一次并根据用户输入继续 运行ning。 Goto 不是最佳做法,因为它会增加阅读难度。