473,794 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem clearing buffer, could use some help.

2 New Member
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 1952
AdrianH
1,251 Recognized Expert Top Contributor
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
7499
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 = "value1" + " : " + "value2";
5
817
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 flags (bits) in the stream, right? I solved it by declaring the stringstream inside the loop body even though I have need of another stringstream just after the loop. So for each iteration the constructor of the stringstream will be executed. Is...
6
31077
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 finding it difficult to track down a method of clearing the input buffer after a cin (to get rid of potential rubbish). cin.flush() has been mentioned, but isn't an option on visual studio and cin.ignore() requires you to specify number of chars...
4
7490
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
2145
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 is say like following. #include <iostream> #include <conio.h> #include <windows.h>
28
16637
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
9536
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
4292
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 a data set which can have a lot of information and it is concatenated in this single string variable object. When the daemon finishes its job, it goes to sleep, but before doing so, it "clears" this variable so it can be reused again in the next...
9
2808
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 the last one it keeps the extra numbers beacuse im having trouble clearing the IP.
0
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.