【问题标题】:how to create an interactive menu in C that moves from one function into another without redrawing the menu如何在 C 中创建一个交互式菜单,该菜单从一个功能移动到另一个功能而不重绘菜单
【发布时间】:2012-08-14 03:42:53
【问题描述】:

我的目标是生成一个程序,该程序将文件作为输入并通过将字符前移 3 来“编码”其中的文本(因此“a”将变为“d”)。它应该生成一个带有编码文本的输出文件。该菜单用于接受用户输入并执行分配给所选数字的功能。

我很早就创建了这个程序,但时间不够用,并且正在努力构建它。目前,我有菜单显示,但是当一个子函数被调用时,它会显示,但随后菜单会覆盖它,我不知道为什么。任何帮助,将不胜感激。这是我到目前为止的代码......

#include <stdio.h>
#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 < 0)               /* number too small */
          || (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);
}

【问题讨论】:

    标签: c


    【解决方案1】:

    您不应该使用gets(user_filename) 来获取文件名,因为gets() 读取到\n 并停止读取。当用户键入菜单选项时,菜单选项的scanf 不会读取行尾的\n。本质上,您正在让gets 读取一个没有单词的字符串。您想要阅读的行实际上是下一行。使用scanf 而不是gets 将解决它。

    否则,您的程序将按预期运行 - 只是您的功能尚未执行任何操作,您的菜单正在“覆盖”子菜单。有关使用 scanf 而不是 gets 的实现,请参阅 http://ideone.com/F2pEs

    【讨论】:

    • 谢谢,这为正在发生的事情以及如何解决它提供了一些启示。我已经实施并且现在对它更有信心了。
    【解决方案2】:

    使用 getchar();在获取(用户文件名)之后不久;它将等待获取角色

     gets(user_filename);
     getchar();
     break;
    

    【讨论】:

    • 再次,有很好的信息。你们都在教我一些我可能需要一段时间才能弄清楚的东西!
    【解决方案3】:

    As in this question which Stackoverflow 已突出显示为匹配项,您需要清除缓冲区以删除其中等待的换行符。

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

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

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

    【讨论】:

    • 所有好信息。我只能说我还在学习这门语言,但这很有帮助!
    【解决方案4】:

    嗯,这个答案已经很晚了,但是遇到它,我忍不住写点东西。 让我们直接开始吧。您将拥有一组菜单,其中数组元素是您想要在菜单中使用的选项。然后在一个真实的条件下,循环遍历数组的元素,选择你想要的选项。

    #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;
    }
    

    【讨论】:

    • 刚刚做到了。非常感谢法比奥·图拉蒂
    猜你喜欢
    • 1970-01-01
    • 2010-09-06
    • 2020-03-03
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2018-03-10
    相关资源
    最近更新 更多