Error while using cin.get - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23

Thread: Error while using cin.get

  1. #16
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,574

    Re: Error while using cin.get

    Quote Originally Posted by FenixRising View Post
    Hi, sorry that i took so long to respond.

    I have changed my code and now have a pre-compile error in this line: void setgolf(golf & g, const char * name, int hc)

    setgolf is the first error flagged.

    Plus, I see there is a function that is not defined in the program memset; memset(g.fullname, 0, Len); in the same function. I am not familiar with it
    You might need to insert

    Code:
    #include <cstring>
    at the beginning of the code (golf.cpp if you are using multiple files - I just used one file for the example) if memset/strncpy is causing a compile error with your compiler. I suspect this rather than the setgolf() function definition statement for the compile error you are obtaining.
    Last edited by 2kaud; September 16th, 2013 at 09:49 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  2. #17
    Join Date
    Apr 2013
    Posts
    42

    Re: Error while using cin.get

    1 : incomplete type is not allowed

  3. #18
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,481

    Re: Error while using cin.get

    Quote Originally Posted by FenixRising View Post
    1 : incomplete type is not allowed
    You may want to post your "changed" code (of course, with Code tags!)
    Victor Nijegorodov

  4. #19
    Join Date
    Apr 2013
    Posts
    42

    Re: Error while using cin.get

    I am sorry about the code tags:

    Code:
    // golf.h -- for pe-9.cpp
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    const int Len = 40;
    struct golf
    {
    	char fullname[Len];
    	int handicap;
    };
    
    //non-interactive version:
    //  function sets golf structure to provided name, handicap
    //  using vlues passed as arguments to the function
    void setgolf(golf & g, const char * name, int hc);
    
    // interactive version:
    // function name and handicap from user
    // and sets the members of g to the values entered
    // returns 1 if name is entered, 0 if name is empty string
    int setgolf(golf & g);
    
    // function resets handicap to new value
    void handicap(golf & g, int hc);
    
    // function displays contents of golf structure
    void showgolf(const golf & g);
    
    void iflush()
    {
    	while (cin.get() != '\n');
    }
    
    // chapEx9-1
    
    #include <iostream>
    #include <cstring>
    #include "golf.h"
    using namespace std;
    
    int main()
    {
    	golf ann;
    
    	setgolf(ann, "Ann Birdfree", 24);
    	showgolf(ann);
    
    	cout << "Enter how many golfers name and handicap do you want to enter: ";
    
    	int ngolfers;
    
    	cin >> ngolfers;
    	iflush();
    
    	golf * ptr_golf = new golf[ngolfers];
    
    	int count = 0;
    	int got = 1;
    
    	while (count < ngolfers && got)
    		count += (got = setgolf(ptr_golf[count]));
    
    	for (int c = 0; c < count; c++)
    		showgolf(ptr_golf[c]);
    
    	delete [] ptr_golf;
    	return 0;
    }
    
    // golf.cpp load and show golf struc
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void setgolf(golf & g, const char * name, int hc)
    {
    	memset(g.fullname, 0, Len);
    	strncpy(g.fullname, name, Len - 1);
    	g.handicap = hc;
    }
    
    int setgolf(golf & g)
    {
    	cout << "Enter golfers name: ";
    	cin.getline(g.fullname, Len);
    	if (*(g.fullname) == 0)
    		return 0;
    
    	cout << "Enter golfers handicap: ";
    	cin >> g.handicap;
    	iflush();
    
    	return 1;
    }
    
    
    void showgolf(const golf & g)
    {
    	cout << "\nFull name: " << g.fullname << "\n";
    	cout << "Handicap: " << g.handicap << "\n";
    }

  5. #20
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,481

    Re: Error while using cin.get

    I'm curious: is the posted code in one file?
    If there are different ones for each modules then remove
    Code:
    #include <cstring>
    using namespace std;
    from golf.h
    and add
    Code:
    #include "golf.h"
    to golf.cpp after
    Code:
    #include  <cstring>
    Victor Nijegorodov

  6. #21
    Join Date
    Apr 2013
    Posts
    42

    Re: Error while using cin.get

    Hi,
    VictorN the last chages compile and ran correctly. There are 3 parts to the program. Thanks for the direction.
    Paul I don't know how to use MSDN, but I would like to learn. Do you know of any white papers, books, or tutorials that could help me learn how to use it? Thanks for the direction.
    2kaud I appreciated the code that helped me see what was going on. Thanks for the direction.

    Bob

  7. #22
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,574

    Re: Error while using cin.get

    Quote Originally Posted by FenixRising View Post
    Hi,
    VictorN the last chages compile and ran correctly. There are 3 parts to the program. Thanks for the direction.
    Paul I don't know how to use MSDN, but I would like to learn. Do you know of any white papers, books, or tutorials that could help me learn how to use it? Thanks for the direction.
    2kaud I appreciated the code that helped me see what was going on. Thanks for the direction.

    Bob
    http://msdn.microsoft.com/en-us/library/ms123401.aspx
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #23
    Join Date
    Apr 2013
    Posts
    42

    Re: Error while using cin.get

    Thanks

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center