-
September 16th, 2013, 09:46 AM
#16
Re: Error while using cin.get
Originally Posted by FenixRising
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
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 16th, 2013, 10:39 AM
#17
Re: Error while using cin.get
1 : incomplete type is not allowed
-
September 16th, 2013, 10:47 AM
#18
Re: Error while using cin.get
Originally Posted by FenixRising
1 : incomplete type is not allowed
You may want to post your "changed" code (of course, with Code tags!)
Victor Nijegorodov
-
September 16th, 2013, 12:27 PM
#19
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";
}
-
September 16th, 2013, 12:41 PM
#20
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 to golf.cpp after
Victor Nijegorodov
-
September 16th, 2013, 02:59 PM
#21
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
-
September 16th, 2013, 03:35 PM
#22
Re: Error while using cin.get
Originally Posted by FenixRising
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 16th, 2013, 09:03 PM
#23
Re: Error while using cin.get
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|