LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   cin and do while problems (https://www.linuxquestions.org/questions/programming-9/cin-and-do-while-problems-675201/)

darkangel29 10-08-2008 08:21 PM

cin and do while problems
 
This a code for a calculator I'm doing.

main.cpp
Code:

#include <string>
#include "Equation.h"
using namespace std;

int main()
{
        Equation a;
        string expresion;
        char option;
       
        do{
                cout<<"\tMathematic Expression Calculator" << endl;
                cout<<endl;;

                cout<<"\ta) Enter Expression" << endl;
                cout<<"\tb) Help" << endl;
                cout<<"\n\tc) EXIT" << endl;
                cout<<endl;
                cout<<"\tOption: ";
                cin>>option;
               
                switch(option)
                {

                case 'a':
                       
                        system("cls");
                        cout<<"Enter the expression: ";
                        getline(cin,expresion);
                        a.set(expresion);
                        a.rpn();
                        system("pause");
                        system("cls");
                        break;

               
                case 'b':
                       
                        system("cls");
                        cout<<"This calculator calculates the result of an mathematic expression.";
                        cout<<"\nIt uses the methodology of postfix to help calculate this.";
                        cout<<"\nExample mathematic expression is 4+5*(7-5)"<<endl;
                        system("pause");
                        system("cls");
                        break;

                case 'c':
                       
                        system("cls");
                        cout<<"Have a nice day."<<endl;
                        break;

                default:
                       
                        system("cls");
                        cout<<"\n\tInvalid Option\n"<<endl;
                        break;

                }
        }while(option !='c');

        return 0;

}

The code works fine but the problem is that when i try to implement the do while loop as you can see and the user chooses a to enter a expression it just skips the getline completely. If I don't do the do while loop everything works great. What I'm doing wrong here? Thanks

I didn't include the Equation.cpp file because is a little to long and I know is working but if you needed let me know. Also I have tested this in MS Visual 2005 and MS Visual 2008.

Nightfish 10-09-2008 06:40 AM

did you try "cin.get(<string>,<string max lenght>);" ?

Fedora Development

dmail 10-09-2008 08:05 AM

The problem is due to characters being left on the input stream, after you get the required option ignore the rest of the stream.
Quote:

#include <limits>
...
cin>>option;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');


All times are GMT -5. The time now is 02:09 AM.