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
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 !
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.
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
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.
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;
}
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.
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 ];
}