Here is a piece of code:
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
stringstream ss;
string st = "2,3,55,33,1124,34";
int a;
char ch;
ss.str(st);
while(ss >> a)
{
cout << a << endl;
ss >> ch;
}
return 0;
}
It produces the output:
2
3
55
33
1124
34
But if I remove the line ss >> ch it produces the output: 2.
Why does it stop iterating through the string? And what difference does ss >> ch make?
What difference does ss >> ch make?
ss >> ch takes a character from your stream an store it in your char ch variable.
So here it removes each and every comma (,) from your string.
Why does it stop iterating through the string without ss >> ch?
Without this operation, your iteration stops because ss >> a fails, since it tries to store a comma inside a, an int variable.
Note: If you replace your commas with spaces, you could get rid of ss >> ch, since a space is recognized as a separator.
Example:
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
stringstream ss;
string st = "2 3 55 33 1124 34";
int a;
ss.str(st);
while (ss >> a)
cout << a << endl;
return 0;
}
You can also use this if you like to keep the commas
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
stringstream ss;
string st = "2,3,55,33,1124,34";
std::string token;
ss.str(st);
while(getline(ss, token, ',')) {
cout << token << endl;
}
return 0;
}
Related
When I input [1,0,0,1,0] I get output with 1 0 0 1 0 0, I don't know why there is an extra zero and why the while loop doesn't terminate after ].
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
int main()
{
std::string str;
std::istringstream is;
std::vector<int> numbers;
std::getline(std::cin, str);
is.str(str);
char ch;
is >> ch;
while (!is.eof())
{
int num;
is >> num >> ch;
numbers.push_back(num);
}
for (auto num : numbers)
std::cout << num << " ";
std::cout << std::endl;
return 0;
}
Change the while loop part will get expected behavior:
int num;
while (is >> num >> ch) {
numbers.push_back(num);
}
The eof check is misused here, so the last character read failed and get a default number 0. Read this answer for more details:
https://stackoverflow.com/a/4533102/1292791
I am working on a project where I have to parse data from user input.
#include <iostream> // for cin and cout
#include <iomanip> // for setw()
#include <cctype> // for toupper()
using namespace std;
int main(){
string playerInput;
cin >> playerInput;
//Player would input strings like C13,C 6, I1, Z 16, etc...
}
return 0;
I've tried something like this, which kinda works but only if the letter proceeds the number in the string.
int myNr = std::stoi(playerInput);
What my end goal is to grab the letter and number from the string, and place them in a char variable and a integer variable respectively. I am stuck on how to proceed from here and could use some help, thanks!
This is the simplest and the shortest way to achieve that (it also ignores spaces and tabs):
int main() {
char ch;
int n;
cin >> ch >> n;
cout << "ch = " << ch << ", n = " << n << endl;
}
I think that other answers are a bit overcomplicated.
You could do like what you had:
char letter = playerInput.front();
playerInput.erase(0);
int number = std::stoi(playerInput);
Of course, that doesn't allow for spaces. Removing spaces can be quite tedious, but it could be done like:
playerInput.erase(
std::remove_if(
begin(playerInput), end(playerInput),
[](uint8_t ch) { return std::isspace(ch); }),
end(playerInput));
Full Demo
Live On Coliru
#include <cctype> // for toupper()
#include <iomanip> // for setw()
#include <iostream> // for cin and cout
#include <algorithm> // for remove_if
static bool ignorable(uint8_t ch) {
return std::isspace(ch)
|| std::ispunct(ch);
}
int main() {
std::string playerInput;
while (getline(std::cin, playerInput)) {
playerInput.erase(
std::remove_if(
begin(playerInput), end(playerInput),
ignorable),
end(playerInput));
if (playerInput.empty())
continue;
char letter = playerInput.front();
playerInput.erase(begin(playerInput));
int number = std::stoi(playerInput);
std::cout << "Got: " << letter << " with " << number << "\n";
}
}
Prints
Got: C with 13
Got: C with 6
Got: I with 1
Got: Z with 16
You have the right idea in using std::stoi. My code expands your approach:
string playerInput;
getline(cin, playerInput);
char c1 = playerInput[0];
int num = stoi(playerInput.substr(1));
The above code receives an input string, then takes out the first character and uses std::stoi on the rest of the string.
Note that I use std::getline to account for the possibility of there being spaces in the input. If you are doing this repeatedly, you will need to add cin.ignore() after each getline() statement. See this link for more info.
std::cin stops reading input when it encounters a space. You can use std::getline() if your input has spaces. To parse your string, you should check out std::stringstream. It allows you to read from a string as if it were a stream like std::cin.
#include <iostream> // for cin and cout
#include <iomanip> // for setw()
#include <cctype> // for toupper()
#include <sstream>
int main(){
std::string playerInput;
int i;
char c;
std::getline(std::cin, playerInput); // Remove trailing newline
std::getline(std::cin, playerInput);
//Player would input strings like C13,C 6, I1, Z 16, etc...
//String Stream
std::stringstream playerInputStream(playerInput);
//Read as if you were reading through cin
playerInputStream >> c; //
playerInputStream >> i;
}
return 0;
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main() {
string str;
getline(cin,str);
stringstream ss(str);
vector<int> arr;
while(!ss.eof()){
int num;
char ch;
ss>>num>>ch;
arr.push_back(num);
}
for(int i=0;i<arr.size();i++){
cout<<arr.at(i)<<endl;
}
return 0;
}
I am getting output for 1,2,3,4,5 as
1
2
3
4
5
but for 1 2 3 4 5 it is
1
3
5
why? space is also a character so it should work or am I missing something?
thank you for helping.
Because formatted input operations skip whitespaces. Thus the following happens:
ss >> num // reads integer 1
>> ch; // skips whitespace after 1 and reads char '2'
In the next iteration:
ss >> num // skips whitespace after 2 and reads integer 3
>> ch; // skips whitespace after 3 and reads char '4'
And the last iteration:
ss >> num // skips whitespace after 4 and reads integer 5
>> ch; // Encounters eof, nothing is read
Don't read that char for space-seperated lists. Or You can use std::noskipws to change this behaviour.
Extraction operator “>>” provides reading space separated integers without reading a separator by:
ss >> num;
instead of additional reading of separator in original code:
ss >> num >> ch;
because for standard streams, the skipws flag is set on initialization.
And this makes more simple reading space separated integers.
To make both separators working similar add
ss >> noskipws;
as in following code:
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main () {
string str;
getline (cin, str);
stringstream ss (str);
ss >> noskipws;
vector<int> arr;
while (!ss.eof ()) {
int num;
char ch;
ss >> num >> ch;
arr.push_back (num);
}
for (int i = 0; i < arr.size (); i++) {
cout << arr.at (i) << endl;
}
return 0;
}
So guys, Actually What I wanna do here is that when I input 3,12,36 the output will be:
3
12
36
But here I have difficulty on how to make it output all the answer. What I have been doing is that when you input 3,12,36 it will output 3 12 only and if you type 3,12,36,48 it will output 3 12 36.
So it will always miss the last integer because my while loop is not correct I guess. but if I change it into
while(output >> life|| output >> ch)
It doesn't work either. I've done a lot of research but it still makes me confused and I'm still stuck on this part.
vector<int> parseInts(string str) {//23,4,56
vector<int>lifeishard;
stringstream output;
string lifeisgood = str;
output.str(lifeisgood);
int life;
char ch;
while(output >> life >> ch){
lifeishard.push_back(life);
//lifeishard.push_back(life2);
//lifeishard.push_back(life3);
}
return lifeishard;
}
int main() {
string str;
cin >> str;
vector<int> integers = parseInts(str);
for(int i = 0; i < integers.size(); i++) {
cout << integers[i] << "\n";
}
return 0;
}
On your last number, the while loop fails because there's no character at the end. Just the end of the string. So it doesn't execute the push_back inside the loop.
Change it so that the while loop just gets the number. Then do the push_back in the loop. Then in the loop, after the push, get the comma character. Don't bother checking for failure getting the comma because when it goes around the while loop again it will fail and exit.
I changed to using getline in your main. I changed your loop index to size_t because it is never a good idea to mix signed and unsigned integers, and whenever you use a size() function, it's a size_t. When posting your program it really should include everything. My fixed up version of your program:
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
vector<int> parseInts(string str) {//23,4,56
vector<int>lifeishard;
stringstream output;
string lifeisgood = str;
output.str(lifeisgood);
int life;
char ch;
while(output >> life){
lifeishard.push_back(life);
output >> ch;
}
return lifeishard;
}
int main() {
string str;
getline(cin, str);
vector<int> integers = parseInts(str);
for(size_t i = 0; i < integers.size(); i++) {
cout << integers[i] << "\n";
}
// Here is how we do for loops over containers in modern C++
for(auto x: integers) {
cout << x << '\n';
}
return 0;
}
A combination of stringstream, getline with delimiter and stoi would be enough for the conversion:
From the C++ reference for getline with delimiter:
Extracts characters from is and stores them into str until the delimitation character delim is found.
With this in mind, the code example below assumes the input is well-formed:
Example
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<int> parseInts(const string& str, const char delim = ',')
{
vector<int> parsed;
stringstream ss(str);
string s;
while (getline(ss, s, delim)) // <- stores input in s upon hitting delimiter
parsed.push_back(stoi(s)); // <-- convert string to int and add it to parsed
return parsed;
}
int main()
{
string str = "3,12,36"; // <-- change to cin if you'd like
vector<int> ints = parseInts(str);
for (auto& i : ints)
cout << i << "\n";
}
Output
3
12
36
See more: getline, stoi
I'm not able to figure out why the loop in following program is not running exactly testCount times. Please help to make it correct.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
size_t testCount;
cin >> testCount;
if(testCount < 0 || testCount > 100) return 0;
int input;
while(testCount--) {
string instr;
getline(cin,instr);
istringstream iss(instr);
while(iss >> input) {
cout << input << endl;
}
}
return 0;
}
Thanks. I got it. The problem is with getline(). First loop cycle is getting wasted as getline() is taking first line containing just new line character when I pressed enter key after typing testCount value.
std::ws is an input stream manipulator which ignores all whitespaces to the point where the first non-whitespace character is encountered.
Also, getline leaves whitespaces where they are if they don't fit in the line. cin >> ws will discard those.
Here's the bullet proof code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
size_t testCount;
cin >> testCount >> ws;
if(testCount < 0 || testCount > 100) return 0;
int input;
while(testCount--) {
cout << "testCount " << testCount << endl;
string instr;
cin >> ws;
getline(cin,instr);
istringstream iss(instr);
while(iss >> input) {
cout << input << endl;
}
}
return 0;
}