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 1933
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
| | |