- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 fgets 请求来自函数的输入。我不断收到一个烦人的错误,其中程序直接跳过输入并转到第二个变量的输入。我不知道问题出在哪里。有问题的代码如下。它从 getchar() 中读取,如果它是 'n',它就进入第二个函数。
#include <stdio.h>
void enter(){
char name[20];
int Age;
float Highbp;
float Lowbp;
printf("name: ");
fgets(name, 20, stdin);
printf("age: ");
scanf("%d", &Age);
printf("high bp: ");
scanf("%f", &Highbp);
printf("low bp: ");
scanf("%f", &Lowbp);
return ;
}
void option(){
char choice = getchar();
if(choice == 'n'){
enter();
}
}
int main(int argc, char **argv)
{
option();
}
产生的输出(不是全部输出):
>n
>name: age:
现在可以了
printf("name: ");
while(getchar()!='\n');
fgets(name, 20, stdin);
最佳答案
我没有运行你的代码,所以我只能猜测。这听起来很熟悉:
the program skips right over the inputand goes to input of the secondvariable.
它与这些问题有关:
The input stream after the first scanfcall still contains a \n, so thegets call reads it right away, withoutpausing for you to enter anythingmore. The problem is that the getscall satisfies its need for input inan unexpected way
所以它可能是一些剩余的 \n
某处。
我重新阅读了您的代码,我认为您的问题是:
char choice = getchar(); /* leaves a \n in the buffer */
关于c - 函数中 fgets 的格式化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6555877/
我有以下代码:(cpp14) static int const max_len = 1000; FILE* m_in_log = NULL; FILE* m_log = NULL; ... ... b
标准库函数 fgets() 有两个缺点: 函数的第二个参数是int类型 它在提供的缓冲区中留下一个尾随的换行符 我做了一个类似于 fgets() 的简单函数,排除了上述缺点,试图提高我的一个程序的效率
我看过各种关于在 fgets 之前使用 scanf 可能会导致问题的帖子。但是,对我来说,为什么在 AFTER 之后添加它会导致第一个语句中断,这是没有意义的。 这就是我所拥有的: unsigne
这可能是一个非常愚蠢的问题(可能),但我正在使用 fgets 处理文本文件。没什么太花哨的: while (fgets(buf, sizeof(buf), inputFile) != NULL) 正如
如果我想使用 fgets 从文本中读取多行,根据我的教科书,我会这样做: char str[53]; ... while(fgets(str, max, f)!=NULL){ ... }
我试图在完成未定义数量的另一个输入后获取用户输入。但问题是 while 循环之后的第二个 fgets 永远不会被调用。我用 EOF 结束循环,也许这就是错误。但我不知道应该如何结束循环。 另一个有趣的
我正在尝试将文件中的邮政编码读入 Object * 数组。文件包括123 无处不在柯克兰加州99223 我的 .h 文件看起来像 typedef struct { char *street;
我已经实现了我自己的 fgets(即 myfgets)。当我的文件中有必须由 myfgets 函数读取的 NULL 字符串时,它会打印所有字符串(好)但有一些垃圾(坏),但如果我使用预定义 fgets
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why does printf not flush after the call unless a newl
假设 FILE* 有效,请考虑: char buf[128]; if(fgets(buf,sizeof buf,myFile) != NULL) { strlen(buf) == 0; //ca
void verification() { char pass[50]; printf(" Enter Password : "); fgets(pass, 50, stdin
我想知道如何从汇编代码中调用fgets。我读到的这些问题与这个问题完全相同:How to call fgets in x86 assembly?这个:How to use c library func
我有一个程序,用户可以在其中输入两个输入。由于我无法控制用户输入的内容,因此用户可以超过数组的固定大小。由于 fgets() appends 在空字符之前保留了一个换行符,如果用户超出预期大小时换行符
我想使用 fgets() 简单地读取一行并去掉换行符。这是否适用于所有情况,包括 Windows 和 UN*X? fgets(buf, sizeof buf, stdin); strtok(buf,
我正在运行以下代码: #include #include #include int main(){ FILE *fp; if((fp=fopen("test.txt","r"))==N
如果我有以下缓冲区: char buffy[40]; // includes \0 at the end fgets 函数应该有 STLEN 40 还是 39?为什么? char buffy[40];
使用fgets输入字符串,我对读取的字符串长度有疑问。 例如,考虑以下程序。 char str[50]; int i; int len; printf("Enter name:\n"); fgets(
fgets()函数是否将文件指针自动移动到该位置,直到我提到的大小参数为止? 例如 : p.txt文件的内容是“我是个好男孩”。使用fgets(a,5,fp1)之后 文件指针会向前移动5个位置吗? 在
gcc 4.4.4 c89 我正在读取一个文件,在该文件中,我必须对每一行进行一些操作。 在一个循环中,我调用了 2 个函数,它们传入此文件指针以对文件执行 2 种不同的操作。 但是,当我使用 fge
我试图从文件中获取我的 9x9 数字网格并将其存储在 2D int 数组中。为此,我编写了代码,以便它使用文件中的 fgets(),然后将 char 数组值转换为 int,然后将其存储在数组中的相应位
我是一名优秀的程序员,十分优秀!