-
July 4th, 2013, 05:52 AM
#1
struct usage and setup
Hi,
I have included a small program that revolves around struct's. This program works to a certain extent. I have three questions about the struct form and how it is used:
1.) Is there a way to enter data without asking the user how many records you want to add (howmany)?
2.) Is there a way to to make the struct persistent. In other words can you end the program and then start the program again and then add more data to the struct?
3.) What are some of the uses of the struct?
Thanks
Code:
//
#include <iostream>
const int strsize = 30;
// Benevolent Order of Programmers name structure
struct bop {
char fullname[strsize]; //real name
char title[strsize]; //job title
char bopname[strsize]; //secret bop name
int preference; //0 = fullname, 1 = title, 2 = bopname
};
int main()
{
using namespace std;
int howmany = 0;
cout << "How many bop's are you going to load into the bop structure?: ";
cin >> howmany;
cin.get();
int count = 0;
bop * ps = new bop[howmany];
while (count < howmany)
{
cout << "\n" << "Please enter bop title: ";
cin.get(ps[count].title, strsize);
cin.get();
cout << "\n" <<"Please enter bop fullname: ";
cin.get(ps[count].fullname, strsize);
cin.get();
cout << "\n" <<"Please enter bopname: ";
cin.get(ps[count].bopname, strsize);
cin.get();
cout << "\n" << "Please enter preference: ";
cin >> ps[count].preference;
cin.get();
/*
cout << "bop title: " << ps[count].title << "\n";
cout << "bop fullname: " << ps[count].fullname << "\n";
cout << "bopname: " << ps[count].bopname << "\n";
cout << "preference: " << ps[count].preference << "\n";
*/
count++;
}
cout << "Please enter one of the following choices:\n";
cout << "a. display by name\t" << "b. display by title\n";
cout << "c. display by bopname\t" << "d. display by preference\n";
cout << "q. quit\n";
cout << "Enter choice ";
char choice;
cin >> choice;
while (choice != 'Q' && choice != 'q')
{
switch(choice)
{
case 'a':
case 'A':
count = 0;
while (count < howmany)
{
cout << ps[count].fullname << "\n";
count++;
}
break;
case 'b':
case 'B':
count = 0;
while (count < howmany)
{
cout << ps[count].title << "\n";
count++;
}
break;
case 'c':
case 'C':
count = 0;
while (count < howmany)
{
cout << ps[count].bopname << "\n";
count++;
}
break;
case 'd':
case 'D': cout << "games are for the young at heart \n";
break;
default : cout << "Enter a valid choice.\n";
}
cout << "Next choice: ";
cin >> choice;
}
cin.get();
cin.get();
return 0;
}
-
July 4th, 2013, 06:58 AM
#2
Re: struct usage and setup
1) Yes. Use a vector and use STL class string instead of char[]. If you use vector with your current struct then you will need to implement constructor/destructor/copy constructor and operator= functions.
2) Yes. Read/Write the records to a file. Read from file at the start of the program and write the records to the file at the end.
preference would be better as an enum as it takes only 3 values.
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)
-
July 4th, 2013, 07:22 AM
#3
Re: struct usage and setup
You didn't ask for one, but I'll give you an English lesson anyway. Apostrophes are used to indicate possession and contraction. They're not used to pluralize words.
-
July 4th, 2013, 07:34 AM
#4
Re: struct usage and setup
Originally Posted by 2kaud
1) Yes. Use a vector and use STL class string instead of char[]. If you use vector with your current struct then you will need to implement constructor/destructor/copy constructor and operator= functions.
2) Yes. Read/Write the records to a file. Read from file at the start of the program and write the records to the file at the end.
preference would be better as an enum as it takes only 3 values.
Thanks a lot, I'll look into it,
Bob
-
July 4th, 2013, 07:35 AM
#5
Re: struct usage and setup
-
July 5th, 2013, 06:57 AM
#6
Re: struct usage and setup
Originally Posted by 2kaud
1) Yes. Use a vector and use STL class string instead of char[]. If you use vector with your current struct then you will need to implement constructor/destructor/copy constructor and operator= functions.
If all the types in a struct are trivial types (which they are in the OP post, but aren't if he uses std::string), then a copy constructor and assignment operator aren't required, the compiler will automagically provide both. If you don't need the dynamic sizing of a std::string, then sticking to char[] does have some benefits/advantages.
The same is true for streaming to/from disk. If all types are trivial you can just dump/load the binary contents of the structure into a file (in so far as the structure doesn't change between dump and load). If you use non trivial types, you'll need explicit code to handle streaming.
-
July 5th, 2013, 07:46 AM
#7
Re: struct usage and setup
Yeah.. I meant to say 'If you use vector with string then you will need to implement...' but interfaced with keyboard before brain had finished booting-up.
Personally, I always implement constructors/destructors/assignment etc for all classes/structs as I've seen too many examples of subtle bugs being introduced when a pointer/non-trivial type is sneeked into the class/struct when reliance is placed upon the default versions.
Last edited by 2kaud; July 5th, 2013 at 07:58 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)
-
July 5th, 2013, 06:13 PM
#8
Re: struct usage and setup
Originally Posted by OReubens
If all the types in a struct are trivial types (which they are in the OP post, but aren't if he uses std::string), then a copy constructor and assignment operator aren't required, the compiler will automagically provide both. [...]
Even then explicitly impementing the rule-of-three members wouldn't strictly be required: The ones the compiler generates by default would properly call the appropriate counterparts on the member instances. Implementing them is only required if the class/struct in question manages its own non-trivial resources.
Of course, however, serialization would need to be implementex explicitly in this case, as has been said.
Originally Posted by 2kaud
Personally, I always implement constructors/destructors/assignment etc for all classes/structs as I've seen too many examples of subtle bugs being introduced when a pointer/non-trivial type is sneeked into the class/struct when reliance is placed upon the default versions.
I wouldn't agree that this necessarily is a good idea: If your explicit implementations only mimic the behavior of those that would be generated by default by the compiler anyway, then you've got an extra chance to introduce your own bugs, which wouldn't be there if you'd leave that job to the compiler.
Last edited by Eri523; July 5th, 2013 at 06:18 PM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
July 6th, 2013, 07:27 AM
#9
Re: struct usage and setup
If your explicit implementations only mimic the behavior of those that would be generated by default by the compiler anyway, then you've got an extra chance to introduce your own bugs, which wouldn't be there if you'd leave that job to the compiler.
Ideally, you are correct. Anything which can be left to the compiler should be. But, as I said, I've seen far too many times inexperienced programmers adding a pointer/non-trivial type into a struct/class (or changing a char array into a string or a char pointer) which previously only held trivial types and then wondering why programs which previously worked correctly then start behaving wrongly. They hadn't realised the repercussions of what they had done as there was no copy constructor/operator assignment function coded for them to change. I've wasted hours over trying to find these types of problems. It's now one of the first things I look for when asked to 'sort out' a program that has started to behave weirdly. [Incidentially this turned out to be the cause of my GlobalFree issue post way back in January].
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)
-
July 6th, 2013, 07:30 PM
#10
Re: struct usage and setup
Originally Posted by 2kaud
Ideally, you are correct. Anything which can be left to the compiler should be. But, as I said, I've seen far too many times inexperienced programmers adding a pointer/non-trivial type into a struct/class (or changing a char array into a string or a char pointer) which previously only held trivial types and then wondering why programs which previously worked correctly then start behaving wrongly. They hadn't realised the repercussions of what they had done as there was no copy constructor/operator assignment function coded for them to change. I've wasted hours over trying to find these types of problems. It's now one of the first things I look for when asked to 'sort out' a program that has started to behave weirdly. [Incidentially this turned out to be the cause of my GlobalFree issue post way back in January].
Usually these problems arise when development haphazardly mixes 'C' POD style functions that wipe out memory or set memory using things such as memcpy(), memset(), etc. The struct started out as POD, but during the development cycle, someone added a std::string, or any other non-POD type to the struct, making functions like memset() absolutely disastrous.
That's why I always stress that unless the struct is from a C-based API, beware of using memset(), memcpy(), malloc(), ZeroMemory() etc. on your own structs to set the memory or to allocate memory.
Regards,
Paul McKenzie
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
|