When I want to get a string with getline function, it jumps from this statement without getting string . Because I get an integer number before it and the \n is pressed . To solving this problem I use cin >> ws before getting string with getline function . Now when I want to enter white spaces in beginning of string , this white spaces is ignored .
What can I do to solving this problem ?
Thanks ......

Recommended Answers

All 6 Replies

cin.ignore() I think.

This is what I would do.

#include <iostream>
#include <string>

using namespace std;

int main()
	int myInt;
	string myString;
	cin >> myInt;
	cin.ignore(); //use this it ignores the last '\n' character
	getline(cin, myString);
	return 0;

twomers beat me to it but yes that is an easy fix for it.

I want if user entered " behi jon", it stores same in variable and not "behi jon" . I need two white space at the beginning of input . But the compiler skip it .

In the code I posted I tested what you wanted and it worked.

In the code I posted I tested what you wanted and it worked.

Oh, thanks very much . It works . Thanks ... Thanks ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.