• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Skipping gets() statement

## Recommended Posts

void addmov()
{

cout << "Enter movie title: ";
gets(item[i].title);

item[i].index = i;

cout << "\nEnter Category: ";
gets(item[i].category);

} 
Everytime the function is called it skips the gets(item.title); and just moves on to the next gets() statement before asking for input. So far I havent been able to get it to not skip it. any suggestions on why its doing that would be great thx. RanBlade "Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry." - Dave Pottinger, Ensemble Studios [edited by - ranblade on October 10, 2003 3:18:51 PM]

##### Share on other sites
ApochPiQ    22999
This is because there is already a line feed (CRLF) in the keyboard input buffer. Flush it out by calling a secondary gets() or by looking up the buffer IO functions on MSDN and figuring out how to flush the buffer yourself (its been too long since I used IO stream buffers to remember how its done).

In all honesty, you should not use gets() and cout in the same program. Use cin >> variable; instead.

##### Share on other sites
as of right now i know no other way to take a multi word string with cin >> so that is why i was using gets(). Ill look around on MSDN

"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn''t be in the games industry." - Dave Pottinger, Ensemble Studios

##### Share on other sites
kordova    140
Try a getchar() or getch() after the first gets() I'll look into it now but that sounds familiar (and sorry if it doesn't halp at all!)

hmm
char title[128];            char category[128];            cout << "Enter movie title: ";			gets(title);			//_getch();					cout << "\nEnter Category: ";			gets(category);            cout<< endl<<title << endl <<category<<endl;            [/soure]Works for me.

[edited by - kordova on October 11, 2003 12:12:42 PM]

##### Share on other sites
ToohrVyk    1595
Try calling cout.flush( ); just before the first gets() (you never know, this could work).

##### Share on other sites
kordova    140
Oh it skips the first one. You must''ve used a cin before it, right? If so, use a getchar(); to be rid of the endl i think gets eats and stops at or such (searching through console code I''ve seen that repeatdly by myself so that must''ve fixed it for me). Cheers.

##### Share on other sites
Guest Anonymous Poster

std::string value = item[i].title;

Just a thought.

flush(stdin);