相关疑难解决方法(0)

即使使用fflush(stdin),程序也不会在scanf()之后执行gets()

在使用scanf()之后浪费了太多时间搜索为什么我的程序没有执行gets(),我找到了一个解决方案,即在scanf()之后使用fflush(stdin)来启用gets()来获取字符串.

问题是fflush(stdin)没有做到它所期望的:程序继续跳过gets(),我不能在控制台中写任何短语来读取.

我的代码是下一个:

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

int main(){
    char nombre[10];
    char mensaje[80];

    printf("Type your name:\n");
    scanf("%s", nombre);

    fflush(stdin);

    printf("Now, type a message:\n");
    gets(mensaje);

    printf("3/%s:%s",nombre,mensaje);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c gets scanf fflush

1
推荐指数
1
解决办法
8394
查看次数

fflush()函数不能与stdin一起使用

对不起这个愚蠢的问题我很抱歉.我有C程序提示用户输入年龄和名称,然后将年龄和名称打印到屏幕上.这是我从书中读到的练习.

这个程序:

#include <stdio.h>

int main (void) {
   int age;
   char name[20];

   puts("Enter your age:");
   scanf("%d",&age);

   fflush(stdin);

   puts("Enter your name:");
   scanf("%s",name);

   printf("Your age is %d\n",age);
   printf("Your name is %s\n",name);

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我向第一个输入额外的字符时,scanf()程序终止并将额外的字符分配给下一个scanf()

然后我更改了代码,并添加了命名函数clear_buff()并使用其中的fgets函数clear_buff()来读取流上的剩余字符.代码按照我的预期工作.

#include <stdio.h>

#define MAXLEN 80

void clear_buff(void);

int main (void) {
  int age;
  char name[20];

  puts("Enter your age:");
  scanf("%d",&age);

  clear_buff();

  puts("Enter your name:");
  scanf("%s",name);

  printf("Your age is %d\n",age);
  printf("Your name is %s\n",name);

  return 0;
}

void clear_buff(void){ …
Run Code Online (Sandbox Code Playgroud)

c stdin fflush

1
推荐指数
1
解决办法
259
查看次数

while 循环代码不起作用(keepgoing='y')

所以我正在学习如何在 C 中使用 while 和 for 循环,但这段代码似乎不起作用。scanf 语句似乎被忽略了,循环只是重复自己而不需要我输入“Y”来重复。这是代码:

void showCommission();

void main() {
    char keepGoing='y';
    while(keepGoing=='y') {
        showCommission();
        printf("Do you want to calculate another?\n");
        scanf("%c",&keepGoing);
   }
}

void showCommission() {
    float sales,commission;
    const float COM_RATE=0.10;
    printf("Enter the amount of sales\n");
    scanf("%f",&sales);
    commission=sales*COM_RATE;
    printf("The commission is $%f.\n",commission);
}
Run Code Online (Sandbox Code Playgroud)

这是运行代码给我的内容:

Enter the amount of sales                                                                         
5000                                                                                              
The commission is $500.000000.                                                                    
Do you want to calclulate another?    

...Program finished with exit code 10                                                             
Press ENTER to exit console.  
Run Code Online (Sandbox Code Playgroud)

它从不提示我输入 y 并且代码只是出于某种原因退出。

c loops scanf while-loop

1
推荐指数
1
解决办法
216
查看次数

C中的Do-While循环 - 打印时出错

我正在制作一个小程序,将"你是一个成年人吗?"这个问题的答案作为输入.像这样的角色:

bool adult() {
    char answer;
    do {
        printf("Are you an adult? [y/n]\n");
        answer = getchar();
    } while (!(answer == 'y' || answer == 'n'));

    return (answer == 'y');
}
Run Code Online (Sandbox Code Playgroud)

我的目标是,如果答案既不是y,那么问题应该重复.但这似乎有一个错误:

当我回答其他内容(y或n)时,问题会被打印两次:

你是成年人吗?你是成年人吗?你是成年人吗?...

为什么会这样?另外,我尝试使用scanf而不是getchar的相同方法,但是存在相同的错误.对于这种程序,我应该使用scanf或getchar吗?为什么?

谢谢!

c printf do-while

0
推荐指数
1
解决办法
488
查看次数

C中的简单循环代码

这个程序中的循环不能像我期望的那样工作:

int main() {
    char userresponse;
    char x;

    do {
        printf("are you human");
        scanf("%c", &userresponse);
        if (userresponse == 'y') {
            printf("welcome");
        }
        if (userresponse == 'y') {
            printf("please leave");
        }
        printf("type z to repeat");
        scanf("%c", &x);
    } while (x == 'z');

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它得到"键入z重复",然后它完成.当我尝试定义char x='z';程序时第一次运行正常,但最后它循环播放2或4次,在语句中显示所有printf消息,甚至是else那些消息if.

我希望程序等待循环底部的输入,并打破循环或继续基于该输入循环.为什么不这样做?

c loops while-loop

0
推荐指数
1
解决办法
130
查看次数

一维数组是作为字符串列表还是单个字符串工作?

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

int main()
{
    int i;
    char a[10];
    for(i=0;i<10;i++)
    {
        scanf("%s",a);// >how this line is working in memory.
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我想知道字符串是如何保存在内存中的,因为我已经将它初始化为1D字符数组,但是数组是作为字符串列表还是单个字符串工作?为什么?

c

-4
推荐指数
1
解决办法
259
查看次数

标签 统计

c ×6

fflush ×2

loops ×2

scanf ×2

while-loop ×2

do-while ×1

gets ×1

printf ×1

stdin ×1