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.
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: ");
}