im new at this so i need a little help. i want to allow the input to be as many characters and spaces as neccesary to fill the input, for example, i typed cin>>name and cin>>birthday if the person types their first and last name then it will skip the next cin function. how do i avoid this, i want to be able to just declare name as some sort of string or char without having to go through the whole name=first+last ordeal and having them input first and last..if someone gets what im saying i just need the header file and proper declarations..im new at this but im sure ill understand the complex answers i might get
Use std::getline to get input on one line.
std::string name;
std::getline(std::cin, name);
Alternatively, if you expect the birthday to be on the same line, then do this.
std::string input;
std::getline(std::cin, input);
std::istringstream input_stream(input);
std::string first_name, last_name, birthday;
if (!(input_stream >> first_name)) std::cout << "Input cannot be empty.\n";
if (!(input_stream >> last_name)) std::cout << "You must have a last name.\n";
if (!(input_stream >> birthday)) std::cout << "You forgot your birthday.\n";
If the birthday contains spaces in it, then you can use std::getline on the remaining words in the stream.
std::getline(input_stream, birthday);
So finally your program becomes:
#include <iostream>
#include <sstream>
int main()
{
std::string input;
std::getline(std::cin, input);
std::istringstream input_stream(input);
std::string first_name, last_name, birthday;
if (!(input_stream >> first_name)) std::cout << "Input cannot be empty.\n";
if (!(input_stream >> last_name)) std::cout << "You must have a last name.\n";
input_stream.ignore(); // Discard space that doesn't get extracted out
std::getline(input_stream, birthday);
std::cout << first_name << "\n";
std::cout << last_name << "\n";
std::cout << birthday;
}
In order to discard the space that's left in the stream, use input_stream.ignore();
.
If you want to handle names that have spaces in it, apply the same principle. This time you have to supply arguments to ignore to tell it to discard spaces.
int main()
{
std::string input;
std::getline(std::cin, input);
std::istringstream input_stream(input);
std::string first_name, last_name, birthday;
std::getline(input_stream, first_name, ',');
input_stream.ignore(1, ' '); // Discard space
std::getline(input_stream, last_name, ',');
input_stream.ignore(1, ' '); // Discard space
std::getline(input_stream, birthday, ',');
std::cout << first_name << "\n";
std::cout << last_name << "\n";
std::cout << birthday;
}
Example run:
First Name, Last Name, This is a birthday
First Name
Last Name
This is a birthday
Basically, you want to split a string by spaces. The following program uses a vector to store the elements. Check the size of it, and if it is 3, then assume the first and last name have been given.
#include <iostream>
#include <vector>
#include <iterator>
int main()
{
std::string buffer;
std::getline(std::cin, buffer);
std::vector<std::string> v;
std::string tmp;
for(unsigned i = 0; i < buffer.length(); i++) {
tmp += buffer[i];
if(buffer[i] == ' ') {
v.push_back(tmp);
tmp = "";
}
}
// push the last one
v.push_back(tmp);
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
return 0;
}
Compiling & Testing
g++ -Wall -Wextra main.cc -o main
Test Data This file is called test.txt
FIRST LAST 10242014
To Test execute this: main < test.txt
Expected Output
FIRST
LAST
10242014
I would always use std::getline()
. I know you asked for all names on one line but I would always split the name gathering to separate lines because first, second and last names can all contain spaces (eg, Du Vall).
struct person
{
std::string f_name;
std::string m_name;
std::string l_name;
std::string DOB;
};
person p;
std::cout << "First name: ";
std::getline(std::cin, p.f_name);
std::cout << "Middle name(s): ";
std::getline(std::cin, p.m_name);
std::cout << "Last name: ";
std::getline(std::cin, p.l_name);
std::cout << "Date Of Birth: ";
std::getline(std::cin, p.DOB);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.