By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 2,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

Problem clearing buffer, could use some help.

P: 2
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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. main ()
  4.  
  5. {
  6.  
  7.  
  8.      fputs("This program will calculate a table showing the voltage,"
  9.            "total resistance, and current for each increment of "
  10.            "resistance given the input from the user\n\n", stdout);
  11.  
  12.  
  13.      int ch, reply = 'y';
  14.      double initcur, endcur, inccur, voltage, current;
  15.  
  16.  
  17.        while ( reply == 'y' )
  18.  
  19.  
  20.      {
  21.          fputs("Enter voltage: ", stdout);
  22.          scanf("%lf", &voltage);
  23.          fputs("Enter initial resistance value: ", stdout);
  24.          scanf("%lf", &initcur);
  25.          fputs("Enter ending resistance value: ", stdout);
  26.          scanf("%lf", &endcur);
  27.          fputs("Enter incremental resistance value: ", stdout);
  28.          scanf("%lf", &inccur);
  29.  
  30.          fputs("Table of induced currents\n\n", stdout);
  31.          fputs("     Voltage     Total Resistance     Current\n\n",
  32.          stdout);
  33.          fputs("     ----------------------------------------\n\n",
  34.          stdout);
  35.  
  36.  
  37.          for (initcur; initcur <= endcur ; initcur += inccur )
  38.          {
  39.            current = voltage / initcur ;
  40.  
  41.            printf("        %.2f            %.4f        %.4f\n",
  42. voltage, initcur, current);
  43.          }
  44.  
  45.        fputs("\nEnd of table.\n\n", stdout);
  46.  
  47.        fputs("Would you like another computation? ", stdout);
  48.        while ((reply = getc(stdin)) !='\n');
  49.        scanf("%c", &reply);
  50.  
  51.  
  52.       }
  53.  
  54.        return 0;
  55.  
  56. }
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. :)
Feb 13 '07 #1
Share this Question
Share on Google+
1 Reply


AdrianH
Expert 100+
P: 1,251
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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. main ()
  4.  
  5. {
  6.  
  7.  
  8.      fputs("This program will calculate a table showing the voltage,"
  9.            "total resistance, and current for each increment of "
  10.            "resistance given the input from the user\n\n", stdout);
  11.  
  12.  
  13.      int ch, reply = 'y';
  14.      double initcur, endcur, inccur, voltage, current;
  15.  
  16.  
  17.        while ( reply == 'y' )
  18.  
  19.  
  20.      {
  21.          fputs("Enter voltage: ", stdout);
  22.          scanf("%lf", &voltage);
  23.          fputs("Enter initial resistance value: ", stdout);
  24.          scanf("%lf", &initcur);
  25.          fputs("Enter ending resistance value: ", stdout);
  26.          scanf("%lf", &endcur);
  27.          fputs("Enter incremental resistance value: ", stdout);
  28.          scanf("%lf", &inccur);
  29.  
  30.          fputs("Table of induced currents\n\n", stdout);
  31.          fputs("     Voltage     Total Resistance     Current\n\n",
  32.          stdout);
  33.          fputs("     ----------------------------------------\n\n",
  34.          stdout);
  35.  
  36.  
  37.          for (initcur; initcur <= endcur ; initcur += inccur )
  38.          {
  39.            current = voltage / initcur ;
  40.  
  41.            printf("        %.2f            %.4f        %.4f\n",
  42. voltage, initcur, current);
  43.          }
  44.  
  45.        fputs("\nEnd of table.\n\n", stdout);
  46.  
  47.        fputs("Would you like another computation? ", stdout);
  48.        while ((reply = getc(stdin)) !='\n');
  49.        scanf("%c", &reply);
  50.  
  51.  
  52.       }
  53.  
  54.        return 0;
  55.  
  56. }
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
Feb 14 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.