easy question part 2

M

max

Hi,
I've tried the std::getline and std::string. It seams to work but I get
some tourbles with it. WIth the folowing code:

if(ne_menu=='4')
{
cout << "Nom du Client/Projet:";
getline(std::cin,estime_nom);
nouv_estime();
}

when ne_menu is equal to 4 it just steps right to nouv_estime without
asking anything... What do I do wrong??
 
R

Ron Natalie

max said:
Hi,
I've tried the std::getline and std::string. It seams to work but I get
some tourbles with it. WIth the folowing code:

if(ne_menu=='4')
{
cout << "Nom du Client/Projet:";
getline(std::cin,estime_nom);
nouv_estime();
}

when ne_menu is equal to 4 it just steps right to nouv_estime without
asking anything... What do I do wrong??

Well you didn't bother showing us the rest of the code, but I suspect
that you showed him:

char ne_menu;
cin >> ne_menu;

before.

The problem is that you read one character, but you left a newline in the buffer.
That immediately gets eaten by getline. You can either do cin.ignore() to eat
the rest of the line, or use getline for reading ne_menu rather than just a char.
 
M

max

On Tue, 21 Oct 2003 18:03:11 -0400, Ron Natalie wrote:
Hi,
I think I didn't put a good example, all I want to do is this to work (
e.g. )

std::string hello;
std::cout << "Please enter your name:";
std::getline(std::cin,name);
std::cout << "Hello, " << name << " !";

So how can you make this work?
 
R

Rolf Magnus

Please don't top-post. Fixed.
god damnit wrong var name!


should be name insted of hello!!

#include <iostream>
#include <string>

int main()
{
std::string name;
std::cout << "Please enter your name:";
std::getline(std::cin,name);
std::cout << "Hello, " << name << " !";

std::cout << std::endl;
}


Works fine for me. Which problem do you get with it?
 
M

max

On Wed, 22 Oct 2003 01:15:50 +0200, Rolf Magnus wrote:

Yes me too but I didn't realize my problem was that when you enter
somthing like "Mr Max" it only takes "Mr" in your var and max stay for
another CIN further. What's the prob?
 
K

Karl Heinz Buchegger

max said:
On Wed, 22 Oct 2003 01:15:50 +0200, Rolf Magnus wrote:

Yes me too but I didn't realize my problem was that when you enter
somthing like "Mr Max" it only takes "Mr" in your var and max stay for
another CIN further. What's the prob?

First of all:
Please don't top post. The code you are referring to is at the end of you
reply, thus to compare your diagnostics with the code it refers to, we need
to constantly scroll up and down. Put your reply beneath the section
you are replying to, such as I did now. And snip the sections you
don't need in your reply.
If you are too lazy to come up with an easy to read reply, where
the things are in context and chronological order then we
will be too lazy to try to help you. It's as simple as that.

[snip]

When entering 'Mr Max' at the prompt, name should contain
the string 'Mr Max'. The diagnostics you showed is the normal
behaviour, if you did:

std::cin >> name;

instead of

std::getline( std::cin, name );

Are you sure this is not the problem?
 
?

=?ISO-8859-1?Q?Juan_Antonio_Dom=EDnguez_P=E9rez?=

max said:
On Wed, 22 Oct 2003 01:15:50 +0200, Rolf Magnus wrote:

Yes me too but I didn't realize my problem was that when you enter
somthing like "Mr Max" it only takes "Mr" in your var and max stay for
another CIN further. What's the prob?

Hint: the std::getline has a third parameter that is the delimiter
between reads, by default is an space. This is the cause of this behaviour.
 
K

Karl Heinz Buchegger

Juan said:
Hint: the std::getline has a third parameter that is the delimiter
between reads, by default is an space. This is the cause of this behaviour.

Are you sure you know what you are talking about?
The standard default delimiter for getline equals '\n', not space.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top