Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9305225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:52:51+00:00 2026-06-18T23:52:51+00:00

When I run the following code and insert a new line (press enter) when

  • 0

When I run the following code and insert a new line (press enter) when prompted for
the golf structure, the second call to the function doesn’t request input and finishes as if I’ve pressed enter again.

I’ve read up on : cin.get() , cin.clear() , cin.ignore(…) but nothing seems to help.

I’m pretty sure that it has nothing to do with multiple .cpp files and the header but I’m putting the code as is.

I’m using Visual Studio C++ 2010 – Express.

Thanks in advance for your help!

header file : golf.h

#ifndef GOLF_H
#define GOLF_H

const int Len = 40;

struct golf{

    char fullname[Len];
    int handicap;

};

int setgolf(golf & g );
void showgolf(const golf & g );

#endif

golf.cpp

#include "stdafx.h"
#include "golf.h"
#include <iostream>
#include <string>


using namespace std;

int setgolf(golf & g ){

    cout << "Enter a name for the golf structure:" << endl;
    if  (cin.get(g.fullname,Len)) {     
        cin.get(); // deals with the '\n' incase the user inputs a valid string
        return 1;
    }
    else{
        return 0;
    }

}
void showgolf(const golf & g ){

    cout << "this golf structure contains the following information:" << endl;
    cout << "name:      " << g.fullname << endl ;
    cout << "handicap:  " << g.handicap << endl ;

}

main ()

#include "stdafx.h"
#include "golf.h"
#include <iostream>
#include <string>


using namespace std;

int main()
{

    golf myGolf;

    // check of int setgolf(golf & g );
    int answ = setgolf(myGolf); //try to input empty string
    showgolf(myGolf);
    cout << "the number returned :" << answ << endl ;

    answ = setgolf(myGolf); // normal string
    showgolf(myGolf);
    cout << "the number returned :" << answ << endl ;

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:52:52+00:00Added an answer on June 18, 2026 at 11:52 pm

    This problem happens when you just press enter in the first prompt. The input stream is marked as eof, an error condition flag (that’s why it returns 0). The input stream then stops working.

    It seems that you’re using a kind of old C++, pre ISO 1998, while I don’t think you need that. However, if you want to stick with your approach, then do the following: after cin.getline() (no need to return anything) write: cin.clear(); cin.sync();, as follows:

    void setGolf(Golf &g)
    {
        cout << "Enter a name for the golf structure:" << endl;
        getline( cin, g.fullname ) );
    
        cin.clear();
        cin.sync();
    }
    

    Now, about modernizing your code. First of all, you can use the standard library’s class string, which is able to store a string literal, even growing if needed, without giving a maximum value of chars. This is somewhat confusing, since you are including the header string, which will include that class, but you’re not using it. The use of string also comes with other advantages, such as automatically correcting the potential buffer overflow which could happen in your Golf structure. So I would change your structure to be:

    struct Golf{
        string fullname;
        int handicap;
    };
    

    Now you can use getline(), in utility, which reads a whole line and stores it in string, doing all the magic for you. So you could change your golf.cpp file, to:

    #include <utility>
    
    //...
    
    void setGolf(Golf &g)
    {
        cout << "Enter a name for the golf structure:" << endl;
        getline( cin, g.fullname ) );   
    }
    

    You can now also change the return type to void. It is not probable to experience an error of any kind while using getline(). Anyway, take into account that you could return bool (boolean type), which is a built-in type, with literals true and false.

    I am certain that you could change your main() now, to a simpler style:

    int main()
    {
    
        Golf myGolf;
    
        setGolf(myGolf);
        showGolf(myGolf);
    
        setGolf(myGolf);
        showGolf(myGolf);
    
        return 0;
    }
    

    Finally, you could consider encapsulating your information in a class, instead of a struct, but that is a whole different issue.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can the following code be modified to run my insert statements 100 at
I run the following code from command line: public class MemoryTest { public static
When I run the following Code it crashes at the last line. I don't
I managed to run the following code to insert into my table on first
I am using the following code to insert a new row to database, I
when I trying to run following code. var result = from c in db.brand
I run the following code on a Windows platform. The purpose is the know
When I run the following code I get B as expected: class A {
If I run the following code on my HTC magic phone I get a
When I run the following code: private void button1_Click(object sender, EventArgs e) { Bitmap

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.