473,406 Members | 2,698 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,406 software developers and data experts.

How do I get the flush function to work?

For this problem I know I need some kind of flush function to fix it, I have a program that runs as a calculator and everything works fine accept if I input a character the program does an infinite loop because its expecting a float, how do I get it to where the program continues on without the infinite loop, here is my code.

Expand|Select|Wrap|Line Numbers
  1. // Program 3
  2. // 10/17/2006
  3. // This program is a calculator
  4.  
  5. #include <iostream>
  6. #include <cmath>
  7. #include <string>
  8. #include <stdio.h>
  9.  
  10.  
  11. using namespace std;
  12.  
  13.  void instructions ()
  14.  
  15. // Repeated instructions for the user
  16. {
  17. cout << "Type your operation then the number;" << endl; 
  18. }
  19.  
  20. void main ()
  21. {
  22. float accum, num;
  23. char op;
  24. op = 0;
  25. accum = 0.0;
  26.  
  27.  
  28. // Beggining instructions for the user
  29.  
  30. cout << "type * for multiplication;" << endl;
  31. cout << "type / for division;" << endl;
  32. cout << "type - for subtraction;" << endl;
  33. cout << "type + for addition;" << endl;
  34. cout << "type q, Q, or = to quit;" << endl;
  35.  
  36. // Calculator process
  37.  
  38. while ((op!='q')&& (op!='Q') && (op!='='))
  39. {
  40. instructions ();
  41.  
  42. cin >> op;
  43. if (op!='q'&& op!='Q' && op!='=')
  44. {    switch (op)
  45.  
  46.         // The operations for the calculator
  47.  
  48.         {
  49.         case '+':
  50.             cin >> num;
  51.             accum = accum + num;
  52.             cout << accum << " " << endl;
  53.             break;
  54.         case '-':\
  55.             cin >> num;
  56.             accum = accum - num;
  57.             cout << accum << " " << endl;
  58.             break;
  59.         case '*':
  60.             cin >> num;
  61.             accum *= num;
  62.             cout << accum << " " << endl;
  63.             break;
  64.         case '^':
  65.             cin >> num;
  66.             accum = pow(accum, num);
  67.             cout << accum << " " << endl;
  68.             break;
  69.         case '/':
  70.         cin >> num;
  71.         if (num == 0) 
  72.         {
  73.             // If the user inputs zero it won't crash
  74.  
  75.  
  76.                 cout << "Error: You Can't divide by a '0'.";
  77.                 cout << endl << endl; accum;
  78.         } if (num != 0)
  79.                { 
  80.  
  81.  
  82.                 accum = (accum / num);
  83.                 cout << accum << " " << endl;
  84.             }
  85.                 break;
  86.         default:
  87.             cout << "invalid operation" << endl;
  88.  
  89. } }
  90.         if (num == '\n' && op != 'q' && op != 'Q' && op != '=')
  91.           {cout << "Invalid input" << endl; }
  92.              num = '\n';
  93.  
  94.              if (op =='q'|| op == 'Q' || op == '=')
  95.              {cout << "Thanks for using my program " << endl;}
  96.  
  97. }
  98. system("pause");
  99. }
Apr 6 '07 #1
2 2418
arne
315 Expert 100+
For this problem I know I need some kind of flush function to fix it, I have a program that runs as a calculator and everything works fine accept if I input a character the program does an infinite loop because its expecting a float, how do I get it to where the program continues on without the infinite loop, here is my code.

// Program 3
// 10/17/2006
// This program is a calculator

#include <iostream>
#include <cmath>
#include <string>
#include <stdio.h>


using namespace std;

void instructions ()

// Repeated instructions for the user
{
cout << "Type your operation then the number;" << endl;
}

void main ()
{
float accum, num;
char op;
op = 0;
accum = 0.0;


// Beggining instructions for the user

cout << "type * for multiplication;" << endl;
cout << "type / for division;" << endl;
cout << "type - for subtraction;" << endl;
cout << "type + for addition;" << endl;
cout << "type q, Q, or = to quit;" << endl;

// Calculator process

while ((op!='q')&& (op!='Q') && (op!='='))
{
instructions ();

cin >> op;
if (op!='q'&& op!='Q' && op!='=')
{ switch (op)

// The operations for the calculator

{
case '+':
cin >> num;
accum = accum + num;
cout << accum << " " << endl;
break;
case '-':\
cin >> num;
accum = accum - num;
cout << accum << " " << endl;
break;
case '*':
cin >> num;
accum *= num;
cout << accum << " " << endl;
break;
case '^':
cin >> num;
accum = pow(accum, num);
cout << accum << " " << endl;
break;
case '/':
cin >> num;
if (num == 0)
{
// If the user inputs zero it won't crash


cout << "Error: You Can't divide by a '0'.";
cout << endl << endl; accum;
} if (num != 0)
{


accum = (accum / num);
cout << accum << " " << endl;
}
break;
default:
cout << "invalid operation" << endl;

} }
if (num == '\n' && op != 'q' && op != 'Q' && op != '=')
{cout << "Invalid input" << endl; }
num = '\n';

if (op =='q'|| op == 'Q' || op == '=')
{cout << "Thanks for using my program " << endl;}

}
system("pause");
}

Expand|Select|Wrap|Line Numbers
  1. cin.clear();
  2.  
will clear your input buffer and prevent the infinite loop.
Apr 6 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
cin.clear() does not flush the input buffer. All it does is reset the failbit buit the offending data is still in the buffer. Use cin.ignore() to skip or cin.get() to eat it.

C++ does not own the input buffer.You may need an operating system call to flush the input buffer. Microsoft uses __flushall().

The input buffer accumulates characters until the enter key is pressed. Only then is data accessible to your program. When you reach the enter key, the program will ask for more data forever. The only way to terminate the input is to put an "end of all data" terminator in the buffer. This is a CTRL+Z. (Note: This has to be a CTRL+Z followed by an enter key)
Apr 6 '07 #3

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

Similar topics

1
by: alex | last post by:
I need your help, please. The following 2.asp is a demo for flush function. It works. BUT when I put it in a frame, it dosen't work. Is any IE limitation, something else. Alex ...
7
by: Jon Spivey | last post by:
Experimenting with response.flush() but can't seem to get it to work. This code <% response.write "<p>started</p>" response.flush() for i = 1 to 10000000 i = i +1 next response.write...
1
by: Victor Irzak | last post by:
Hello, I have a g++-3.3.1 compiler (latest). I can't figure the type of endl, ends, flush. Here is my program. It compiles with VS.NET, Intel C++ compiler, but not with g++. If you can...
30
by: Jonathan Neill | last post by:
I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge) way of flushing stdin or any other application-level input buffer, but if anyone knows a hack or a non-portable way to...
11
by: Darklight | last post by:
is this correct or incorrect i just read the post below before posting this: In fflush(stdin), what happens to flushed data? this program is taken from sams teach yourself c in 21 days /*...
11
by: Junkguy | last post by:
I need some help programmatically causing a row in a DataGrid to "flush" its contents to its bound data (in Visual Studio 6 using Windows Forms with C#). My issue is I want to send an update to...
5
by: Luiz Vianna | last post by:
Guys, I need to send some info to my client while I'm processing some stuff. The flow will be something like : -process -response -process -response .... I imagine to use response.flush...
4
by: Mantas Miliukas | last post by:
Hi, I have problem when flushing the generated HTML code to the client. It seems that "Page.Response.Flush()" method doesn't work at all. See my code below: protected override void...
0
by: jose.mendez22 | last post by:
I'm trying to fire a pop-up window before I execute a lengthy stored procedure so I may utilize this window as a status window on number of records executed. After my response.write statements...
10
by: Peter Larsen [] | last post by:
Hi, Is is possible to programmatically flush all opened files ?? BR Peter
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.