LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2008, 08:21 PM   #1
darkangel29
Member
 
Registered: Nov 2004
Location: Puerto Rico
Distribution: Ubuntu 10.04
Posts: 121

Rep: Reputation: 15
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.
 
Old 10-09-2008, 06:40 AM   #2
Nightfish
Member
 
Registered: Sep 2008
Posts: 36

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

Fedora Development

Last edited by Nightfish; 10-22-2008 at 12:51 PM.
 
Old 10-09-2008, 08:05 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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');
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with data validation and cin darkangel29 Programming 2 09-14-2008 08:23 PM
cin.get() question JMJ_coder Programming 7 03-10-2008 02:54 PM
cin in cpp , milky.way Programming 4 12-08-2007 10:16 AM
Catch 22 at cin entz Programming 16 03-28-2007 04:39 AM
cin.getline adam_boz Programming 6 10-02-2002 08:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration