Tag: getchar

scanf / getchar只在第一次循环时正常工作?

我试图让用户输入一个他们想要的次数(并为每个数字创建一个链表节点)。 但是,我尝试了多种清除字符输入缓冲区的方法,但无济于事。 奇怪的是,代码将执行一次但不能正确执行第二次。 例如,使用下面的代码,终端读取: would you like to enter an integer? y Enter an integer: 4 would you like to enter an integer? y **program terminates** 在我使用scanf(“%c”, yesno); 我甚至不能在最后一行输入’y’。 它刚刚终止。 struct node *read_numbers(void){ struct node *first = NULL; int n; char yesno; yesno = ‘y’; while( yesno == ‘y’){ printf(“Would you like enter an integer […]

为什么getchar()在printf语句后读取’\ n’?

我提示用户输入数组的长度,使用此输入初始化char []数组,然后提示用户输入要输入char []数组的消息。 我正在使用getchar()读取用户消息的第一个字符。 但是, getchar()在读取任何用户输入之前正在读取换行符’\n’ 。 它似乎是从前一个提示用户的printf语句中获得’\n’ … 这是相关的代码: #include int main(void) { int len = 0, originalLen = 0; printf(“\n\nWhat is the length of the array? “); scanf(“%d”, &originalLen); char str[originalLen]; // intitializing the array printf(“Enter a message to enter into the array: “); char target = getchar(); str[len] = target; // why […]

使用getchar()输入字符串

以下代码使用getchar()接受一行输入。 #include #include int main() { char *rawString = (char *)malloc(200*sizeof(char)); char *rawStringInitial = rawString; char c; c=getchar(); while(c!=’\n’) { *rawString=c; rawString++; c=getchar(); } *rawString=’\0′; printf(“\n[%s]\n”,rawStringInitial); return(0); } 键入时,如果我按退格键,它不应该被getchar()接收并存储在rawString指向的位置吗? 但是输出只显示没有任何特殊字符的最终字符串。 有人可以解释原因吗?

为什么这个语句在while循环中打印两次?

我写了这个简单的练习程序: #include #include #include #define CLASSES 3 #define STUDENTS 4 int grades[CLASSES][STUDENTS]; int main(void) { int i = 1; char t,k; while(i == 1) { printf(“\n\n\nMENU:\nEnter the grades(E)\nReport Grades(R)\nQuit(Q)\nYour choice: “); k = toupper(getchar()); printf(“Input entered… %c\n”, k); switch(k) { case ‘E’ : printf(“Entering the grades..\n”); break; case ‘R’ : printf(“Reporting the grades…\n”); break; case […]

对getchar()函数感到困惑

我对以下代码中getchar()的作用感到困惑。 我的意思是我知道它帮助我看到输出窗口,只有在按下Enter键时才会关闭。 所以getchar()基本上等着我按回车然后再读一个字符。 这个函数读取的单个字符是什么? 我没有按下键盘上的任何键来阅读。 现在当它没有读任何东西时,为什么它没有给出错误说“嘿,你没有输入任何东西供我阅读”? #include int main() { printf( “blah \n” ); getchar(); return 0; }

getchar和putchar

我的C代码: int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } 为什么这个程序在输入hello会这样反应? hello hello 而不是像: hheelloo

getchar()跳过C中的所有其他char

我在C中完全使用getchar()已经有一段时间了。在这种情况下,我试图读取一行并将该行的char放入数组中。 但是,在将getchar()分配给数组时,它会跳过一些字符。 例如,输入“它会相互跳过”,输出是…… \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n (\ n只是代表新行。) int N = 0; char message[80] = {}; do { […]

C – getchar()在循环中?

我如何在循环中使用getchar()? 我现在有… for (p=0; p<n_players; p++) { … fflush(stdin); getchar(); } 但它不起作用……如果n_players为3,它只在最后执行getchar 2次… for (p=0; p<n_players; p++) { blank_start(); ascii_art_title(); printf("%s, tocca a te…\n",player_info[p].player_name); srand(time(NULL)); random_speed = MIN_WHEEL_SPEED + rand()%MAX_WHEEL_SPEED; move_wheel_pointer(random_speed, &pointer); if (player_points(&wheel[pointer]) == 0){ player_info[p].points = wheel[pointer]; } else { player_info[p].points = 0; } printf("\nGuadagni %d punti…\n",player_info[p].points); if (p<(n_players-1)) { printf("\nOra tocca a […]

使用bool编译c代码而不使用c99标准

我试图在C中使用bool变量编译代码并且我已经包含了stdbool头但是当我编译它时我没有指定我想用c99标准编译它(因此它是用ANSI C编译的)标准)但无论如何它都有效。 我想知道为什么会这样? 这是代码: #include #include int main() { char name[20]; printf(“What’s your name ? “); gets(name); printf(“Nice to meet you %s.\n”, name); bool exit = false; char c; printf(“Do you wish to exit the program ? (Y/N) “); while (!exit) { c = getchar(); if (c == ‘\n’) { continue; } printf(“Do you wish […]

C中的for循环和getchar()

为什么代码会在偶数时间直接获取空数据? 我不知道发生了什么事。 非常感谢你。 #include #pragma warning(disable : 4996) void main() { int f, a = 10, b = 20; for (int i = 0; i < 5; i++) { char ch; ch = getchar(); printf("ch = %c\n", ch); switch (ch) { case '+': f = a + b; printf("f = %d\n", f); break; case '−': […]