8

I am writing a program that takes input from the user. I need the input to include spaces between the words. I am having trouble finding a solution to do that.

Before you ask, I have tried multiple other questions on StackOverflow with the same question. These are some of the ones I have tried:

How to cin Space in c++?

std::cin input with spaces?

Demonstration of noskipws in C++

Effect of noskipws on cin>>

The problem with my code is that as soon as my setBusinessName() method is called, it just completes itself. It outputs and then returns itself without waiting for me to input my data.

string setBusinessName()
{
    string name = "";
    cout << "The name you desire for your business:";
    getline(cin, name, '\n');
    cout << name;
    return name;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
stefan.kenyon
  • 319
  • 1
  • 5
  • 16
  • 5
    Sounds like you could be mixing `>>` and `getline`. `>>` leaves unparsed whitespace in the stream, often leaving an end of line marker for getline to trip over. Need to see an [mcve] to be sure this is what you've run into, though. – user4581301 Dec 05 '16 at 22:49
  • 1
    Try this edit: [link](http://cpp.sh/45q6s) – Dimitar Nikovski Dec 05 '16 at 22:59

6 Answers6

14

I can't comment yet, don't have enough points, but did you try adding cin.ignore(); before the getline(cin, name, '\n'); ?

Like this:

string setBusinessName()
{
    string name = "";
    cout << "The name you desire for your business:";
    cin.ignore();
    getline(cin, name, '\n');
    cout << name;
    return name;
}
Jack Of Blades
  • 495
  • 7
  • 16
  • 2
    While I'm pretty sure this solves OP's problem, this is a Bad Idea in the general case. If there is no hanging whitespace in the buffer you just ate valid input. Better to put the `ignore` after the `>>`where you know there should be a EOL and upgrade `ignore()` to `cin.ignore(numeric_limits::max(), '\n');` just to be sure there isn't a space or something else also hanging around in addition to the EOL. – user4581301 Dec 05 '16 at 23:11
4

Just adding some more explanation to the comments, when you do:

cout << "Enter value:";
cin >> x;

The cin instruction is executed when the user presses Enter, so the input buffer has the value the user inserted and an extra '\n' char. If you continue doing cin that is ok, but if you want to use getline (like in your case to include spaces in a string) you must be aware that getline will stop at the first occurence of '\n' in the buffer, so the result from getline will be empty.

To avoid this, and if you really must use both cin and getline, you need to remove that '\n' from the buffer by using cin.ignore(streamsize n = 1, int delim = EOF), this function clears streamsize chars from the buffer or until the first char that matches delim (including), here's an example:

cin << x;
cin.ignore(256, '\n');
getline(cin, name, '\n');

Note it is advisable to use:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

if you don't want to guess how many chars are in the buffer.

M.M
  • 2,254
  • 1
  • 20
  • 33
3
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name1, name2, name3, name4, name5;
    int a,b; //or float ...

    cout << "Input name 1: ";
    getline(cin, name1); //input: abc def
    cout << "=> Name 1: "<< name1 << endl;      //output: abc def
    cout << "Input name 2: ";
    getline(cin, name2); //input: abc def
    cout << "=> Name 2: "<< name2 << endl;      //output: abc def

    cout<<"a: ";
    cin>>a;
    cout<<"a: "<<a<<endl;
    cout << "Input name 3: ";
    getline(cin, name3); //can not input
    cout << "=> Name 3: "<< name3 << endl; //output:

    cout<<"b: ";
    cin>>b;
    cout<<"b: "<<b<<endl;
    cout << "Input name 4: ";
    cin.ignore();
    getline(cin, name4); //input: abc def
    cout << "=> Name 4: "<< name4 << endl; //output: abc def

    
    cout << "Input name 5: ";
    cin.ignore();
    getline(cin, name5); //input: abc def
    cout << "=> Name 5: "<< name5 << endl; //output: bc def  !!!!!!!!!!
    
    //=> cin>>number; cin.ignore(); getline(cin, str); => OK
    //else: !!!!!!!!
    return 0;
}
2

It's possible that there is already something in the stream and getline() just reads it.

Make sure you didn't use cin>> before this function. And you can use cin.ignore() before getline() to avoid something already existed in the stream.

Brad Pitt
  • 398
  • 3
  • 11
0

It is working fine. I just tried this.

#include <iostream>
#include <string>
using namespace std;

string setBusinessName(){
    string name = "";
    cout << "The name you desire for your business:";
    getline(cin, name);
    cout << name;
    return name;
}

int main() {
    setBusinessName();
    system("PAUSE");
}
coderpc
  • 4,119
  • 6
  • 51
  • 93
0
#include<bits/stdc++.h>
using namespace std;

string setBusinessName(){
 string name;
 cout << "The name you desire for your business: ";
 getline(cin, name);
 cout << name;
 return name;
 }

int main() {
  setBusinessName();
  return 0;
}
  • Although this code might solve the problem, a good answer should explain **what** the code does and **how** it helps – BDL Aug 05 '20 at 09:54