如何在C中创建一个交互式菜单,从一个function移动到另一个function而不重绘菜单

我的目标是制作一个程序,它将文件作为输入,并通过将字符前移3来“编码”文本(所以’a’将来’d’)。 它应该生成带有编码文本的输出文件。 菜单用于输入用户输入并执行分配给所选号码的function。

我很早就开始创建这个程序,但是时间紧迫,我正在努力构建它。 目前,我有菜单显示,但是当调用子function时,它会显示但是然后菜单会覆盖它,我无法找出原因。 任何帮助,将不胜感激。 这是我到目前为止的代码……

#include  #define INPUT_FILE 1 //define statements #define OUTPUT_FILE 2 #define NUM_TO_SHIFT 3 #define ENCODE 4 #define QUIT 0 int menu(); //function prototypes int input(); int output(); int shift(); int encode(); void quit(); int main() { int choice; // main variables char user_filename[100]; choice = menu(); // get user's first selection while(choice != QUIT) //execute so long as choice is not equal to QUIT { switch(choice) { case INPUT_FILE: printf("Enter the filename of the file to encode:\n"); printf("(hit the Enter key when done)\n"); gets(user_filename); break; case OUTPUT_FILE: output(); break; case NUM_TO_SHIFT: shift(); break; case ENCODE: encode(); break; case QUIT: quit(); break; default: printf("Oops! An invalid choice slipped through. "); printf("Please try again.\n"); } choice = menu(); /* get user's subsequent selections */ } printf("Bye bye!\n"); return 0; } int menu(void) { int option; printf("Text Encoder Service\n\n"); printf("1.\tEnter name of input file (currently 'Secret.txt')\n"); printf("2.\tEnter name of output file (currently not set)\n"); printf("3.\tEnter number of characters data should be shifted (currently +7)\n"); printf("4.\tEncode the text\n\n"); printf("0.\tQuit\n\n"); printf("Make your selection: "); while( (scanf(" %d", &option) != 1) /* non-numeric input */ || (option  4)) /* number too large */ { fflush(stdin); /* clear bad data from buffer */ printf("That selection isn't valid. Please try again.\n\n"); printf("Your choice? "); } return option; } int input() { } int output() { return 2; } int shift() { return 3; } int encode() { return 4; } void quit() { printf("Quiting...Bye!"); exit(0); } 

您不应该使用gets(user_filename)来获取文件名,因为gets()会读取\n并停止读取。 当用户在菜单选项中键入时,菜单选项的scanf不会读取行尾的\n 。 从本质上讲,你正在阅读一个没有文字的字符串。 您阅读的行实际上是下一行。 使用scanf代替gets将修复它。

否则,您的程序正在按预期工作 – 只是您的function没有做任何事情,而您的菜单“覆盖”子菜单。 有关使用scanf而不是gets的实现,请参见http://ideone.com/F2pEs 。

使用getchar(); get(user_filename)之后不久; 它会等待获得角色

  gets(user_filename); getchar(); break; 

正如在这个Stackoverflow突出显示为匹配的问题中,您需要清除缓冲区以删除在那里等待的换行符。

阅读有效的菜单选项后添加此代码:

 do { c = getchar(); } while (c != EOF && c != '\n'); 

其中c是由option声明的char 。 这会循环输入流中的剩余字符,直到达到EOF (文件结束)或换行符,这意味着它们不会影响您对gets()调用。 请注意, gets()被认为是不安全的,因为它不能防止缓冲区溢出,用户可以轻松输入超过100个字符(包括换行符)并开始写入不应被其输入触及的内存。 当您看到围绕这样的函数调用的编译器警告时,您最好查找安全等效项,通常它们会采用第二个参数,即读入的缓冲区的最大大小。

嗯,这个答案是迟到的,但遇到它,我忍不住写点什么。 让我们直截了当。 您将拥有一系列菜单,数组元素是您在菜单中所需的选项。 然后在处于真实状态时,循环遍历数组的元素,选择所需的选项。

 #include "stdio.h" #include "stdlib.h" #include "string.h" //function prototypes int input(); int output(); int shift(); int encode(); void quit(); int main(){ int menus_on = 1; const char *menus[5] = {"Input","Output","Shift","Encode","Quit"}; while(menus_on){ int menu,*temp; for(int i =0;i<6;i++){ printf("%d: %s\n",i,menus[i]); } printf("Select menu\n"); scanf("%d",temp); menu = *temp; printf("Selected menu::%d\n",menu); switch(menu){ case 0: input(); break; case 1: output(); break; case 2: shift(); break; case 3: encode(); break; case 4: quit(); break; default: printf("Invalid selection\n"); break; } } return 0; } int input() { return 0; } int encode () { return 0; }