sstream not populating vector C++ - c++

With input in the command line:
1 2 3
Which is stored in 'line' my vector is only being populated with
1
What am I doing wrong?
Here is the code
string line;
string buffer;
int a,b,base;
cin >> line;
stringstream ss(line);
std::vector<string> tokens;
while( ss >> buffer){
tokens.push_back(buffer);
}
for(int i=0; i<tokens.size(); i++){cout << tokens[i] << endl;}

Your problem is here:
cin >> line;
Note that this function
operator>>(istream& is, string& str)
gets all characters until the first occurrence of whitespace (in the case of input 1 2 3, it stops on the space after 1)
Try using the function getline(), which reads the string up until the first occurrence of a newline.
This seems to work:
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main(void) {
string line;
string buffer;
int a,b,base;
getline(cin, line);
stringstream ss(line);
vector<string> tokens;
while( ss >> buffer){
tokens.push_back(buffer);
}
for(int i=0; i<tokens.size(); i++){cout << tokens[i] << endl;}
return 0;
}

Related

getline() skipping over string line from text file

My project is supposed to basically sum up the value of all ints on a line and print the word in the next line that amount of times. However, something is causing it to skip over the string line where the word but I am getting the summed value right.
Input file:
1,2,3
word
2,3,4
word2
Here is my code:
int main() {
std::ifstream in;
std::ofstream out;
std::string line;
in.open("input.txt");
out.open("output.txt");
while(std::getline(in, line)){
std::stringstream ss(line);
while(ss){
std::string word;
std::string number;
int a = 0;
while(std::getline(ss, number, ',')){
a = a + atoi(number.c_str());}
std::getline(ss, word);
for (int z = 0; z < a; z++){
out << word << ",";}
out << "\n";
}
}
return(0);
}
Output I'm getting:
,,,,,,
,,,,,,,,,
What I should be getting:
word,word,word,word,word,word
word2,word2,word2,word2,word2,word2,word2,word2
Change:
std::getline(ss, word);
To this:
// Gets the next line in input.txt and stores it in the variable 'word'
std::getline(in, word);
This is because during the while loop, ss is emptied (exhausted) by the nested while loop (the 2nd while loop) and has no words when it's contents are inserted into the variable 'word'.
Final code:
#include <iostream>
#include <fstream>
#include <sstream>
int main() {
std::ifstream in;
std::ofstream out;
std::string line;
in.open("input.txt");
out.open("output.txt");
while (std::getline(in, line)) {
std::stringstream ss(line);
while (ss) {
std::string word;
std::string number;
int a = 0;
while (std::getline(ss, number, ',')) {
a = a + atoi(number.c_str());
}
std::getline(in, word); // Changed line
for (int z = 0; z < a; z++) {
out << word << ",";
}
out << "\n";
}
}
return(0);
}

c++ ifstream to stringstream using getline()

I am making a bowling program for school that stored scores in a text file with the format:
paul 10 9 1 8 1, ...etc
jerry 8 1 8 1 10 ...etc
...etc
I want to read the file into a stringstream using getline() so I can use each endl as a marker for a new player's score (because the amount of numbers on a line can be variable, if you get a spare or strike on round 10). I can then read the stringstream using >> to get each score and push it into a vector individually.
However, when trying to use getline(fstream, stringstream), I get an error
no instance of overloaded function "getline" matches the argument list -- argument types are: (std::fstream, std::stringstream)
How can I make this work?
My code looks like this:
#include <iostream>
#include <vector>
#include <fstream>
#include <exception>
#include <string>
#include <sstream>
using namespace std;
//other parts of the program which probably don't matter for this error
vector <int> gameScore;
vector<string> playerName;
int i = 0;
int j = 0;
string name;
int score;
stringstream line;
while (in.good()){ //in is my fstream
playerName.push_back(name);
cout << playerName[i] << " ";
i++;
getline(in, line);
while (line >> score){
gameScore.push_back(score);
cout << gameScore[j] << " ";
j++;
}
}
You can't use std::getline() to read from a std::ifstream directly into a std::stringstream. You can only read into a std::string, which you can then assign to the std::stringstream, eg:
vector<int> gameScore;
vector<string> playerName;
string name, line;
int score;
while (getline(in, line)){
istringstream iss(line);
iss >> name;
playerName.push_back(name);
cout << name << " ";
while (iss >> score){
gameScore.push_back(score);
cout << score << " ";
}
}

Read array of objects from file by custom separator

I have model class for my objects:
class customClass {
string s1;
string s2;
string s3;
}
and file like this:
text1;text1;text1 text1 text1...
text2;text2;text2 text2 text2...
...
and I want make array of objects where
s1 = "text1"
s2 = "text1"
s3 = "text1 text1 text1..."
...
My code:
infile.open("file.txt");
if (infile.is_open())
{
string line;
for (int i = 0; i < 3; i++)
{
infile >> line;
stringstream ss(line);
while (ss.good())
{
string substring;
getline(ss, substring, ';');
cout << substring <<endl;
}
}
}
But it separated every single word. How can I ignore whitespaces to make my 3rd string as text not as single word.
The reason it doesnt work for you is because infile >> line; will read up to the first space character instead of the whole line (this is when you need getline). Maybe something like this:
#include <string>
#include <iostream>
#include <fstream>
int main()
{
std::ofstream outfile("file.txt");
outfile <<
R"(text1;text1;text1 text1 text1...
text2;text2;text2 text2 text2...)";
outfile.close();
// read file
std::ifstream infile("file.txt");
std::string par1, par2, par3;
while (std::getline(infile, par1, ';') && std::getline(infile, par2, ';') && std::getline(infile, par3))
std::cout << par1 << " | " << par2 << " | " << par3 << std::endl;
}
Demo: http://coliru.stacked-crooked.com/view?id=eb52001b5d4ecbed
Read all line with getline function. Default >> operation get values until \n, (space) character.
#include <iostream>
#include <fstream>
#include <string.h>
#include <sstream>
using namespace std;
class CustomClass
{
public:
string s1;
string s2;
string s3;
};
int main()
{
ifstream infile;
infile.open("test.txt");
if (infile.is_open())
{
while (!infile.eof())
{
string line;
getline(infile, line);
stringstream ss(line);
CustomClass cls;
getline(ss, cls.s1, ';');
getline(ss, cls.s2, ';');
getline(ss, cls.s3, ';');
cout << cls.s1 << " - " << cls.s2 << " - " << cls.s3 << endl;
}
}
return 0;
}
Just don't read single string at the begining, also I added the loop until the end of file, so your code would look like:
std::ifstream infile("file.txt");
std::string line;
if (infile.is_open())
{
string line;
while (std::getline(infile, line)) {
std::stringstream ss(line);
string substring;
while (getline(ss, substring, ';')) {
cout << substring <<endl;
}
}
}

Iterating through a string with sstream

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;
}

segmentation fault error when parsing string to pointer array with stringstream

I am reading line by line from a text file whose contents are separated by commas and parsed by extracting with getline() into my stringColor, stringName, stringReward variables, passed into my stringstream ss, and then passed to my tileArray pointer array into respective int, string, and int variables.
My program compiles, however when I run it, it generates a Segmentation Fault 11 to what appears to be where I pass the line contents into stringstream. I cannot find where the problem is however...
Perhaps if someone could point out where the error is, I would be greatly appreciative.
This is the format of each line I am trying to read in from the text file.
It should be able to read in any number of lines.
0,tile 1,5
4,tile 2,0
2,tile 4,1
#include <stdio.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct
{
int color;
string name;
int reward;
}tile;
int main()
{
string line;
int numberOfLines = 0;
ifstream inputFile("inputFile.txt");
if (inputFile.is_open())
{
while(getline(inputFile, line))
{
++numberOfLines; //value to set tile amount
cout << numberOfLines <<endl;
}
tile *tileArray = new tile[numberOfLines];
string stringColor, stringName, stringReward; //declare these values as strings and later convert
stringstream ss; //stringstream variable to convert string variable
for(int n = 0; n<(numberOfLines-1); n++)
{
getline(inputFile, stringColor, ','); //delimiter at first comma
cout << stringColor << endl;
getline(inputFile, stringName, ','); // delimiter at second
cout << stringName << endl;
getline(inputFile, stringReward); // stop at the end of the line
cout << stringReward << endl;
ss<<stringColor;
ss>>tileArray[n]->color;
ss.str("");
ss.clear();
cout << tileArray[n]->color;
ss<<stringName;
ss>>tileArray[n]->name;
ss.str("");
ss.clear();
cout << tileArray[n]->name;
ss<<stringReward;
ss>>tileArray[n]->reward;
ss.str("");
ss.clear();
cout << tileArray[n]->reward;
}
}
return 0;
}
I would simplify the use of the stringstream object. Using the same stringstream object as an input stream and output stream requires a deep understanding of how the internal position is manipulated.
{
// Create a nested block and a local istringstream in the nested scope
istringstream ss(stringColor);
ss >> tileArray[n]->color;
}
cout << tileArray[n]->color;
Similarly,
{
istringstream ss(stringName);
ss >> tileArray[n]->name;
}
cout << tileArray[n]->name;
and
{
istringstream ss(stringReward);
ss >> tileArray[n]->reward;
}
cout << tileArray[n]->reward;