9

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;
}
2
  • 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 minimal reproducible example to be sure this is what you've run into, though. Commented Dec 5, 2016 at 22:49
  • 1
    Try this edit: link Commented Dec 5, 2016 at 22:59

7 Answers 7

15

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

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<streamsize>::max(), '\n'); just to be sure there isn't a space or something else also hanging around in addition to the EOL.
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.

Comments

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;
}

Comments

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.

Comments

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");
}

2 Comments

What does the system("PAUSE") do?
It pauses the output console. Whenever, a result is returned by your program, it helps the output console to pause until you close the window.
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;
}

1 Comment

Although this code might solve the problem, a good answer should explain what the code does and how it helps
-1

Bro, I have searched a lot on the Internet for this issue for an entire semester and couldn't find the solution that was easy enough to understand until I came across a single line of code → getline(cin >> ws, stringVarName); Below is the example code:

#include <iostream>
using namespace std;

int main() {
     int age, rollNumber;
     string fullName;


     cout << "Enter your age: "; cin >> age;
     cout << "Enter your full name: "; getline(cin >> ws, fullName);
     cout << "Enter your roll number: "; cin >> rollNumber;

     cout << "Dear, " << fullName << ", you confirm that you are " << age << " years old and that your roll number is " << rollNumber; 

return 0;
}

There is a reason behind taking age (int type) followed by fullName (string type) followed by rollNumber (int type). If we try to get around this problem using some other way such as the cin.ignore(), and if the string input contains spaces, the compiler will terminate abruptly without taking the roll number input from user.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.