-
September 14th, 2013, 03:28 PM
#1
Error while using cin.get
Hi, Line 76: cin.get(g.handicap+count);
Pre compile error dot in between cin and get. no instance of overload function ...
Code:
// golf.h -- for pe-9.cpp
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);
// chapEx9-1
#include <iostream>
#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;
while (cin.get() != '\n')
continue;
golf * ptr_golf = new golf[ngolfers];
//int entered = getinfo
int rint = 1;
while (rint == 1)
rint = setgolf(ptr_golf);
showgolf(ann);
}
// golf.cpp load and show golf struc
#include<iostream>
#include "golf.h"
using namespace std;
void setgolf(golf & g, const char * name, int hc)
{
strcpy_s(g.fullname, name);
g.handicap = hc;
// strlen(name)
}
int setgolf(golf & g)
{
static int count = 1;
static int pos = 0;
char fname[40];
cout << "Enter golfers name: ";
cin.getline(g.fullname+count, 40, '\n');
cout << "Enter golfers handicap: ";
cin.get(g.handicap+count);
cin.get();
++count;
if (strlen(g.fullname) == 0)
return 0;
else
return 1;
}
void showgolf(const golf & g)
{
cout << "\nFull name: " << g.fullname << "\n";
cout << "Handicap: " << g.handicap << "\n";
}
-
September 14th, 2013, 03:43 PM
#2
Re: Error while using cin.get
Originally Posted by FenixRising
Hi, Line 76: cin.get(g.handicap+count);
Pre compile error dot in between cin and get. no instance of overload function ...
Code:
cin.get(g.handicap+count);
And what are you going to achieve with this line?
Victor Nijegorodov
-
September 14th, 2013, 03:53 PM
#3
Re: Error while using cin.get
I am trying to enter into the member handicap of the stuct golf of the handicap of the golfer entered. It is an integer.
-
September 14th, 2013, 04:44 PM
#4
Re: Error while using cin.get
cin.get reads a single character (!) from the input to a receiving buffer (!!). In the light of this g.handicap+count makes no sense, as this is not a buffer, and even if it were the one, you won't need the input character code to be put to your variable.
Best regards,
Igor
-
September 14th, 2013, 04:56 PM
#5
Re: Error while using cin.get
Code:
int setgolf(golf& g);
This function declaration for setgolf takes one argument as a reference to a type called golf. However, when you call this function
Code:
rint = setgolf(ptr_golf);
you are passing a pointer to an array of golf types.
Either you need to have an additional function overload which takes as an argument a pointer to an array of golf, or the current one needs its argument changed.
Code:
char fname[40];
cout << "Enter golfers name: ";
cin.getline(g.fullname+count, 40, '\n');
cout << "Enter golfers handicap: ";
cin.get(g.handicap+count);
I suspect that here you are trying to set the details of an individual element of the array pointed to by ptr_golf. However, this is not how it's done in c++. Arrays start at 0 and not 1 and are accessed either as
Code:
ptr_golf[count].handicap
or
Code:
(ptr_golf + count)->handicap
If g is a reference to a particular element of the ptr_golf array then the access would be
The way you are trying to access
Code:
cin.get(g.handicap + count);
is not valid c++
Also the code
Code:
if (strlen(g.fullname) == 0)
return 0;
else
return 1;
can be shorted to
Code:
return *(g.fullname) != 0;
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 14th, 2013, 05:00 PM
#6
Re: Error while using cin.get
Can you give me some direction to accomplish what I want to do?
-
September 14th, 2013, 05:11 PM
#7
Re: Error while using cin.get
What book are you using as you make reference to chap 9? If I was writing this program, I would be using a class with class functions to set/get golfers details.
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 14th, 2013, 06:05 PM
#8
Re: Error while using cin.get
A simple example of doing this using a class could be
Code:
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef map<string, int> mapsi;
typedef mapsi::iterator miter;
typedef mapsi::const_iterator mciter;
typedef pair<string, int> pairsi;
class golfers {
private:
mapsi details;
public:
bool setperson(const string& name, int hc);
bool setperson();
bool set_hc(const string& name, int hc);
int get_hc(const string& name);
void show_golfers();
};
bool golfers::setperson(const string& name, int hc)
{
if (details.find(name) == details.end()) {
details.insert(pairsi(name, hc));
return true;
}
cout << "'" << name << "' already exists!" << endl;
return false;
}
bool golfers::setperson()
{
string name;
int hc;
do {
cout << "full name: ";
getline(cin, name);
if (name.length() == 0) {
//cout << "no name entered!" << endl;
return false;
}
//Note no checking for invalid handicap input
cout << "handicap: ";
cin >> hc;
cin.ignore(100, '\n');
} while (setperson(name, hc) == false);
return true;
}
bool golfers::set_hc(const string& name, int hc)
{
miter dit;
if ((dit = details.find(name)) != details.end()) {
dit->second = hc;
return true;
}
cout << "'" << name << "' not found!" << endl;
return false;
}
int golfers::get_hc(const string& name)
{
miter dit;
return (dit = details.find(name)) != details.end() ? dit->second : 0;
}
void golfers::show_golfers()
{
mciter dcit;
for (mciter dcit = details.begin(); dcit != details.end(); ++dcit)
cout << dcit->first << " " << dcit->second << endl;
}
int main()
{
golfers players;
players.setperson("Ann Birdfree", 24);
players.show_golfers();
while (players.setperson() == true);
players.show_golfers();
}
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 14th, 2013, 06:46 PM
#9
Re: Error while using cin.get
Hi,
I'm using C++ Primer Plus. Classes are in the next chapter with objects.
I am digesting what you have told me. I'll get back to you.
-
September 14th, 2013, 09:43 PM
#10
Re: Error while using cin.get
I have changed my program. I had some incorrect code left in the program. The program works as it has been written. But, the problem I am having now is how to enter multiple golf struct records using a loop with functions. I understand what your saying , but it was based on a incorrect premise.
[CODE]
// golf.h -- for pe-9.cpp
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);
// chapEx9-1
#include <iostream>
#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;
while (cin.get() != '\n')
continue;
golf * ptr_golf = new golf[ngolfers];
//int entered = getinfo
int rint = 1;
while (rint == 1)
rint = setgolf(ann);
showgolf(ann);
}
// golf.cpp load and show golf struc
#include<iostream>
#include "golf.h"
using namespace std;
void setgolf(golf & g, const char * name, int hc)
{
strcpy_s(g.fullname, name);
g.handicap = hc;
// strlen(name)
}
int setgolf(golf & g)
{
static int count = 1;
static int pos = 0;
cout << "Enter golfers name: ";
cin >> g.fullname;
cout << "Enter golfers handicap: ";
cin >> g.handicap;
++count;
/*if (strlen(g.fullname) == 0)
return 0;
else
return 1;
*/
return 0;
}
void showgolf(const golf & g)
{
cout << "\nFull name: " << g.fullname << "\n";
cout << "Handicap: " << g.handicap << "\n";
}
[CODE]
-
September 15th, 2013, 06:51 AM
#11
Re: Error while using cin.get
Doing it using your design, one possible way would be
Code:
#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');
}
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
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";
}
Last edited by 2kaud; September 16th, 2013 at 09:46 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, 04:46 AM
#12
Re: Error while using cin.get
Originally Posted by FenixRising
I have changed my program. I had some incorrect code left in the program. The program works as it has been written.
First, use code tags correctly. The code you posted is almost unreadable due to using improper code tags.
Second:
Code:
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
//...
cout << "Enter golfers name: ";
cin >> g.fullname;
What if the name is greater than 40 characters (including the terminating NULL)? You now have a memory overwrite error, possibly causing your program to crash.
On a more general note, why are you resorting to 'C' style character handling? Since you're using C++, why not use a string class such as std::string?
Next, there is a memory leak in your code:
Code:
golf * ptr_golf = new golf[ngolfers];
There is no call to deallocate the memory (where is delete[] ptr_golf?)
Regards,
Paul McKenzie
-
September 16th, 2013, 05:13 AM
#13
Re: Error while using cin.get
See the code in my post #11 which takes into account the 2 code points raised by Paul.
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:28 AM
#14
Re: Error while using cin.get
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
-
September 16th, 2013, 09:36 AM
#15
Re: Error while using cin.get
1.
Originally Posted by FenixRising
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.
And what is the exact error message and error code?
2.
Originally Posted by FenixRising
Plus, I see there is a function that is not defined in the program memset; memset(g.fullname, 0, Len);
Well, don't you know how to search with Google? How to use MSDN?
http://www.cplusplus.com/reference/cstring/memset/
http://msdn.microsoft.com/en-us/library/1fdeehz6.aspx
3. Please, use Code tags!
Victor Nijegorodov
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
|