【发布时间】:2015-07-04 10:54:42
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j;//count
int c;//for EOF test
int menu;
unsigned int firstSize = 12811;
unsigned int lastSize;
char * text=malloc(firstSize);
if(text == NULL){
printf("\n Error. no Allocation.");
exit(-1);
}
printf("\n for input 1e,for autoinput press 2.");
scanf("%d",&menu);
if(menu==1){
printf("enter text..");
c = EOF;
i = 0;
lastSize = firstSize;
while (( c = getchar() ) != '\n' && c != EOF)
{
text[i++]=(char)c;
//if i reached maximize size then realloc size
if(i == lastSize)
{
lastSize = i+firstSize;
text = realloc(text, lastSize);
}
}
这是问题所在的代码部分。
当我为scanf 输入1 时的输出是:
for input 1e,for autoinput press 2.
1
enter text..
它不允许我为getchar() 提供输入。
但是当我为menu 删除scanf 并使用menu=1; 时,我可以轻松地为getchar() 提供输入,它可以正确地给我输出:
printf("\n for input 1e,for autoinput press 2.");
scanf("%d",&menu);
而不是那个
printf("\n for input 1e,for autoinput press 2.");
//scanf("%d",&menu);
menu=1;
是关于printfscanf我不知道的问题吗?在java中,在进行第二次输入之前,我们需要放一些空白。是这样的吗?
【问题讨论】:
-
发布代码 with 给出问题的 scanf。请对齐代码。
-
你知道这段Java代码有什么问题吗:
Scanner input = new Scanner(System.in); String str1 = input.nextLine(); String str2 = input.nextLine();?如果是这样,这是同样的问题。