-
getchar() problem.
I'm trying to use getchar with scanf and I have this bug here that ignores the first whitespace character of the first loop. YES I want to use scanf and getchar TOGATHER in order to understand it better and NO i don't want to use fgets. :D
I'm just curious what the bug is. I know its like < line of code:
Code:
/* This code prints out a multiplication table based on the user's input
* it uses scanf and test for the num of return arguments so there is no undefined
behaviour. It works on windows XP compiled with dev c++ 4.9.8.0. to port it simply
remove the system calls. however you won't have the pretty interface ;)
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXCLM 20 /* MAX number of clms (horizontal integers) */
#define MAXCHOICE 40 /*MAX number of rows so no bigger than the screen */
#define FLUSH while ( (getchar() !='\n'));
void Winconsole(void);
int main(void){
int choice, row, clm;
Winconsole();
/* problems: ignores first input unless its a 'q' */
while ( (getchar()) != 'q'){
if( (scanf("%d", &choice) !=1) || choice > MAXCHOICE || choice < 1){
FLUSH
printf("Invalid integer. Unable to print to screen\n");
printf("Try again: ");
}
else {
for (row = 1; row <= choice; row++){
for (clm = 1; clm <= MAXCLM; clm++){
printf("%4d", row * clm);
}
printf("\n");
}//end of outer if
printf("Enter another integer or [q] to quit: ");
}//end of else
}// end of while
return 0;
}//end of main
void Winconsole(){
system("TITLE Multiplication Table 1.0 by bitshadow.");
system("COLOR 64");
printf("What Multiplication table would you like: ");
}
-
Can you explain your problem better? All I see is a fairly typical problem with scanf and getchar where a newline is left in the stream, causing it to appear as if the call to getchar was ignored.
-
You've hit it on the head. I'm just trying to understand how to make the two work togather. As of now, scanf seems only well suited to being terminated properly when its a number.
Is there a way to make it work? with the existing code.
-
>Is there a way to make it work? with the existing code.
Add a FLUSH in the else block.
-
okay i'm lost.
I've checked the program state. but i'm still having problems. where in the else block??? :confused:
-
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXCLM 20 /* MAX number of clms (horizontal integers) */
#define MAXCHOICE 40 /*MAX number of rows so no bigger than the screen */
#define FLUSH while ( (getchar() !='\n'));
void Winconsole(void);
int main(void){
int choice, row, clm;
Winconsole();
/* problems: ignores first input unless its a 'q' */
while ( (getchar()) != 'q'){
if( (scanf("%d", &choice) !=1) || choice > MAXCHOICE || choice < 1){
FLUSH
printf("Invalid integer. Unable to print to screen\n");
printf("Try again: ");
}
else {
FLUSH // Here
for (row = 1; row <= choice; row++){
for (clm = 1; clm <= MAXCLM; clm++){
printf("%4d", row * clm);
}
printf("\n");
}//end of outer if
printf("Enter another integer or [q] to quit: ");
}//end of else
}// end of while
return 0;
}//end of main
void Winconsole(){
system("TITLE Multiplication Table 1.0 by bitshadow.");
system("COLOR 64");
printf("What Multiplication table would you like: ");
}
-
I put it there origianlly, however it doesn't fix the problem. it still ignore s the first input . :eek:
-
>it still ignore s the first input
You realize that you need to enter a single character to satisfy getchar before typing a number for scanf, right? ;) Otherwise it would appear as if a single digit number is being ignored. Give it an input of 12 when the program starts. The table printed will only be for 2 because getchar eats the 1.
-
Yeah I understand that. that's the prob i've been having. :d
but how does the FLUSH call solve it. and how can i solve it using getchar and scanf. or give me a bone or something ;)
-
>but how does the FLUSH call solve it.
It doesn't. :) That's a logic problem, not a getchar/scanf mix problem. Adding the FLUSH "fixes" the getchar/scanf mix problem, but the logic problem still exists. To fix it you would need to change the structure of your loop a bit:
Code:
while (1){
if( (scanf("%d", &choice) !=1) || choice > MAXCHOICE || choice < 1){
FLUSH
printf("Invalid integer. Unable to print to screen\n");
printf("Try again: ");
}
else {
int ch;
FLUSH // Here
for (row = 1; row <= choice; row++){
for (clm = 1; clm <= MAXCLM; clm++){
printf("%4d", row * clm);
}
printf("\n");
}//end of outer if
printf("Enter another integer or [q] to quit: ");
if ((ch = getchar()) != 'q') {
ungetc(ch, stdin);
}
else {
break;
}
}//end of else
}// end of while
-
Thank you ma'am.
I think. I guess you get bored with simplistic problems sometimes you need to have a little fun.
I understand :p
-
>I guess you get bored with simplistic problems sometimes you need to have a little fun.
The who and the what now? :confused: