pointers and structures
-
Right now I'm creating a program that declares a structure, which also has enum inside it. Now I believe I declare the structure right, but the next step is to create a loop that has the user enter 5 different items of data for library information. But I have to use pointers, so when the loop runs and I enter the information, the data has to be sent to a structure variable only once all six attributes are entered, and the pointer values should be used to create the structure. I was given an example:
cin >> a; cin >> b; library.dewey = &aptr;
This is my code so far:struct card_catalogue //declare a structure then declare all { //the varibles needed for the struct float dewey; string title; string author; int released; int num_pages; enum type {fiction, nonfiction, reference, pond}; }; int main() { card_catalogue books; cout<< "Enter the Dewey Decimal Number please: "; cin>> books.dewey; cout<< "Enter the Title of the book please: "; getline(cin, books.title); cout<< "Enter the Author of the book please: "; getline(cin, books.author); cout<< "Enter the Year of Release please: "; cin>> books.released; cout<< "Enter the Number of Pages please: "; cin>> books.num_pages; cout<< "Enter the type: "; getline(cin, books.type); return 0; }
-
Right now I'm creating a program that declares a structure, which also has enum inside it. Now I believe I declare the structure right, but the next step is to create a loop that has the user enter 5 different items of data for library information. But I have to use pointers, so when the loop runs and I enter the information, the data has to be sent to a structure variable only once all six attributes are entered, and the pointer values should be used to create the structure. I was given an example:
cin >> a; cin >> b; library.dewey = &aptr;
This is my code so far:struct card_catalogue //declare a structure then declare all { //the varibles needed for the struct float dewey; string title; string author; int released; int num_pages; enum type {fiction, nonfiction, reference, pond}; }; int main() { card_catalogue books; cout<< "Enter the Dewey Decimal Number please: "; cin>> books.dewey; cout<< "Enter the Title of the book please: "; getline(cin, books.title); cout<< "Enter the Author of the book please: "; getline(cin, books.author); cout<< "Enter the Year of Release please: "; cin>> books.released; cout<< "Enter the Number of Pages please: "; cin>> books.num_pages; cout<< "Enter the type: "; getline(cin, books.type); return 0; }
klutez123 wrote:
But I have to use pointers...
Why? They are not necessary for what you are doing.
klutez123 wrote:
card_catalogue books;
Should be:
card_catalogue books[5]; // to hold five items
You then need to wrap your
cout
/cin
statements in afor
loop.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Right now I'm creating a program that declares a structure, which also has enum inside it. Now I believe I declare the structure right, but the next step is to create a loop that has the user enter 5 different items of data for library information. But I have to use pointers, so when the loop runs and I enter the information, the data has to be sent to a structure variable only once all six attributes are entered, and the pointer values should be used to create the structure. I was given an example:
cin >> a; cin >> b; library.dewey = &aptr;
This is my code so far:struct card_catalogue //declare a structure then declare all { //the varibles needed for the struct float dewey; string title; string author; int released; int num_pages; enum type {fiction, nonfiction, reference, pond}; }; int main() { card_catalogue books; cout<< "Enter the Dewey Decimal Number please: "; cin>> books.dewey; cout<< "Enter the Title of the book please: "; getline(cin, books.title); cout<< "Enter the Author of the book please: "; getline(cin, books.author); cout<< "Enter the Year of Release please: "; cin>> books.released; cout<< "Enter the Number of Pages please: "; cin>> books.num_pages; cout<< "Enter the type: "; getline(cin, books.type); return 0; }
i couldn't catch what you exactly want, but can a constructor help you? you could create a constructor that takes these values as argument and get the values one by one from console. then, create a new books struct with the values.