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: - #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. :)
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: - #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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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";
|
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...
|
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...
|
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>
|
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>
| |
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
|
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:
|
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...
|
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.
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |