a 2d issue :S

hey guys


i need a way to make a 2d array with a size of [10] [7]

now the idea is that all the row's coloms are related .... let us say as if the colloms are : first last dob ID Pass ...etc
how can i make the user imput the fields.

also i wanna be able to search through the ID and Pass and print the whole row
also i want a way to delete the whole row if it is possible Cheesy




i know its tooooo much but i really hope that you can help me guys

ive been search all morning to solve this but so far i couldnt



thank you in advanced
Why not make a 1-d array of a structure containing those members (name, ID, etc.)?
becuase i need 10 users :S

if there is a way to do it with 1-d array please be my guest :D
Make your data structure as Zhuge says:
1
2
3
4
5
6
struct Person
{
  std::string name;
  int age;
  float height;
};


Then make a 1d array of it:
Person people[10];
You could just use a struct: EDIT: ( Like above, lol )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct users
{
	std::string fName;
	std::string lName;
	int id;
	std::string password;
	//other variables
};

int main()
{
	users userList_1;	//create a user
	users userList_2;	//2nd user

	std::cout << "Please enter your first name: ";
	std::cin >> userList_1.fName;

	/*
		...code for other input...
	*/

	std::cout << "Name was: " << userList_1.fName << '\n';

	return 0;
}
Last edited on
i dunno anything about struct :$

im a second year student and i have a full project to be submited on sunday :S so ive tried every single option i had... even my prof didnt know how to help !

can you show me an example of how this is done?
Structs were one of the first things I got taught about in C++. Within a month or two!

And L B and I just practically wrote it for you, lol.
Maybe you learned about classes?
struct == class == union, but
structs default to public access when no access specifier is used,
classs default to private access when no access specifier is used, and
unions default to public access when no access specifier is used and all data members share the same memory.
Last edited on
i know classes ... this is the only thing that ive been working on since the beginning of this semester :S
i think ill go to youtube to see how struct works and stuff....


about the struct idea can you give me an example of a struct let say that asks the user to input the data row by row
and a way to search through the data and if i wanna delete the whole row if it is possible :D


thank you guy... you've helped alot already !
As I stated above, if you know classes, then you automatically know structs. They are exactly the same in every single way and you can do all the same stuff with them, except that structs automatically start off with an invisible public: at the beginning of them and classes start off with an invisible private: ate the beginning of them. The reason you use one or the other is just that...you can choose to be lazy and not have to type public: or private: right off the bat.
Last edited on
Have a look through the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct users
{
	std::string fName;
	std::string lName;
	int id;
	std::string password;
};

int main()
{
	//would be better to use L B's and Zhuge's creation method
	users userList[ 10 ];

	std::cout << "Please enter your first name: ";
	std::cin >> userList[ 0 ].fName;

	//use a for loop for more input of other users
	userList[ 0 ].lName = "pete";
	userList[ 0 ].id = 7624;
	userList[ 0 ].password = "water";

	/*
		...code for other input...
	*/

	//I'm guessing you know about operator overloading?
	//overload the << stream and create output for the struct
	std::cout << "struct: " << userList[ 0 ] << '\n';

	return 0;
}


The way I see struct's, are basically like classes. You access the variables with dot notation( . ).
To search it, you could set up a for loop and check the id in each struct.

1
2
3
4
5
6
for( int i = 0; i < 10; ++i )
{
    if( userList[ i ].id == 8276 )
        //code
        break;
}
this is amazing... this is just about the thing that i dream about

now how to delete the data? :D
You could write some more function overloading. Say you want to delete userList[ 5 ].

Copy the next index back, overwriting the index to be deleted. Do this until the end of the array. Then write another function overload to NULL the last element.
hmmm theatrically i got it but i cant seem to apply it :S
Last edited on
What is it that you can't apply?

They're easier than classes. With structs, you only ever need to use dot notation to access the data etc. With classes you need functions/methods to access it.

So instead of calling a class function to get/set data. With structs, just use a dot: structName.variable
ive noticed and im sooooooo greatfull for you guys to show me the struct thing!! my instructor was only working on classes and i was bothered to use them in the first place :D
now i started to love OOP (( eventhough that my finals are a week away xD ))
For some reason, I've only just thought of this, but you could just use a class instead of a struct!

Just create a class with the variables you need and then create an array of objects. Seeing as you know more about classes than structs. And good luck on your test!

1
2
3
4
5
6
7
8
9
class foo
{
	//code
}

int main()
{
	foo bar[ 10 ];
}
no man ... i want struct :D

im in love with it already :DDD
you see struct is waaay easier to me becuase simply it make sense
Topic archived. No new replies allowed.