How do I get the flush function to work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joder2006
    New Member
    • Dec 2006
    • 12

    #1

    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.

    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");
    }
    Last edited by Ganon11; Apr 6 '07, 12:51 PM. Reason: code tags added
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by joder2006
    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") ;
    }

    Code:
    cin.clear();
    will clear your input buffer and prevent the infinite loop.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      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)

      Comment

      Working...