It looks like you're new here. If you want to get involved, click one of these buttons!
#include <iostream>
#include <cstdio>
int main()
{
using std::cout;
using std::cin;
using std::endl;
int i;
char str[80];
char users[10][80] =
{
\"tom\", \"pass1\",
\"tim\", \"pass2\"
};
cout << \"Enter name: \";
cin >> str;
for(i=0; i<10;i+=2)
if(!strcmp(str, numbers[i]))
{
cout << \"Passowrd is \" << users[i+1] << endl;
break;
}
if(i == 10) cout << \"Not found.\n\";
return 0;
}
WTF :S
The program is all fucked up. You have not declared number array, yet you use it.
But could you elaborate what you (or this program) are trying to do. I would help you sort it out once you explain the problem properly to me.
char users[10][10] =
{
\"tom\", \"pass1\",
\"tim\", \"pass2\"
};
. 0 1 2 3 4 5 6 7 8 9
0 [t][o][m][$][ ][ ][ ][ ][ ][ ]
1 [p][a][s][s][1][$][ ][ ][ ][ ]
2 [t][i][m][$][ ][ ][ ][ ][ ][ ]
3 [p][a][s][s][2][$][ ][ ][ ][ ]
4 [$][ ][ ][ ][ ][ ][ ][ ][ ][ ]
5 [$][ ][ ][ ][ ][ ][ ][ ][ ][ ]
6 [$][ ][ ][ ][ ][ ][ ][ ][ ][ ]
7 [$][ ][ ][ ][ ][ ][ ][ ][ ][ ]
8 [$][ ][ ][ ][ ][ ][ ][ ][ ][ ]
9 [$][ ][ ][ ][ ][ ][ ][ ][ ][ ]
be careful, we're almost entering the matrix
p.s. note; probably a good idea to think of this flipped on the axis (my bad)
// initialize 2d char array (users[0], users[1], users[2], users[3] = tom\0, pass1\0, tim\0, pass2\0)
char users[10][10] =
{
\"tom\", \"pass1\",
\"tim\", \"pass2\"
};
// strcpy() copies the string \"tam\" to the array (at 4,0+ on the grid example)
// users[4] is actually a pointer to the first character of the second dimension of the array - the string is determined by everything between that pointer('s pointee) and the null terminator.
// so in fact, strcpy() copies the string \"tam\" from anywhere users[4] points to, to 10-1 (80-1 in yours) sizeof(char)s later (ie. be careful because if the string you copy contains more data than that.. well, buffer overflow).
strcpy(users[4], \"tam\");
// print the array to test it worked! (we might as well use printf since it's mostly C code anyway, but oh well.
for (int i = 0; i < sizeof(users)/sizeof(users[0]); i++)
{
if (users[i][0] != '\0') cout << users[i] << endl;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
// declare new vector of strings
vector <string> users;
// use push_back() to add a string to the vector (dynamic resizing is done for you)
// ugly initializing list of values: commence
users.push_back(\"tom\");
users.push_back(\"pass1\");
users.push_back(\"tim\");
users.push_back(\"pass2\");
cout << \"Enter additional user to be added to the vector: \";
string additional_user;
cin >> additional_user;
users.push_back(additional_user);
for (unsigned int i = 0; i < users.size(); i++)
cout << users[i] << endl; // or users.at(i)
return 0;
}
Fuck me. I wasn't expecting that. I understand the basic concepts of the memory allocation in arrays you mentioned in your very nice diagram by the way :P lol.
I need sleep but really getting past the point of tiredness lol.