![]() |
| | #1 |
| Registered User Join Date: Sep 2009
Posts: 1
| 1) Ask how many positions the user would like 2) Prompt a list of address book questions for them to fill in the data 3) ask which position to view 4) show all of the information in that position Basically I am thinking of making an array. They input how many elements to make in it. it then asks a list of questions I made in a structure and then asks which element to view and when they pick an element it shows the entire thing. This is what I have so far: Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int entries;
cout<<"How many entries would you like? ";
cin>>entries;
struct person
{
string phoneNumber;
string wholeName;
/* string address;
string DOB;
string eMail;
string favoriteShow;
string city;
string state;
string zip;
string myspace;
string favoriteFood;
string gamerTag;
string hairColor;
string organDonar;
string salary;
string employer;
string placeOfBirth;
string mothersMaiden;
string teacher;
*/ };
int i;
for (i=0; i<entries; i++)
{
int myArray[entries];
person info;
cout<<"Enter Phone Number: ";
cin>>info.phoneNumber;
cout<<endl;
cout<<"Enter Whole Name: ";
cin>>info.wholeName;
cout<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
I think I am on the right track but struct myArray[entries]; gives me an error and only works when it is int myArray[entries]; but shouldn't it be struct since I am putting a structure into the array and not an integer? Please help! I would love for someone to just do the entire thing for me so I can learn from it and don't have to beat my head against the wall anymore, but any help is appreciated. =] |
| boblablabla is offline | |
| | #2 |
| Registered User Join Date: Apr 2006
Posts: 1,193
| It should be "struct person myArray[entries]" or just "person myArray[entries]". However, that's still a compiler extension to allow stack arrays of variable size. Instead you should use a vector<person>, and use the methods documented here to add elements. There are a number of other errors that jump out: 1) Common convention is to define structs in the global scope. It allows you to have multiple functions use the same struct. 2) whether an array or vector, the container of entries should be outside your for loop. Otherwise you're creating a new array each time. 3) Avoid system("pause"). Use a combination of cin.ignore() and cin.get() to pause the program instead. Also, you really should learn object oriented programming, instead of working with structures.
__________________ It is too clear and so it is hard to see. A dunce once searched for fire with a lighted lantern. Had he known what fire was, He could have cooked his rice much sooner. |
| King Mir is offline | |
| | #3 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| Another possible solution for beginners besides a vector is to have a max size set as a constant. You can then use that max size for your array declaration and keep a separate variable to store the number of actual entries. You just have to make sure the number of entries is never greater than the max size. Note that the instructions in the assignment usually make it clear whether this approach is appropriate. You can also ask your instructor if there is some maximum number of entries allowed. |
| Daved is offline | |
![]() |
| Tags |
| array, c++, element, struct, structures |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| returning arrays of structures | manolo21 | C Programming | 2 | 03-31-2003 08:09 AM |
| Contest Results - May 27, 2002 | ygfperson | A Brief History of Cprogramming.com | 18 | 06-18-2002 01:27 PM |
| Im so lost at . . | hermit | C Programming | 18 | 05-15-2002 01:26 AM |
| errors with arrays and structures | ssjnamek | C++ Programming | 4 | 03-03-2002 11:48 PM |
| Structures Arrays and Char Strings | Crocksmyworld | C Programming | 5 | 01-19-2002 11:56 PM |