getline problems

How do I show 2 decimal places for a double value?
example: if I put in 14 I would like it to show up as 14.00

It seems like it should be something very simple, but I haven't found a simple solution anywhere yet.
[Solved!]

Is it possible to use cin.getline with a string? nevermind, it looks like this is the correct format: "
getline(cin,name); "
[Solved!]


Hmm... When I use getline(cin,name); after another prompt for input, it won't work.
[Solved!]
Last edited on
Thank you very much!
I was missing the "fixed" option.
Last edited on
I am having another problem.
When I prompt for input before using a getline(cin,name); The second never prompts for input. (sorry, i don't know how to explain it)

Here is the code that I'm using:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//test

#include<iostream>

#include<string> 

using namespace std; 

int main()

{
	int year;
	string name; 
	cout<<"enter year of birth ";
	cin>>year;

	cout<<"Enter name ";

	getline(cin,name); 

	cout<<"You are "<<name<<" and were born in the year "<<year<<endl; 

	return 0;
}

Any ideas?
It works if I move the year after the name, but not before!?
The reason why it is not asking you for an input is that you are mixing cin and getline statements.
Let me explain:
getline() stores all the text you enter until when you enter a specific character. By default that is '\n', the new line character (i.e. when you press enter).
You can change this by adding one more parameter to getline()

example:
 
getline(cin, name, 'a');


This getline will store every letter you enter until when you enter an a.

So, getting back to your problem, when you enter the value of year using cin, you enter a number and then you press enter (which corresponds to the new-line character '\n'). This character gets goes into the getline() function, and since your getline is set to stop accepting values when it sees a \n the function getline() ends and your program goes on to the next instruction.

An easy way to avoid this you can use the ignore() function before you use getline.

For example ignore(1000, '\n'); is going to ignore up to 1000 characters until when it sees a \n, which will be the last character ignored.

I hope this helps.
You could do this to clear the buffer:

#include<limits>
1
2
3
4
5
6
7
	cout<<"enter year of birth ";
	cin>>year;
	cin.ignore(numeric_limits<streamsize>::max(), '\n');

	cout<<"Enter name ";

	getline(cin,name); 


But it would be better to not mix cin and getline like borekiller said.
http://www.daniweb.com/tutorials/tutorial71858.html#

I think this would be best:
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
26
#include <iostream>
#include <string> 
#include <sstream> // for stringstreams

using namespace std; 

int main()
{
	int year;
	string name;
	string line;
	stringstream stream;
	cout<<"enter year of birth ";
	getline(cin, line);
	stream << line;
	stream >> year;

	cout<<"Enter name ";

	getline(cin,name); 

	cout<<"You are "<<name<<" and were born in the year "<<year<<endl; 

	return 0;
}



Thank you both so much!
My program now works again. :)
Topic archived. No new replies allowed.