473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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:

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
1 1933
AdrianH
1,251 Expert 1GB
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

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

Similar topics

2
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer =...
5
by: William Payne | last post by:
How do you get rid the contents in a std::stringstream? I'm using one in a for loop to format some status messages which I print to the screen. The stringstream member function clear() only clears...
6
by: Carol Pedder | last post by:
Hello all, may I ask a question? Having just started C++ (using microsoft visual studio), I am using cin/cout for console applicarions (yes I know it doesn't happen in the real world!) and I'm...
4
by: news.eircom.net | last post by:
Hello, How do you clear the contents of an ostringstream object? I've included some example code so you can see what I mean. t.i.a. Craig H. #include <iostream>
2
by: vikas | last post by:
hi, I want to generate random numbers in c++ (using vc++ 6.0) on hit of enter. I am using _kbhit(), but don't know how to reset it, so that I can use it again, can anybody help me with this. code...
28
by: Terry Andersen | last post by:
I have an array that I initialize to zero, like: Buffer = {0x00}; How do I in my code reset this array to all zeros ones more? Without running a whole for loop? Best Regards Terry
4
by: Roubles | last post by:
Hi All, Quick question; what is the difference between initializing and clearing a structure like this: mystruct_t a = {0}; and initializing and clearing it like this:
12
by: Avalon1178 | last post by:
Hi, I have an application that periodically uses a std::string variable which is assigned a VERY VERY large string (15000000+ bytes long). This application is essentially a daemon, and it polls...
9
by: kardon33 | last post by:
Hi all, What i am trying to accomplish is take in a list of IP addresses from a text file (each Ip is on a new line). I got my program mostly working except when an IP address is longer than...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.