0

Hey guys, I really need your help on this, because I'm either being really dizzy or I just can't figure out why my code isn't working at all.
I've wasted around 4 hours trying to turn around this problem so far and I just can't.

here is the part of the code responsible for all my frustraion:

cout<<"Name:"<<endl;                    
       fgets(name,256,stdin);
            cout<<"Phone:"<<endl;
            cin>>phone;

I need to use fgets because if I use the standard cin for my char name[257], it will end after the first whitespace typed and I don't want that.

The line Name: is automaticaly skipped and the result is this:

Name:
Phone:
(waiting for input)

If you know any alternative for this I would be really happy.

Edited by Diogo Martinho: n/a

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by WaltP
0

if it helps I've also tried this method and got the same problem

cout<<"Name:"<<endl;                    
       std::getline(std::cin, name);
cout<<"Phone:"<<endl;
            cin>>phone;

Edited by Diogo Martinho: n/a

0

Try

cout<<"Name:"<<endl;                    
    std::getline(std::cin, name);
    cout<<"Phone:"<<endl;
    std::getline(std::cin, phone);

Mixing line inputs (fgets/getline) with cin is causing problems because they both do things differently. cin is leaving the input stream dirty (stuff left in the buffer you aren't expecting).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.