I'm having trouble with splitting a string with sstream. It appears when the loop of cout happens, the last item of data, an integer in my case, is repeated? Is my loop set up wrong??
This is my code:
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
string inputText("Jane Smith 18");
istringstream iss(inputText);
while (iss)
{
string first;
string last;
int age;
iss >> first >> last >> age;
cout << first << " " << last <<" "<< age << endl;
}
system("pause");
return 0;
}
See the picture - showing how the 'age' variable seems to be repeated. What am I doing wrong?
Thanks for your help in advance!
Console-Example
Related
Why this doesn't print the first word of sentence?
#include <iostream>
#include <string>
int main()
{
std::string sentence;
std::cout<<"Enter sentence: ";
std::cin>>sentence;
std::getline(std::cin,sentence);
std::cout<<sentence;
return 0;
}
If I enter
"This is text"
output would be
" is text"
You dont need the first cin (std::cin>>sentence;), this will solve your problem
#include <iostream>
#include <string>
int main()
{
std::string sentence;
std::cout<<"Enter sentence: ";
std::getline(std::cin,sentence);
std::cout<<sentence;
return 0;
}
std::cin>>sentence;
This line of code takes the first word you input.
Remove it and you are good to go
The final element in the vector is the char to search for.
Here’s my code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector<string> words;
string in;
while(cin>>in)
{
words.push_back(in);
}
int size = words.size()
string check = words.at(size-1);
}
I tried your code and the loop was infinite. Try asking a set number of words then use the find method for each string. Here's an example.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> words;
string in;
cout << "Enter 5 words: \n";
for (int i{0}; i < 5; ++i)
{
cout << "Word: ";
cin >> in;
words.push_back(in);
}
cout << "What character do you want to search for? ";
char c;
cin >> c;
for (auto word : words)
{
if (word.find(c) != string::npos)
{
cout << "Character \"" << c << "\" found in " << word << endl;
}
}
return 0;
}
Edit: I noticed that you tried to use int to store the size for the string. Use size_t instead. I also think you meant to use the length method of the string, which also returns size_t.
I want to separate a full name from each other. It only works for first name.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string FullName;
int i = 0;
cout <<"Enter your full name "<<endl;
getline(cin,FullName);
while (FullName[i] != ' ')
{
cout<<FullName.substr(i,FullName.find(' '))<<endl;;
i++;
}
cout <<endl;
}
return 0
}
I want to separate each name in a separate line like this: If I enter this:
Max Michael Max
the output should be with each name in a separate new line:
Max
Michael
Max
How can I split names each one in separate line ?
The simplest approach is to use std::istringstream after the name is read in.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string FullName;
cout <<"Enter your full name "<<endl;
getline(cin, FullName);
string namepart;
istringstream strm(FullName);
while ( strm >> namepart )
cout << namepart << '\n';
}
Live Example
I'm new to C++.
In a part of a simple project, I have to enter a number and verify that it is positive and not an alphanumeric sequence. With the following code I perform the "check for alphanumeric sequence" but in which way I can add the "positive check"?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string getline()
{
string str;
getline(cin, str);
return str;
}
int main()
{
int choice;
istringstream iss(getline());
iss >> choice >> ws;
if(iss.fail() || !iss.eof())
{
cout << "Error" << endl;
}
}
Thank you in advance!!
So I am trying to get the user to input their name and height.
I have got other code.
I have got this.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int name1;
cout << "What's your name?";
cin >> name1;
int height1;
cout << "What's your height?";
cin >> height1;
return 0;
}
The issue is that it won't allow the user to enter their height. Any ideas?
The Problem is, that you're using int variables instead of std::string. However, you already included the <string> header file, so you probably want to do this:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
std::string name1;
cout << "What's your name?";
cin >> name1;
std::string height1;
cout << "What's your height?";
cin >> height1;
return 0;
}
Otherwise it only works if you enter integer numbers - but that doesn't make much sense for an 'name' input.
Edit: If it's also your requirement to input names with whitespaces, you could use std::getline
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
std::string name1;
cout << "What's your name?";
getline(cin, name1);
std::string height1;
cout << "What's your height?";
getline(cin, height1);
return 0;
}