Problem clearing buffer, could use some help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kelharis
    New Member
    • Feb 2007
    • 2

    Problem clearing buffer, could use some help.

    Hello,

    I am writing a program that will run once, and once done, ask the user if they would like to use it again. It has to work with any answer beginning with "y". I am having trouble with clearing the buffer when I answer "yes" or anything other than "y" itself. It works perfectly when "y" is entered, now I just need to somehow accept the other yes words and clear the buffer once the "y" has been consumed. code is as follows:

    Code:
    #include <stdio.h>
    
    main ()
    
    {
    
    
         fputs("This program will calculate a table showing the voltage,"
               "total resistance, and current for each increment of "
               "resistance given the input from the user\n\n", stdout);
    
    
         int ch, reply = 'y';
         double initcur, endcur, inccur, voltage, current;
    
    
           while ( reply == 'y' )
    
    
         {
             fputs("Enter voltage: ", stdout);
             scanf("%lf", &voltage);
             fputs("Enter initial resistance value: ", stdout);
             scanf("%lf", &initcur);
             fputs("Enter ending resistance value: ", stdout);
             scanf("%lf", &endcur);
             fputs("Enter incremental resistance value: ", stdout);
             scanf("%lf", &inccur);
    
             fputs("Table of induced currents\n\n", stdout);
             fputs("     Voltage     Total Resistance     Current\n\n",
             stdout);
             fputs("     ----------------------------------------\n\n",
             stdout);
    
    
             for (initcur; initcur <= endcur ; initcur += inccur )
             {
               current = voltage / initcur ;
    
               printf("        %.2f            %.4f        %.4f\n",
    voltage, initcur, current);
             }
    
           fputs("\nEnd of table.\n\n", stdout);
    
           fputs("Would you like another computation? ", stdout);
           while ((reply = getc(stdin)) !='\n');
           scanf("%c", &reply);
    
    
          }
    
           return 0;
    
    }
    I am supposed to be using the getc function, not fflush, so if you can tell where I might be going wrong, I would me most appreciative. :)
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Originally posted by kelharis
    Hello,

    I am writing a program that will run once, and once done, ask the user if they would like to use it again. It has to work with any answer beginning with "y". I am having trouble with clearing the buffer when I answer "yes" or anything other than "y" itself. It works perfectly when "y" is entered, now I just need to somehow accept the other yes words and clear the buffer once the "y" has been consumed. code is as follows:

    Code:
    #include <stdio.h>
    
    main ()
    
    {
    
    
         fputs("This program will calculate a table showing the voltage,"
               "total resistance, and current for each increment of "
               "resistance given the input from the user\n\n", stdout);
    
    
         int ch, reply = 'y';
         double initcur, endcur, inccur, voltage, current;
    
    
           while ( reply == 'y' )
    
    
         {
             fputs("Enter voltage: ", stdout);
             scanf("%lf", &voltage);
             fputs("Enter initial resistance value: ", stdout);
             scanf("%lf", &initcur);
             fputs("Enter ending resistance value: ", stdout);
             scanf("%lf", &endcur);
             fputs("Enter incremental resistance value: ", stdout);
             scanf("%lf", &inccur);
    
             fputs("Table of induced currents\n\n", stdout);
             fputs("     Voltage     Total Resistance     Current\n\n",
             stdout);
             fputs("     ----------------------------------------\n\n",
             stdout);
    
    
             for (initcur; initcur <= endcur ; initcur += inccur )
             {
               current = voltage / initcur ;
    
               printf("        %.2f            %.4f        %.4f\n",
    voltage, initcur, current);
             }
    
           fputs("\nEnd of table.\n\n", stdout);
    
           fputs("Would you like another computation? ", stdout);
           while ((reply = getc(stdin)) !='\n');
           scanf("%c", &reply);
    
    
          }
    
           return 0;
    
    }
    I am supposed to be using the getc function, not fflush, so if you can tell where I might be going wrong, I would me most appreciative. :)

    You have to use getc to clear the buffer? Because you could use scanf("%*[^\n\r]%*[\n\r]"); The first format reads a line upto but not including the carraige return/line feed, the second reads all the carraige return/line feed after that.

    I've seen you use scanf so it shouldn't be prohibited or anything.

    Hope this helps,


    Adrian

    Comment

    Working...