对于带有Switch语句的字符,需要使用Do/While循环与NOT运算符一起工作
如果循环结束时选择了字符“3”,或者如果我键入除1或2以外的任何字符,那么它将成为默认值,我如何做到这一点 我用它们作为角色来修复一个bug,如果我输入数字以外的任何东西,就会导致一个奇怪的疯狂循环对于带有Switch语句的字符,需要使用Do/While循环与NOT运算符一起工作,c,switch-statement,C,Switch Statement,如果循环结束时选择了字符“3”,或者如果我键入除1或2以外的任何字符,那么它将成为默认值,我如何做到这一点 我用它们作为角色来修复一个bug,如果我输入数字以外的任何东西,就会导致一个奇怪的疯狂循环 void menu(double data[], double output[], int *count){ int count1 = *count; char com; do{ com = displaymenu(&count1);
void menu(double data[], double output[], int *count){
int count1 = *count;
char com;
do{
com = displaymenu(&count1);
switch(com){
case '1':
enterdata(data, &count1);
break;
case '2':
processdata(data, output, &count1);
displaydata(data, output, &count1);
break;
case '3':
printf("Have a nice day!!!\n\n\n\n\n");
break;
default:
printf("Not a Valid Option \n\n\n");
break;
}
}while(com != '3');
}
int displaymenu(int *count1){
int count2 = *count1;
char choice;
printf("*------------------------------------------------------------*\n");
printf("| Mini Stats Package Data Entries = %3d |\n",count2);
printf("*------------------------------------------------------------*\n");
printf("* *\n");
printf("* Enter a number to perform one of the following *\n");
printf("* *\n");
printf("* 1. Enter Data. *\n");
printf("* *\n");
printf("* 2. Display the data and the following statistics: *\n");
printf("* the number of data items, the high and low values *\n");
printf("* in the data[], the mean, the median, mode, *\n");
printf("* variance and the standard deviation. *\n");
printf("* *\n");
printf("* 3. Quit Program *\n");
printf("* *\n");
printf("*------------------------------------------------------------*\n\n");
printf("Your Choice? ");
scanf("%c",&choice);
printf("\n\n\n");
return choice;
}
在while循环之前的顶部声明
com
,并在循环底部修复数值比较。下面是一个工作示例
#include <stdio.h>
#include <stdlib.h>
int main(){
char com;
do{
com = getchar();
getchar(); // consume the extra newline from pushing enter
switch(com){
case '1':
printf("Case 1 was hit\n");
break;
case '2':
printf("Case 2 was hit\n");
break;
case '3':
printf("Have a nice day!!!\n\n\n\n\n");
break;
default:
printf("Not a Valid Option \n\n\n");
break;
}
}while(com !=('3'));
}
#包括
#包括
int main(){
char-com;
做{
com=getchar();
getchar();//通过按enter键使用额外的换行符
交换机(com){
案例“1”:
printf(“案例1被击中\n”);
打破
案例“2”:
printf(“案例2被击中\n”);
打破
案例“3”:
printf(“祝您愉快!!!\n\n\n\n”);
打破
违约:
printf(“不是有效的选项\n\n\n”);
打破
}
}而(com!=('3'));
}
在while循环之前的顶部声明com
,并在循环底部修复数值比较。下面是一个工作示例
#include <stdio.h>
#include <stdlib.h>
int main(){
char com;
do{
com = getchar();
getchar(); // consume the extra newline from pushing enter
switch(com){
case '1':
printf("Case 1 was hit\n");
break;
case '2':
printf("Case 2 was hit\n");
break;
case '3':
printf("Have a nice day!!!\n\n\n\n\n");
break;
default:
printf("Not a Valid Option \n\n\n");
break;
}
}while(com !=('3'));
}
#包括
#包括
int main(){
char-com;
做{
com=getchar();
getchar();//通过按enter键使用额外的换行符
交换机(com){
案例“1”:
printf(“案例1被击中\n”);
打破
案例“2”:
printf(“案例2被击中\n”);
打破
案例“3”:
printf(“祝您愉快!!!\n\n\n\n”);
打破
违约:
printf(“不是有效的选项\n\n\n”);
打破
}
}而(com!=('3'));
}
在while循环之前的顶部声明com
,并在循环底部修复数值比较。下面是一个工作示例
#include <stdio.h>
#include <stdlib.h>
int main(){
char com;
do{
com = getchar();
getchar(); // consume the extra newline from pushing enter
switch(com){
case '1':
printf("Case 1 was hit\n");
break;
case '2':
printf("Case 2 was hit\n");
break;
case '3':
printf("Have a nice day!!!\n\n\n\n\n");
break;
default:
printf("Not a Valid Option \n\n\n");
break;
}
}while(com !=('3'));
}
#包括
#包括
int main(){
char-com;
做{
com=getchar();
getchar();//通过按enter键使用额外的换行符
交换机(com){
案例“1”:
printf(“案例1被击中\n”);
打破
案例“2”:
printf(“案例2被击中\n”);
打破
案例“3”:
printf(“祝您愉快!!!\n\n\n\n”);
打破
违约:
printf(“不是有效的选项\n\n\n”);
打破
}
}而(com!=('3'));
}
在while循环之前的顶部声明com
,并在循环底部修复数值比较。下面是一个工作示例
#include <stdio.h>
#include <stdlib.h>
int main(){
char com;
do{
com = getchar();
getchar(); // consume the extra newline from pushing enter
switch(com){
case '1':
printf("Case 1 was hit\n");
break;
case '2':
printf("Case 2 was hit\n");
break;
case '3':
printf("Have a nice day!!!\n\n\n\n\n");
break;
default:
printf("Not a Valid Option \n\n\n");
break;
}
}while(com !=('3'));
}
#包括
#包括
int main(){
char-com;
做{
com=getchar();
getchar();//通过按enter键使用额外的换行符
交换机(com){
案例“1”:
printf(“案例1被击中\n”);
打破
案例“2”:
printf(“案例2被击中\n”);
打破
案例“3”:
printf(“祝您愉快!!!\n\n\n\n”);
打破
违约:
printf(“不是有效的选项\n\n\n”);
打破
}
}而(com!=('3'));
}
试试这个
int count1 = *count;
int com;
do{
com = displaymenu(&count1);
switch(com){
case 1:
enterdata(data, &count1);
break;
case 2:
processdata(data, output, &count1);
displaydata(data, output, &count1);
break;
case 3:
printf("Have a nice day!!!\n\n\n\n\n");
break;
default:
printf("Not a Valid Option \n\n\n");
break;
}
}while(com != 3);
对displaymenu进行一些更改
int displaymenu(int *count1){
int count2 = *count1;
int choice; //make it of int type
/*********
**********/
printf("Your Choice? ");
scanf("%d",&choice); // Get integer value here
printf("\n\n\n");
return choice;
}
试试这个
int count1 = *count;
int com;
do{
com = displaymenu(&count1);
switch(com){
case 1:
enterdata(data, &count1);
break;
case 2:
processdata(data, output, &count1);
displaydata(data, output, &count1);
break;
case 3:
printf("Have a nice day!!!\n\n\n\n\n");
break;
default:
printf("Not a Valid Option \n\n\n");
break;
}
}while(com != 3);
对displaymenu进行一些更改
int displaymenu(int *count1){
int count2 = *count1;
int choice; //make it of int type
/*********
**********/
printf("Your Choice? ");
scanf("%d",&choice); // Get integer value here
printf("\n\n\n");
return choice;
}
试试这个
int count1 = *count;
int com;
do{
com = displaymenu(&count1);
switch(com){
case 1:
enterdata(data, &count1);
break;
case 2:
processdata(data, output, &count1);
displaydata(data, output, &count1);
break;
case 3:
printf("Have a nice day!!!\n\n\n\n\n");
break;
default:
printf("Not a Valid Option \n\n\n");
break;
}
}while(com != 3);
对displaymenu进行一些更改
int displaymenu(int *count1){
int count2 = *count1;
int choice; //make it of int type
/*********
**********/
printf("Your Choice? ");
scanf("%d",&choice); // Get integer value here
printf("\n\n\n");
return choice;
}
试试这个
int count1 = *count;
int com;
do{
com = displaymenu(&count1);
switch(com){
case 1:
enterdata(data, &count1);
break;
case 2:
processdata(data, output, &count1);
displaydata(data, output, &count1);
break;
case 3:
printf("Have a nice day!!!\n\n\n\n\n");
break;
default:
printf("Not a Valid Option \n\n\n");
break;
}
}while(com != 3);
对displaymenu进行一些更改
int displaymenu(int *count1){
int count2 = *count1;
int choice; //make it of int type
/*********
**********/
printf("Your Choice? ");
scanf("%d",&choice); // Get integer value here
printf("\n\n\n");
return choice;
}
scanf
有点棘手。这里发生了两件事。第一个是来自控制台的stdin
是行缓冲的;也就是说,在按下enter键之前不会处理输入。第二个是scanf(“%c”,&c)
读取任何字符,包括空格,因此如果输入3,字符流是'3'
,'\n'
解决问题的快速方法是在读取字符之前扫描所有空白:
scanf(" %c", &choice);
这将跳过先前输入流中仍保留在流中的任何新行字符
但在我看来,这不是一个好的解决方案。最好读一整行,然后从中取第一行:
char line[20];
char choice = 0;
fgets(line, sizeof(line), stdin);
sscanf(line, " %c", &choice);
这仍然不好:如果用户用Ctrl-D/Ctrl-Z终止输入会怎么样?如果用户没有在行中指定非空格字符怎么办?您应该检查这些函数的返回值,并决定在这些情况下采取哪些操作。Ctrl-D可能会终止程序。空行可能需要重新读取该行。因此:
int displaymenu()
{
char choice = 0;
char line[20];
// Display the menu
do {
if (fgets(line, sizeof(line), stdin) == NULL) {
choice = '3';
break;
}
} while (sscanf(line, " %c", &choice) < 1);
printf("\n\n\n");
return choice;
}
int显示菜单()
{
字符选择=0;
字符行[20];
//显示菜单
做{
如果(fgets(line,sizeof(line),stdin)==NULL){
选择='3';
打破
}
}而(sscanf(行、%c、&choice)<1);
printf(“\n\n\n”);
回报选择;
}
scanf
有点棘手。这里发生了两件事。第一个是来自控制台的stdin
是行缓冲的;也就是说,在按下enter键之前不会处理输入。第二个是scanf(“%c”,&c)
读取任何字符,包括空格,因此如果输入3,字符流是'3'
,'\n'
解决问题的快速方法是在读取字符之前扫描所有空白:
scanf(" %c", &choice);
这将跳过先前输入流中仍保留在流中的任何新行字符
但在我看来,这不是一个好的解决方案。最好读一整行,然后从中取第一行:
char line[20];
char choice = 0;
fgets(line, sizeof(line), stdin);
sscanf(line, " %c", &choice);
这仍然不好:如果用户用Ctrl-D/Ctrl-Z终止输入会怎么样?如果用户没有在行中指定非空格字符怎么办?您应该检查这些函数的返回值,并决定在这些情况下采取哪些操作。Ctrl-D可能会终止程序。空行可能需要重新读取该行。因此:
int displaymenu()
{
char choice = 0;
char line[20];
// Display the menu
do {
if (fgets(line, sizeof(line), stdin) == NULL) {
choice = '3';
break;
}
} while (sscanf(line, " %c", &choice) < 1);
printf("\n\n\n");
return choice;
}
int