How to print string in C++ using getline - c++

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

Related

How to accept an unknown number of lines in c++, each line has two strings

How do I accept an unknown number of lines in c++? Each line has two strings in it separated by a space. I tried the solutions mentioned in This cplusplus forum, but none of the solutions worked for me. One of the solutions works only when Enter is pressed at the end of each line. I am not sure if the \n char will be given at the end of my input lines. What are my options?
My current attempt requires me to press Ctrl+Z to end the lines.
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
int main(){
string line;
while(cin>>line and cin.eof()==false){
cout<<line<<'\n';
}
return 0;
}
I would like to take an unknown number of strings as shown below:
cool toolbox
aaa bb
aabaa babbaab
Please don't flag this as a duplicate, I really tried all I could find! I tried the following solution on the above given link by m4ster r0shi (2201), but it did not work for me.
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> words;
string word;
string line;
// get the whole line ...
getline(cin, line);
// ... then use it to create
// a istringstream object ...
istringstream buffer(line);
// ... and then use that istringstream
// object the way you would use cin
while (buffer >> word) words.push_back(word);
cout << "\nyour words are:\n\n";
for (unsigned i = 0; i < words.size(); ++i)
cout << words[i] << endl;
}
And this other solution also did not work: other soln, and I tried this SO post too: Answers to similar ques. This one worked for my example, but when I pass only one line of input, it freezes.
// doesn't work for single line input
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
int main(){
string line ="-1";
vector<string>data;
while(1){
cin>>line;
if(line.compare("-1")==0) break;
data.push_back(line);
line = "-1";
}
for(int i =0;i<data.size();i+=2){
cout<<data[i]<<' '<<data[i+1]<<'\n';
}
return 0;
}
If each line has two words separated by whitespace, then perhaps you should have a Line struct which contains two std::strings and overloads the >> operator for std::istream.
Then you can just copy from std::cin into the vector.
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
struct Line {
std::string first;
std::string second;
};
std::istream& operator>>(std::istream& i, Line& line) {
return i >> line.first >> line.second;
}
int main() {
std::vector<Line> lines;
std::copy(
std::istream_iterator<Line>(std::cin),
std::istream_iterator<Line>(),
std::back_inserter(lines)
);
for (auto &[f, s] : lines) {
std::cout << f << ", " << s << std::endl;
}
return 0;
}
A test run:
% ./a.out
jkdgh kfk
dfgk 56
jkdgh, kfk
dfgk, 56

splitting words for full name in only one variable

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

How do we read the first character of a string input?

For example, a user enters John as his name and i want to print just the first letter which is 'J'. How do we do that?
Thanks in advance..✌✌
Use std::cin to get name, and use operator [] to access character you want. First character has index 0. So first letter of name is name[0]
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cin >> name;
if (!name.empty())
{
std::cout << name[0];
}
}
Also check if string is not empty.
You can use std::string::at function
#include <iostream>
#include <string>
int main ()
{
std::string str ("Test string");
std::cout << str.at(0);
}

C++ Input validation (double control)

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!!

Last item of istringstream repeats?

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