Need help using the flush function in my code?
Hi,
I'm currently working on a project for my programming class, I'm nearly done except for one flaw that I do not know how to fix.
I do not really know how the flush works but I plugged in to multiple parts of my functions and it has fixed a couple errors.
This code draws a diamond out of stars
__*
_***
*****
_***
__*
Here's my current problem, the first run of the program works fine but if the program asks me to run the program again, if I was to put a character instead of a number, the program will draw the last diamond that was made.
Here is the code, I feel like I am so close to finishing, if someone can tell me what I'm doing wrong that would be great thanks >.<
Code:
/*Creates a specifically sized Diamond*/
#include <stdio.h>
void flush(void){
while (getchar() != '\n');
}
int main(void){
char reply;
int a,b,d,x,acc,counter;
do {
int num;
printf("enter ODD size: ");
scanf("%i",&num);
flush();
while (num<1 || num%2==0){
printf("Error, input must be an odd integer greater than 0, input another number: ");
scanf("%i",&num);
flush();
}
b=-1,d=1,x=0;
counter = num/2;
for (x;x<((num/2)+1);x++){
for (a=0;a<counter;a++){
printf(" ");
}
for (acc=0;acc<num-((counter*2));acc++){
printf("*");
}
printf("\n");
counter-=1;
}
for (x=0;x<((num/2));x++){
for (a=0;a>counter;a--){
printf(" ");
}
for (acc=0;acc<num+(counter*2);acc++){
printf("*");
}
printf("\n");
counter -=1;
}
printf("more? ");
scanf("%c",&reply);
flush();
}while (reply=='y' || reply=='Y');
}