我正在尝试创建一个菜单功能,循环回到现场0 a.k.a回到do while循环开始上方的菜单,当遇到第1点和第2点if语句时。任何人都可以提供一些帮助吗?
#include <stdio.h> //library including standard input and output functions
#include <stdlib.h> //library including exit and system functions used below
#include <string.h> //library including string functions used
struct packet{
int source;
int destination;
int type;
int port;
char data[50];
char * filename;
};
int main ()
{
struct packet s[50]; //Array for structure input
char choice;
int NetworkPacket = 0, ii = 0;
int recordCount = 0;
///// SPOT 0//////
do{
system("cls");
puts("Please enter an option:\n");
puts("'A' Add a packet:\n");
puts("'D' to Display the Packet List:\n");
puts("'S' to Save the Packets to a file:\n");
puts("'C' to Clear the list of current saved packets:\n");
puts(" or X to exit the program...\n");
//wait for user input
scanf("%c", &choice); //take the first char of user input and assing the value to
//the variable choice using the address of (&) the variable
if(choice == '\n') //check to see that the character is not \n left over from the
scanf("%c", &choice); //previous choice made, if it is then we need to scan the input again
switch (choice)
{
case 'A':
system("cls"); //clear the screen
////SPOT 1//////
if(NetworkPacket==50){
printf("No more network packets can be added");
getch();
}
else{}
////SPOT 1//////
printf("\n****Adding a packet*****\n");
printf("Where is the packet from?\n");
scanf("%i", &s[NetworkPacket].source);
printf("Where is the packet going?\n");
scanf("%i", &s[NetworkPacket].destination);
printf("What type is the packet?\n");
scanf("%i", &s[NetworkPacket].type);
printf("What is the packet's port?\n");
scanf("%i", &s[NetworkPacket].port);
printf("Enter up to 50 characters of data.\n");
scanf("%s", s[NetworkPacket].data);
NetworkPacket++;
break;
case 'D':
system("cls"); //clear the screen
printf("\nDisplaying Infomation\n");
////SPOT 2//////
if(NetworkPacket==0){
printf("no records yet, Please press any key to revert back to the main menu\n");
getch();
}
else{}
////SPOT 2//////
for(ii = 0; ii < NetworkPacket; ii++) { // adds a 1 onto the NetworkPacket counter keeping a tally on the number of records stored.
printf("\nSource: %d", s[ii].source);
printf("\nDestination: %d", s[ii].destination );
printf("\nType : %d", s[ii].type);
printf("\nPort : %dgetch();", s[ii].port);
printf("\nData: %s\n---\n", s[ii].data);
} break;
case 3:
break;
system("cls");
break;
case 4: break;
case 5: break;
default:
system("cls");
printf("%c is not an option\n Try again...\n", choice);
}
}while(choice!='X' && (NetworkPacket >= 50? (printf("No more network packets can be added" ), 0) : 1));
return 0;
}
答案 0 :(得分:0)
像这样使用continue指令:
if (something_cool_happened)
continue;
这将导致do-while循环结束当前迭代并返回循环的开头。这也适用于while循环和for循环
答案 1 :(得分:0)
我不确定我是否正确理解了你的问题。但是要执行循环的下一次迭代,可以使用continue语句。
答案 2 :(得分:0)
如果我误解了这个问题,请原谅我。