如何从切换菜单返回
How to go back from switch menu
我想制作一个菜单,将动物分类为昆虫、鸟类、哺乳动物和鱼类,每一种都有特定的特征(使用复合变量)。我想我会使用一个嵌套的开关盒(一个开关用于动物类别,另一个开关用于每个类别以执行操作)。问题是,例如,在我输入了一个新条目之后,我想可以选择返回并选择另一个操作,甚至返回并选择另一个类别。我怎样才能做到这一点?我尝试了 do-while,但我只是陷入了无限循环。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct{
unsigned int noLegs, lifeSpan;
char *name;
}insect;
typedef struct{
float speed, length;
char *habits, *name;
}bird;
typedef struct{
float weight, height;
char *food, *name;
}mammal;
typedef struct{
float weight, depth, salt;
char *name;
}fish;
int main(int argc, char **argv)
{
char choice, action;
bool running = true;
bool returnBack = true;
insect *i = (insect *)malloc(sizeof(insect));
if(!i)
{
printf("Failed to allocate memory.\n");
return -1;
}
while(running)
{
printf("Please choose a category:\n");
printf("1. Insects\n2. Birds\n3. Mammals\n4. Fishes\n5. Exit\n");
scanf(" %c", &choice);
switch(choice)
{
case '1':
printf("You've chosen: Insects\n");
while(returnBack)
{
printf("Please choose an action:\n");
printf("a. Add entry\nb. Delete entry\nc. Replace entry\nd. Lookup entries\ne. Back\n");
scanf(" %c", &action);
switch(action)
{
case 'a':
printf("What entry would you like to add?\n");
i->name = (char *)malloc(sizeof(char));
scanf("%s", i->name);
printf("How many legs does a %s have?\n", i->name);
scanf("%u", &(i->noLegs));
printf("What is the lifespan of a %s?\n", i->name);
scanf("%u", &(i->lifeSpan));
break;
case 'b':
printf("Which entry would you like to delete?\n");
break;
case 'c':
printf("Which entry would you like to replace?\n");
break;
case 'd':
printf("Which entry would you like to replace?\n");
break;
case 'e':
returnBack = false;
break;
default:
printf("Invalid action.\n");
}
}
break;
case '2':
printf("You've chosen: Birds\n");
break;
case '3':
printf("You've chosen: Mammals\n");
break;
case '4':
printf("You've chosen: Fishes\n");
break;
case '5':
running = false;
break;
default:
printf("Invalid choice.\n");
}
}
return 0;
}
正如 alex01011 所说,你只需要 while
覆盖整个 switch
当用户选择 5
时,running
变为 FALSE
并退出循环。
不过有一件事,你忘了清除第一个 scanf()
中的空格
#include <stdio.h>
#include <stdlib.h>
#define TRUE (1==1)
#define FALSE (!TRUE)
/* ...
* ALL THE STRUCTS
*/
int main(int argc, char **argv)
{
char choice, action, running = TRUE;
insect *i = (insect *)malloc(sizeof(insect));
// CHECK ON MALLOC RETURN
while(running)
{
printf("Please choose a category:\n");
printf("1. Insects\n2. Birds\n3. Mammals\n4. Fishes\n5. Exit\n\n");
scanf(" %c", &choice); // <-- The leading space was missing
switch(choice)
{
/* ...
* PREVIOUS CASES
*/
case '5':
running = FALSE;
break;
default:
printf("Invalid choice.\n");
}
}
return 0;
}
我想制作一个菜单,将动物分类为昆虫、鸟类、哺乳动物和鱼类,每一种都有特定的特征(使用复合变量)。我想我会使用一个嵌套的开关盒(一个开关用于动物类别,另一个开关用于每个类别以执行操作)。问题是,例如,在我输入了一个新条目之后,我想可以选择返回并选择另一个操作,甚至返回并选择另一个类别。我怎样才能做到这一点?我尝试了 do-while,但我只是陷入了无限循环。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct{
unsigned int noLegs, lifeSpan;
char *name;
}insect;
typedef struct{
float speed, length;
char *habits, *name;
}bird;
typedef struct{
float weight, height;
char *food, *name;
}mammal;
typedef struct{
float weight, depth, salt;
char *name;
}fish;
int main(int argc, char **argv)
{
char choice, action;
bool running = true;
bool returnBack = true;
insect *i = (insect *)malloc(sizeof(insect));
if(!i)
{
printf("Failed to allocate memory.\n");
return -1;
}
while(running)
{
printf("Please choose a category:\n");
printf("1. Insects\n2. Birds\n3. Mammals\n4. Fishes\n5. Exit\n");
scanf(" %c", &choice);
switch(choice)
{
case '1':
printf("You've chosen: Insects\n");
while(returnBack)
{
printf("Please choose an action:\n");
printf("a. Add entry\nb. Delete entry\nc. Replace entry\nd. Lookup entries\ne. Back\n");
scanf(" %c", &action);
switch(action)
{
case 'a':
printf("What entry would you like to add?\n");
i->name = (char *)malloc(sizeof(char));
scanf("%s", i->name);
printf("How many legs does a %s have?\n", i->name);
scanf("%u", &(i->noLegs));
printf("What is the lifespan of a %s?\n", i->name);
scanf("%u", &(i->lifeSpan));
break;
case 'b':
printf("Which entry would you like to delete?\n");
break;
case 'c':
printf("Which entry would you like to replace?\n");
break;
case 'd':
printf("Which entry would you like to replace?\n");
break;
case 'e':
returnBack = false;
break;
default:
printf("Invalid action.\n");
}
}
break;
case '2':
printf("You've chosen: Birds\n");
break;
case '3':
printf("You've chosen: Mammals\n");
break;
case '4':
printf("You've chosen: Fishes\n");
break;
case '5':
running = false;
break;
default:
printf("Invalid choice.\n");
}
}
return 0;
}
正如 alex01011 所说,你只需要 while
覆盖整个 switch
当用户选择 5
时,running
变为 FALSE
并退出循环。
不过有一件事,你忘了清除第一个 scanf()
#include <stdio.h>
#include <stdlib.h>
#define TRUE (1==1)
#define FALSE (!TRUE)
/* ...
* ALL THE STRUCTS
*/
int main(int argc, char **argv)
{
char choice, action, running = TRUE;
insect *i = (insect *)malloc(sizeof(insect));
// CHECK ON MALLOC RETURN
while(running)
{
printf("Please choose a category:\n");
printf("1. Insects\n2. Birds\n3. Mammals\n4. Fishes\n5. Exit\n\n");
scanf(" %c", &choice); // <-- The leading space was missing
switch(choice)
{
/* ...
* PREVIOUS CASES
*/
case '5':
running = FALSE;
break;
default:
printf("Invalid choice.\n");
}
}
return 0;
}