gpt4 book ai didi

c - getline的二次使用

转载 作者:行者123 更新时间:2023-11-30 19:03:59 26 4
gpt4 key购买 nike

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 作为选项,因此它是无效选项。我找不到错误,请帮忙。

最佳答案

#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

关于c - getline的二次使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53195598/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com