How to print out a certain part of a string [duplicate] - c++

This question already has answers here:
How do I tokenize a string in C++?
(37 answers)
Closed 8 years ago.
EDIT:I see how it works now, in the string myString it takes everything after the space. So how would this be done if you wanted to take the first name and not the last name, since now you can't use the space if you do that.
I'd like to get the user to input their first and last name, then I'd like to print their last name only to the screen.
I know how to ask the user the question, and I was using getline to get the string of their name. But once that's happened how do I take their last name and print it out without printing out their first name as well. I can't seem to, I guess, isolate each part of the string.
I tried searching for an answer to this, but I can't seem to find one.
using namespace std;
int main()
{
string firstName;
string lastName;
cout << "Welcome to my store!" << endl;
cout << "---------------------------" << endl;
cout << "Please enter your first and last name. ";
getline(cin, firstName);
cout << "\nThank you " << firstName << " for shopping with us!";
}
I left the getline as is because I tried getline(cin, firstName, lastName) in an attempt to assign each word input by the user to a string but that didn't work.

I think this is what you are looking for(for printing the last name alone)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myString;
int i;
cout<<"\n Enter a name:";
getline(cin, myString, '\n');//Get a name where first name and last name is seperated by a space
i=myString.find(' ');//find the find occurance of space in that string
cout<<"thanks for shopping:";
cout<<myString.substr(i,myString.length());//printing the rest of the string from the occurence of space
return 0;
}
When you give input like 'Sachin Tendulkar'
It says 'thanks for shopping:Tendulkar'

Related

I have to write a c++ code that inputs a name in string and outputs it in a different order

//This program inputs a student's name in the following form: lastName, firstName middleName.
//The program will convert the name to the following form : firstName middleName lastName.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, str, substr;
int pos;
cout << "Enter a name in the following format: last name, first name middle name" << endl;
cin >> str.length (name);
pos = str.find(", ");
cout << "The students name is " << str.substr(pos + 1, str.length() - pos - 1) << str.substr(0, pos - 1) << endl;
return 0;
}
//This is what I have so far. I think that am stuck on the cin statement and I don't know what it should be. When I run the code, it doe not output a name.
Well ... even I think you don't have any clue what you're doing there, let's see where your mistakes are.
Let's take the first wrong part of your initial code.
cin >> str.length(name);
name has no value so far, so str.length() will return 0 but ... you want to get the name here. But what you're trying here is like 'Please get a input by user and write that user input to the method str.length(). Do you think that this sounds right?
So what you have to do is
cin >> str;
But now, let's help yourself and simply print, what the user has entered:
cin >> str;
cout << str;
When the user enters Doe, John Max the output will be Doe,. This is because the input stream just handles a whitespace like a symbol to stop reading. To get this working, search for std::getline and afterwards think about your usage of substr.

String not working with #include <string> and using namespace std

about the code below, string doesn't light up anymore and when I entered "John Smith", only "John" appears, string was working fine for me weeks ago until i tried calling strings function today which didn't work so i tested for a simpler one.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name;
// Get the user's name
cout << "Please enter your first name: ";
cin >> name;
// Print the greeting
cout << "Hello, " << name << "." << endl;
return 0;
}
string doesn't light up like int
I might be asking at the wrong place but I cant' tell what's the problem, please help :(
To get all the line, use getline(cin, name);
instead of cin >> name;
See http://www.cplusplus.com/reference/string/string/getline/
With std::string's, using std::cin >> someString will only read the first word off the buffer (it will stop at the first whitespace encountered).
Use getline(std::cin, someString) instead to read the entire line.
std::cin gets only characters to first 'white' character, like space, tab or enter.
If you want to read whole line use e.g. getline()
string line;
cin.clear(); //to make sure we have no pending characters in input buffer
getline(cin, line);

cin missing first word of line

I've been having issues in a program involving cin.
My problem is that the first word of everything I input appears to be skipped, possibly because of the way the buffer is handled. I have seen similar posts regarding this but trying to apply their fixes to my code have so far failed. What is supposed to happen is the user inputs a name and that name gets stored in a text file with other entered data. However, it always drops the first word.
#include "string"
#include "stdafx.h"
string _name;
int main()
{
cout << "Choose a name" << endl;
getline(cin, _name);
cout << _name;
ofstream dat;
dat.open("data.txt");
dat << _name;
dat.close();
return 0;
}
This code is where the problem appears to be. I just can't get it to take the first word.
cin >> _name;
This reads the first word on the first line of input into _name.
getline(cin, _name);
This will read the rest of the line into _name. This overwrites the existing contents of name.
Because this overwrites the existing contents of _name, which contains the first word read, this ends up reading all except the first word of the line, as you described.
If you just want to read the entire line into _name, the only thing that needs to be done is to remove the cin >> _name.
If you want to read a name from cin, then your code should look something like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string _name;
cout << "Choose a name : ";
getline(cin, _name);
cout << _name << endl;
// Do something with _name - write to file etc..
// ..
}

String Truncated for Some Reason in C++ [duplicate]

This question already has answers here:
Issue with cin when spaces are inputted, using string class
(3 answers)
Closed 8 years ago.
So, I was just starting C++, and I have a problem. I declared a string variable and all that, but when I have input give the variable a value, then try to display it, it only shows the first word of the sentence.
#include <iostream>
#include <string>
using namespace std;
using std::string;
int main()
{
string answer;
cout << "Give me a sentence and I will repeat it!";
cin >> answer;
cout << answer;
return 0;
}
For example, I entered "Yay it worked!", and it outputted "Yay"
The delimiter for std::cin is whitespace, so it only took the first word of your sentence. Like #πάνταῥεῖ said, use std::getline(cin,answer) instead.
As the comment explains, cin will only read until the first bit of whitespace is met (in your case this seems to be a space). Instead, you can use std::getline, which will read until a specified character, or a return by default:
std::string answer;
std::cout << "Give me a sentence and I will repeat it!";
std::getline(std::cin, answer):
std::cout << answer;
To make it read until a specified character would look like:
char end_char = 'a';
std::getline(std::cin, answer, end_char);

It won't print full address

Can somebody please tell me why it won't print my full address? The final output is "Your name is Syd and you live at 2845." Why is it doing this? I clearly state within the cout to print the address string. (by the way, I actually type 2845 SE Taylor Street)
#include <iostream>
using namespace std;
int main()
{
string address;
cout << "Please enter your address." << endl;
cin >> address;
cout << "You live at " << address << "." << endl;
return 0;
}
cin >> address;
This reads a single word, stopping at the first white-space character. To read a whole line:
std::getline(cin, address);
The input operator reads space separated values. So if your address have a space in it then it will read just the first "word.
And worse, if you enter your full name, the first input will read the first name, and the second input will read the second name.
Try to use std::getline to read the whole line, but first do std::cin.ignore(numeric_limits<streamsize>::max(),'\n'); to skip the newline from the last input (or use std::getline for both inputs).