getline的第二次使用

时间:2018-11-07 18:27:44

标签: c

int get_int(){//gets an integer 
  size_t readint;
  size_t numbytes;

  char *input;
  int inint;
  int num;

  input = NULL;
  numbytes = 0;
  readint = getline(&input, &numbytes, stdin);
  if (readint == 0){
    num = 0;

  }else{
    num = atoi(input);//string to integer

  }
  return num;
   }
int exit_program(){//allows to finish and exit the program

  char c;
  printf("\nAre you sure you want to exit?(y/n) ");
  c = get_char();
  if(c == 'y' || c == 'Y'){
    exit(0);}else{
 if(c =='n' || c == 'N'){
      show_menu(); 
     }}return 0;
}

void show_menu(){//choose the option
  int option; 
printf("Type an option: \n");
    option = get_int();     
    switch(option){
        case 1: exit_program(); break;
        case 2: //create_jedi(&head); break;

        case 3: //show_jedis(head); break;

        case 4: puts("To be implemented"); break;

        case 5: puts("To be implemented"); break;

        default: printf("Invalid option"); break;
    }}

main(){
show_menu();}

该代码应该询问一个值。如果选择“ 1”,它将询问您是否要退出程序。如果答案是否定的,则程序必须再次询问您一个值,但是,它会将0分配为一个选项,因此它是无效的选项。 我找不到错误,请帮忙。

1 个答案:

答案 0 :(得分:1)

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void clear_stream(FILE *is)  // clears garbage from the stream after
{                            // a failed input operation
    int ch;
    while ((ch = fgetc(is)) != EOF && ch != '\n');
}

// reads an integer from stdin until it is within the range min, max
int get_int(int min, int max, char const *prompt)
{
    int number;
    while (prompt && printf("%s", prompt),
           scanf("%d", &number) != 1 || number < min || max < number)
    {
        fputs("Input error :(\n\n", stderr);
        clear_stream(stdin);
    }
    return number;
}

// reads a character from stdin until it is within range (if range != null)
char get_char(char const *range, char const *prompt)
{
    char ch;
    while (prompt && printf("%s", prompt),
           scanf(" %c", &ch) != 1 || range && !strchr(range, ch) )
           //     ^ skips leading whitespace
    {
        fputs("Input error :(\n\n", stderr);
        clear_stream(stdin);
    }
    return ch;
}

// asks user if he is sure to exit the program and returns true if that is so
bool exit_program()
{
    char choice = get_char("yYnN",
        "\nAre you sure you want to exit?\n"
        "[y] Yes\n"
        "[n] No\n\n"
        "> ");
    return choice == 'y' || choice == 'Y';
}

void show_menu()
{
    bool do_exit = false;  // false until the user decides otherwise.
    do {
        int choice = get_int(1, 4,
            "\n[1] Exit\n"
            "[2] Function A\n"
            "[3] Function B\n"
            "[4] Function C\n\n"
            "> ");

        switch (choice) {
        case 1:
            do_exit = exit_program();
            break;

        case 2:
            puts("Function A\n");
            break;

        case 3:
            puts("Function B\n");
            break;

        case 4:
            puts("Function C\n");
            break;
        }
    } while (!do_exit);
}

int main(void)
{
    show_menu();
}

示例输出:

[1] Exit
[2] Function A
[3] Function B
[4] Function C

Type an option: 1

Are you sure you want to exit?
[y] Yes
[n] No

> f
Input error :(


Are you sure you want to exit?
[y] Yes
[n] No

> asldjkflkasdjf
Input error :(


Are you sure you want to exit?
[y] Yes
[n] No

> n

[1] Exit
[2] Function A
[3] Function B
[4] Function C

Type an option: 2
Function A


[1] Exit
[2] Function A
[3] Function B
[4] Function C

Type an option: 3
Function B


[1] Exit
[2] Function A
[3] Function B
[4] Function C

Type an option: 4
Function C


[1] Exit
[2] Function A
[3] Function B
[4] Function C

Type an option: 1

Are you sure you want to exit?
[y] Yes
[n] No

> n

[1] Exit
[2] Function A
[3] Function B
[4] Function C

Type an option: 2
Function A


[1] Exit
[2] Function A
[3] Function B
[4] Function C

Type an option: 1

Are you sure you want to exit?
[y] Yes
[n] No

> y