stefan.kenyon
stefan.kenyon

Reputation: 339

C++ Input String with Spaces

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

Upvotes: 9

Views: 74398

Answers (7)

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.

Upvotes: -1

user15448600
user15448600

Reputation: 31

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

Upvotes: 3

Abhishek kumar
Abhishek kumar

Reputation: 139

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

Upvotes: 0

M.M
M.M

Reputation: 2304

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.

Upvotes: 4

krsna
krsna

Reputation: 4353

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

Upvotes: 0

Brad Pitt
Brad Pitt

Reputation: 416

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.

Upvotes: 2

Jack Of Blades
Jack Of Blades

Reputation: 505

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

Upvotes: 15

Related Questions