I am trying to take in inputs from the user for one of my projects.

cout<<"Enter Event Name (enter 'exit' to quit) ::";
    gets(dat.evnm);
    cout<<"Enter Type Of Event (eg: Dance, Music, 'exit' to quit) ::";
    cin>>dat.type;
     if(strcmp(dat.type,"exit")==0)
        return 0;
    cout<<"\n";

here the line

gets(dat.evnm);

will not execute, and the program jumps to the next input statement.

I tried replacing gets with, cin.getline but still have the same problem,

using cin solves the problem, but i need to take in a string so cin wont work.

Please help me with this, I am at an emergency, ...please help
Thanks In Advance

Recommended Answers

All 2 Replies

Using gets() is usually a bad idea from the start (See this page for more information), but this problem is usually caused by stray characters still present in the buffer. gets() will simply take those characters as input and it seems as though your program is skipping input. It can be fixed by flushing the buffer(input stream). Just add cin.ignore(); before your gets() statement.

If you need better methods to do the same, then take a look at Narue's article (which incidentally, is a sticky right at the top of the forum)

A quick suggestion, though: Use std::string instead of char arrays, as many things (including input and comparison) are made easier.

commented: Great suggestions :) +33
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.