![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
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;
}aaaa<space> You entered: aaaa bbb You entered: bbb ccc Bad Input. ddd Bad Input. ![]()
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 | |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,193
Rep Power: 5
![]() |
Quote:
"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 |
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|