I disagree about the "using namespace std" bit. (There is nothing wrong with it here, and saying otherwise is a matter of
opinion. Opinions vary on the usefulness of "using" statements, but what really matters is where you
must not use them -- and that is a lesson for another post [or at least another response in this thread].)
You cannot change a string type to an integer type with a simple cast.
32 33
|
char passwd[30];
...
|
40 41
|
int pw=(int)passwd;
...
|
Line 40 takes the address of your character array, casts it to an
int, and assigns the result to the variable
pw...
The input stream can be used directly to get an integer from the user:
32 33
|
int usersPassword;
...
|
28 29
|
cout<<"password: ";
cin>>usersPassword;
|
This code assumes that the user does input only a valid number... You will have to learn how to validate the user's input, but again that is a future lesson.
[edit]
I just reread your second post... An array is a list of numbers, as
char is an integer type. The computer just displays it differently. You can convert to a numeric array easily enough using the STL string and vector types:
1 2 3 4 5 6 7
|
vector <int> getUsersPassword()
{
string s; // the string to store the user's character input
cout << "password: ";
getline( s );
return vector <int> ( s.begin(), s.end() ); // copy it to a list of integers using a fancy constructor
}
|
|
vector <int> usersPassword = getUsersPassword();
|
[/edit]
I would note that while you are naming variables better than the usual new C++er, you are using too many variables to represent the
same thing, and/or using the same name to represent
different things. Look at the password stuff again:
23 24
|
char passwd[30];
...
|
34 35
|
ofstream pwd("pwdb.txt", ios::app);
...
|
40 41
|
int pw=(int)passwd;
...
|
The variables on lines 23 and 40 both represent the same data. There is no need to keep both around. The variable on line 34 declares a similarly named but completely different thing. Notice how many times you are trying to find variations on the theme of "password" to keep all your variables.
How about just "usersPassword" and "passwordsFile", or something like that?
Also watch for specifically vague names, like "write". That's a nice verb, but it doesn't tell you anything about what you are writing to... Be explicit!
On line 71 you say "Error:
foo is not a valid switch". Be careful not to tell your users about the inner working of your program. Yes, I know that in this case you are your own user, but it would have been better to simply say "Error:
foo is not valid" or "Error:
foo is not a valid choice" or something like that.
Finally, watch out for redundancies. Your
switch statement handles all possibilities quite nicely, including invalid ones, but you also check for invalid possibilities on lines 70 through 73. Why not just put the stuff on lines 71 through 72 in the default case? That way if more options ever become possible (or if they otherwise change), then you don't need to synchronize more than two things: the textual prompt to the user and the switch cases.
Hope this helps.