Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 28th, 2006, 2:35 PM   #1
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
stringstream expects a space at the end of input?

I've this li'l program.
#include<iostream>
#include<string>
#include<sstream>
#include<cstdlib>
using namespace std;

//function to print the error message and terminate the program.
void err_quit(string msg)
{
	cerr<<msg;
	exit(1);
}

int main()
{
	char line[200],rest[200];
	string line1;
	stringstream ss;
	char token;
	int id;
	while(true)
	{
		
		cin.getline(line,200);
		
		if(!cin.good())
			err_quit("Cin failed");
		
		line1=line;
		ss.str(line1);
		ss>>rest;
		if(!ss.good())
			err_quit("ss failed at rest");
		cout<<rest<<endl;
	}
return 0;
}

Sample run:
234 a aaa
234
aaa a aaa
aaa
aaa<space>
aaa
aaa
ss failed at rest.

Don't know what's wrong but it expects a space after the word if I enter only one word.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Apr 28th, 2006, 2:55 PM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Something like this suit you better?:
#include <iostream>
#include <string>
#include <sstream>

// TM DaWei
void uhOh(std::string TroubleInRiverCity)
{
	std::cerr << TroubleInRiverCity << std::endl;
	exit(1);
}

int main()
{
    std::string myString;
    while (getline ( std::cin, myString ))
    {
        std::istringstream iss(myString);
        iss >> myString;
        if (!iss)
            std::cout << "Bad input" << std::endl;
        else
            std::cout << "You entered: " << iss.str() << std::endl;
    }
    return 0;
}
Of course you can make it a normal stringstream instead of just an input stringstream, and/or put the stringstream outside the loop.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 28th, 2006, 8:20 PM   #3
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
I made a few changes to your program(underlined).
#include <iostream>
#include <string>
#include <sstream>

// TM DaWei
void uhOh(std::string TroubleInRiverCity)
{
	std::cerr << TroubleInRiverCity << std::endl;
	exit(1);
}

int main()
{
    std::string myString;
    std::istringstream iss;

    while (std::getline ( std::cin, myString ))
    {
        iss.str(myString);
        iss >> myString;
        if (!iss)
            std::cout << "Bad input" << std::endl;
        else
            std::cout << "You entered: " << myString << std::endl;

	}
    return 0;
}
Sample run:
aaaa<space>
You entered: aaaa
bbb
You entered: bbb
ccc
Bad Input.
ddd
Bad Input.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Apr 28th, 2006, 8:49 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I went to the doc and said, "Hey, Doc, my back hurts when I bend over to pick up my shoes!" He jes' said, "W'al.....don't DO that....".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 29th, 2006, 2:20 AM   #5
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,193
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by DaWei
I went to the doc and said, "Hey, Doc, my back hurts when I bend over to pick up my shoes!" He jes' said, "W'al.....don't DO that....".
Or the old guy goes to the doctor, and says, "Doc, I can't pee!"

"How old are you?" asks the doctor.

"Eighty-seven!" replies the man.

"Well," says the doctor, "haven't you peed enough?"
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old Apr 29th, 2006, 8:46 AM   #6
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Well this is probably a very bad way of doing it, but it works
#include <iostream>
#include <sstream>

// TM DaWei
void uhOh(std::string TroubleInRiverCity)
{
	std::cerr << TroubleInRiverCity << std::endl;
	exit(1);
}

int main()
{
	std::string myString;
	std::istringstream iss;

	while (std::getline ( std::cin, myString ))
	{
		if(myString == "")
		{
			std::cout << "Enter some text" << std::endl;
			continue;
		}
			iss.str(myString);
		iss >> myString;

		if (!iss)
		{
			uhOh("Bad input");

			if(iss.bad())
				uhOh("Very bad");
		}

		else
		{
			std::cout << "You entered: " << iss.str() << std::endl;
			iss.clear(); // probably very bad!!
		}
	}
	return 0;
}
If someone knows how to do it right, then please say so.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:40 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC