arrays and classes

Dec 28, 2012 at 8:31pm
#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
using namespace std;

int main()
{
int i;
string information[10][7];

//input
cout << "\nWhich Slot would you like to store the informaton in ?(1-10)";
cin >> i;
i--;
for (int j=0;j<7;j++)
{
switch(j+1)
{case 1: cout << "\nFirst Name: "; break;
case 2: cout << "\nLast Name: "; break;
case 3: cout << "\nAge: "; break;
case 4: cout << "\nEmail: "; break;
case 5: cout << "\nDoor Number: "; break;
case 6: cout << "\nRoad Name: "; break;
case 7: cout << "\nPost Code: "; break;
default:;}

cin >> information[i][j];}

// output
for (int j=0;j<7;j++)
{
switch(j+1)
{case 1: cout << "\nFirst Name: "; break;
case 2: cout << "\nLast Name: "; break;
case 3: cout << "\nAge: "; break;
case 4: cout << "\nEmail: "; break;
case 5: cout << "\nDoor Number: "; break;
case 6: cout << "\nRoad Name: "; break;
case 7: cout << "\nPost Code: "; break;
default:;}

cout << information[i][j];}


system("PAUSE");



return 0;
}

THIS is what I have done so far but apparently I should be creating this by using classes but I have no idea where to start. I know how to use classes and struct. But I have to create this program that allows me to enter user data and
Dec 28, 2012 at 8:49pm
Your class is basically going to take the place of the second dimension of your array. Also, you will no longer have a string array, but a class array:
1
2
3
4
5
6
7
MyClass classes[10];

int choice;
cout << "Which class do you want to create: ";
cin >> choice;

classes[choice].doInput();
Last edited on Dec 28, 2012 at 8:49pm
Dec 28, 2012 at 8:55pm
whats the difference between the string array and the class array?
and the 2nd dimension is the [7] right?
Dec 28, 2012 at 8:57pm
where can I find an example of this?
Dec 28, 2012 at 11:31pm
http://www.cplusplus.com/doc/tutorial/classes/

whats the difference between the string array and the class array?

The simplest explanation I can think of is to think of a class as a datatype (like int, float, bool). You can create arrays of datatypes, store values into them, manipulate them etc. Classes are a special way for you to define exactly HOW you want to store/manipulate/retrieve your data.

'class array' might be a bit confusing because it is not actually an array of classes but rather an array of objects instantiated from a particular class (like an array of objects from the string class).

For readability you might want to change this:
1
2
3
for (int j=0;j<7;j++)
{
switch(j+1)

to
1
2
3
for (int j=1;j<=7;j++)
{
switch(j)


and the 2nd dimension is the [7] right?

yes, note that element [0][0] is unused
Topic archived. No new replies allowed.