Passing a string of user input to a function is blank

Posted on 2004-10-25
Last Modified: 2010-04-17
cout<<"Please enter a cities name in Ohio begining with the letter S"<<"\n";
      getline(cin, temp.city_name);
      Search_hash_table(temp, count, vec1);
I am sorry to bother everyone but I am stuck. If I wanted to pass the input string of user input to the search_hash and temp is my struct isn't this how I would do it? The reason I am asking because the search function is outputing a blank city_name and I have even debugged by putting in a cout and it is blank. What the heck am I missing? Thanks
Question by:Corey_819

    Accepted Solution

    Well it could be because you're reading twice into temp.city_name.
    when you do getline(cin, temp.city_name); you're overwriting whatever you received on the line above.
    Try to remove either
    LVL 21

    Expert Comment

    What datatype is city_name?

    Which should read in the name?
         cin>>temp.city_name;                      //Read in the name?
         getline(cin, temp.city_name);            //Read in the name or the name plus newline or just newline
    LVL 3

    Expert Comment


    If temp.city_name is a character array (char[]) then use :

    ... but if it is a "string" then use :
    getline(cin, temp.city_name);

    Don't put both lines in your code.

    LVL 1

    Author Comment

    Sorry I wish I had more points to give you both. Why was some of the website c++ fourms using both cin and getline that is now why I am confused? Thank you so much.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: JavaScript Coding - Massive 12-Part Bundle

    Regardless of your programming skill level, you'll go from basics to advanced concepts in a vast array of JavaScript subjects including Sammy.js, Agility.js, Ember.js, Node.js, jQuery, AJAX, Extjs, AngularJS, Knockout.js, and JSON.

    Suggested Solutions

    A short article about a problem I had getting the GPS LocationListener working.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now