Thread: Need help using the flush function in my code?

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    4

    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');
    }
    Last edited by Aowei Chenqi; 05-10-2012 at 05:32 PM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    scanf has a return value that indicates the number of items it was able to read and convert. you should always check it. the second time you scanf("%i",&num) you aren't getting a value, so scanf fails and returns 0 and does not change the value of 'num'. this lets it fall through your validity test and reexecute the code innards of your loop.

    Code:
    status = scanf("%i",&num);
    if (status != 1) {
        // erroneous input do something (ask again or whatever)
    }

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    4
    Thank you, that helped, much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. flush stdout?
    By Cmaniac in forum C Programming
    Replies: 4
    Last Post: 05-19-2007, 02:14 AM
  2. Flush or something....
    By Inquirer in forum Linux Programming
    Replies: 0
    Last Post: 04-16-2003, 07:55 PM
  3. Flush !!
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2002, 07:44 PM
  4. How to flush the buffer
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-26-2002, 03:28 AM
  5. flush()?
    By onurak in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2002, 09:35 AM