用户定义的功能,用于读取输入不起作用(User

编程入门 行业动态 更新时间:2024-10-10 17:33:30
用户定义的功能,用于读取输入不起作用(User-Defined function for reading input not working)

我已经创建了一个用户定义的函数来读取输入并用'\0'替换换行符'\n' ,所以当我使用printf语句打印字符串时,它不会在最后添加换行符。

char xgets(char *line, int size, FILE *stdn) { //READS THE LINE fgets(line, size, stdn); //REMOVES NEWLINE CHARACTER '\n' AND ADDS '\0' line[strcspn(line, "\n")] = '\0'; return line; }

当我在main()函数中调用xgets它可以正常工作,但是当在其他用户定义的函数中调用它时,它不会等待用户输入。

我正在使用Visual Studio 2015来调试我的代码。

这是我的代码:

#include<stdio.h> #include<stdlib.h> #include<process.h> //USER-DEFINED FUNCTION char xgets(char *line, int size, FILE *stdn); void sortm_hgrade(); void sortm_rcharge(); void header(void); void header(void) { printf("*-*-*-*-*HOTEL_INFO*-*-*-*-*"); printf("\n\n"); } char xgets(char *line, int size, FILE *stdn) { //READS THE LINE fgets(line, size, stdn); //REMOVES NEWLINE CHARACTER '\n' AND ADDS '\0' END LINE CHARACTER line[strcspn(line, "\n")] = '\0'; return line; } #define MAX 1000 //PROGRAMS STARTS HERE int main(void) { //VARIABLE-DECLARATION int i = 0, j = 0, n = 0; char line[MAX] = { 0 }; char o = { 0 }; char h[10] = { 0 }; //FUCNTION CALL-OUT header(); printf("Type anything : "); xgets(h, sizeof(h), stdin); printf("Enter one option from the following : \n\n"); printf("(a) To Print out Hotels of a given Grade in order of charges. \n"); printf("(b) To Print out Hotels with Room Charges less than a given Value. \n"); printf("Please type a proper option. \n"); while (n == 0){ scanf_s(" %c", &o); switch (o){ case 'a': sortm_hgrade(); n = 1; break; case 'b': sortm_rcharge(); n = 1; break; default: printf("Option INVALID \n"); printf("Please type a proper option \n"); n = 0; break; } } //TERMINAL-PAUSE system("pause"); } void sortm_hgrade() { //FOR SORTING BY GRADE char g[10] = { 0 }; printf("Enter the Grade : "); xgets(g, sizeof(g), stdin); printf("\n"); } void sortm_rcharge() { printf("----"); }

I've made a user-defined function for reading input and replacing newline character '\n' with '\0' so when I use printf statement for printing the string it won't add newline at the end.

char xgets(char *line, int size, FILE *stdn) { //READS THE LINE fgets(line, size, stdn); //REMOVES NEWLINE CHARACTER '\n' AND ADDS '\0' line[strcspn(line, "\n")] = '\0'; return line; }

When I call xgets inside main() function it works properly, but when it is called in other user-defined function it does not wait for user-input.

I'm using Visual Studio 2015 for debugging my code.

Here's my code:

#include<stdio.h> #include<stdlib.h> #include<process.h> //USER-DEFINED FUNCTION char xgets(char *line, int size, FILE *stdn); void sortm_hgrade(); void sortm_rcharge(); void header(void); void header(void) { printf("*-*-*-*-*HOTEL_INFO*-*-*-*-*"); printf("\n\n"); } char xgets(char *line, int size, FILE *stdn) { //READS THE LINE fgets(line, size, stdn); //REMOVES NEWLINE CHARACTER '\n' AND ADDS '\0' END LINE CHARACTER line[strcspn(line, "\n")] = '\0'; return line; } #define MAX 1000 //PROGRAMS STARTS HERE int main(void) { //VARIABLE-DECLARATION int i = 0, j = 0, n = 0; char line[MAX] = { 0 }; char o = { 0 }; char h[10] = { 0 }; //FUCNTION CALL-OUT header(); printf("Type anything : "); xgets(h, sizeof(h), stdin); printf("Enter one option from the following : \n\n"); printf("(a) To Print out Hotels of a given Grade in order of charges. \n"); printf("(b) To Print out Hotels with Room Charges less than a given Value. \n"); printf("Please type a proper option. \n"); while (n == 0){ scanf_s(" %c", &o); switch (o){ case 'a': sortm_hgrade(); n = 1; break; case 'b': sortm_rcharge(); n = 1; break; default: printf("Option INVALID \n"); printf("Please type a proper option \n"); n = 0; break; } } //TERMINAL-PAUSE system("pause"); } void sortm_hgrade() { //FOR SORTING BY GRADE char g[10] = { 0 }; printf("Enter the Grade : "); xgets(g, sizeof(g), stdin); printf("\n"); } void sortm_rcharge() { printf("----"); }

最满意答案

你应该改变

scanf(" %c", &o);

scanf("%c ", &o);

此强制scanf消耗尾随字符,如'\ n'

在您的代码'\n' ,用户输入的scanf %c不被消耗,并且它被xgets函数中的fgets消耗,该函数立即使用空缓冲区退出。

BTW该解决方案只有在用户输入单个字符时才能进行。 最好的代码是

char c; while (n == 0) { o = getchar(); while ((c = getchar()) != EOF && c != '\n') ;

编辑

使用第二个解决方案代码正在等待并丢弃,直到'\n'被触发或文件结束为止。 在您的特定情况下(使用stdin作为控制台)EOF不是必需的。 在从“真实文件”读取输入的情况下,它是强制性的。

You should change

scanf(" %c", &o);

to

scanf("%c ", &o);

This force scanf to consume trailing chars, like '\n'

In your code '\n' of user input for scanf %c is not consumed and it is consumed by fgets in your xgets function that exit immediately with an empty buffer.

BTW that solution can wok only if a single char is input by user. Best code would be

char c; while (n == 0) { o = getchar(); while ((c = getchar()) != EOF && c != '\n') ;

EDIT

With the second solution code is waiting, and discarding, chars until a '\n' is triggered or end of file. In your specific case (using stdin as console) EOF is not mandatory. It will be mandatory in case of input is being read from a "real file".

更多推荐

本文发布于:2023-07-06 07:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047458.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   定义   功能   用户   User

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!