LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 03-28-2007, 04:15 AM   #16
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453

Original Poster
Blog Entries: 3

Rep: Reputation: 40
Cool I've got the solution by myself , but thanks anyway


After dedicating a full time for this Catch 22 , I got the remedy to the problem.
googling , testing ,testing ,testing and testing Is the ultimate recipe in this business (and most definitly in any other field as well)
Hell , This is C++ programming ! you either fire up your compiler and probe the code or you can just rename your title to anything else than a "coder"

well , someone before just asked me to giving him a note when I find a smarter solution .

wait , should i license that?? ....nah no need...
So here it's , have fun and educate yourselves! :

Code:
#include <iostream>
using namespace std;

int main(){
	char x[10];
	
	for(;;)
	{
		cout<<"Enter Message:";
		cin.getline(x,10);
		if(cin.fail()){
			cin.clear();
                        //I'm the magic line 
			cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
		}
		cin.clear();
		cout<<"Message is:"<<x<<endl;
	}
	return 0;
}
according to the 2003 standard update (and how i hate them)

Edit:
Ooops , someone else just post this before me , should have clicked "post" quicker LOL

Last edited by entz; 03-28-2007 at 04:19 AM.
 
Old 03-28-2007, 04:39 AM   #17
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by entz
well , someone before just asked me to giving him a note when I find a smarter solution .
Actually it was you who asked...

The important point is that the code was doing what it should. Because you wanted a different behaviour it was necessary to tell the stream to ignore the remaining characters.

I'm glad that you managed to find a solution so that your code will behave the way you want it to.

Last edited by graemef; 03-28-2007 at 04:40 AM.
 
  


Reply

Tags
getline, ignore



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
C++::std::cin adilturbo Programming 3 07-03-2006 11:22 AM
How to cin >> string? kornerr Programming 2 11-01-2005 03:58 AM
cin.ignore Xiangbuilder Programming 4 10-08-2003 07:57 PM
g++ and cin.getline() codeviking Programming 8 07-03-2003 07:42 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 01:56 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