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

Dani AI

Generated

Good point from — the symptom comes from mixing formatted extraction (operator>>) with line-oriented input. Formatted extraction leaves the trailing newline in the stream; the next line-based read (gets/getline) immediately consumes that newline and returns an empty string, so it looks like input was "skipped". A more robust, modern C++ approach is to stop using raw char[] buffers and use std::string with std::getline.

#include <iostream>
#include <string>

struct Data { std::string evnm, type; };

Data dat;
std::cout << "Enter Event Name (enter 'exit' to quit) :: ";
std::getline(std::cin >> std::ws, dat.evnm);
if (dat.evnm == "exit") return 0;

std::cout << "Enter Type Of Event (eg: Dance, Music, 'exit' to quit) :: ";
std::getline(std::cin >> std::ws, dat.type);
if (dat.type == "exit") return 0;

Notes and troubleshooting tips: use std::getline for any input that should include spaces, and either (a) use it consistently for string fields, or (b) when mixing token extraction and line reads, consume the leftover newline before calling getline (the code above uses the std::ws manipulator to skip leading whitespace). For numeric input followed by line reads, read the entire line and parse it with std::istringstream to avoid leftover characters altering subsequent reads.

Additional cautions: gets is unsafe and should not be used in C++; prefer std::getline (or fgets in C). Switch char[] fields to std::string and compare with == rather than strcmp. If unexpected behavior persists, check std::cin.fail()/clear() and print debug info (length or contents) of the string to confirm whether an empty line was read.

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

Works Great....thank you

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.