Capture a space

Sep 20, 2012 at 6:24am
Hi all
I struggle to capture the space when it is entered as part of the name
The 2 lines commented out is how I try to capture the space but it doesn't work. Anyone mind telling me what i do wrong please?

Here is my code
#include<iostream>
#include<string>
using namespace std;

void displayData( string name, int ID, int StartTime, int EndTime)
{
cout << " Please enter name, ID, Starttime and EndTime " << endl;
//cin.get();
//getline(cin, name, 30);
cin >> ID >> StartTime >> EndTime;



cout << name << endl;
cout << "ID : " << ID << endl;
cout << "StartTime : " << StartTime << endl;
cout << "EndTime : " << EndTime << endl;
}

int main()
{
string name;
int ID, StartTime, EndTime;

displayData(name, ID, StartTime, EndTime);
displayData(name, ID, StartTime, EndTime);
return 0;
}
Sep 20, 2012 at 6:27am
Ahhoy, you should not have added the cin.get(); there.

Hope it HELPS!!!
Sep 20, 2012 at 6:30am
Hi Aceix
Thanks for your response. Even if i comment out cin.get() i still get the error
Sep 20, 2012 at 6:48am
What is the error?
Sep 20, 2012 at 6:59am
Standard string extraction stops on white space characters, so you'll have to use the getline() function. Below is an example of the function in use.

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main(void) {
     std::string fullName;
     getline(std::cin, fullName);
     std::cout << fullName << std::endl;
     return 0;
}


EDIT: The code compiles and runs just fine. What's the problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;

void displayData( string name, int ID, int StartTime, int EndTime)
{
	cout << " Please enter name, ID, Starttime and EndTime " << endl;
        cin.ignore(); // extracts whitespace characters left in the input buffer
	getline(cin, name);
	cin >> ID >> StartTime >> EndTime;
	cout << name << endl;
	cout << "ID : " << ID << endl;
	cout << "StartTime : " << StartTime << endl;
	cout << "EndTime : " << EndTime << endl;
}

int main()
{
	string name;
	int ID, StartTime, EndTime;

	displayData(name, ID, StartTime, EndTime);
	displayData(name, ID, StartTime, EndTime);
	return 0;
}
Last edited on Sep 20, 2012 at 7:04am
Sep 20, 2012 at 7:00am
Thanks for the responses guys but I got this working a minute ago. changed the statement to std::getline(std::cin, name, '\n');

thanks for the willingness to help. Appreciate it
Sep 20, 2012 at 7:03am
why do i need to add std:: for the statements to work? if someone can explain in simple terms pls?
Last edited on Sep 20, 2012 at 7:04am
Sep 20, 2012 at 7:15am
The std namespace has heaps of stuff in it - so using namespace std; pollutes the global namespace, so not doing that makes it easier on the compiler.


Try to avoid the using namespace std; you can do one of these instead:

1. Put these after the #include statements

1
2
3
4
5
using std::cout;
using std::cin;
using std::endl;
using std::string;
//similar for whatever std thing you want to use  



2. Put std:: before each std thing in your code. Example :

std::cout << "Testing " << std::endl;



I prefer the first - I would rather have 20 of those at the start of the file rather than 200 std:: throughout.

However, a lot of people prefer the second.

If someone sends me code with std:: throughout - I just leave them there.
Sep 20, 2012 at 7:18am
Thanks for the explaination. I see if i change teh statement to getline(cin, name, '\n'); it also works so I will leave it like that ie. without the std::
Sep 20, 2012 at 7:28am
it also works so I will leave it like that ie. without the std::


Is that because the using namespace std; is still there?
Sep 20, 2012 at 7:36am
Correct. I see the actuall issue was that i tried to enter a name like Mr. J. Blog which contained spaces and a period so i had to add '\n'. That was the issue why the problem would'nt run
Topic archived. No new replies allowed.