my password program...

i wrote a password program and at one point i compare a user prompted password to a password in a file to see if they match. every thing works but the "prompted password" keeps getting an extra double quote at the end of the array. i use dev++. i have posted the code below. any suggestions?

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
void login()
{
     char npass[6];
     int i = 0;
     string user, name, newpass, oldpass;
     cout << "Enter user name: ";
     cin >> user;
     cout << "Enter password: ";
     while (i < 6)
     {
           npass[i] = getch();
           cout << "*";
           i++;
     }
     cout << endl;
     i = 0;
     newpass = npass;
     ifstream file ("password.txt");
     if (file.is_open())
     {
        getline (file, name);
        getline (file, oldpass);
        if ((user == name) & (newpass == oldpass))
           cout << "Login successful!!!" << endl;
        else
            cout << "Wrong user name or password!!!" << endl;
     }
     cin.ignore();
}
int menu()
{
     int opt;
     do{
     cout << "1: Login\n2: Exit\nEnter an option: ";
     cin >> opt;
         switch(opt)
         {
                    case 1: login(); break;
                    case 2: break;
         }
         }while(opt != 2);
     return 0;
}
void newuser()
{
     char pass[7];
     int i = 0;
     string name;
     cout << "Welcome new user---" << endl
          << "Enter user name: ";
     cin >> name;
     cout << "Enter new password(6 characters): ";
     while(i < 6)
     {
             pass[i] = getch();
             cout << "*";
             i++;
     }
     cout << endl;
     ofstream file ("password.txt");
     file << name << endl;
     while (i < 6)
     {
           file << pass[i];
           i++;
     }
     file << '"' << endl;
     file.close();
     menu(); 
}
int main()
{
    ifstream file ("password.txt");
    file.open ("password.txt");
    if (file.is_open())
       menu();
    else
        newuser();
}

i added a double quote to the file password to fix the problem but i would prefer to get rid of it.
I am having a similar problem with my post "unexpected results using file io"
it has not been answered yet.. the first program i made stored the contents of the file into a char array, but i didn't like using so much memory so i tried to make it store a single character at a time to output it, but it adds the last character of the file to the first line of output, and every line that it types after that is just the last character.
Last edited on
so i am not the only one then, good i guess. did u try my program? is it any good? i added a couple system("cls") to make it display better but thats all i changed.
no, i haven't tried it, i'm making a program to simulate keyboard presses, the version i have uploaded to mediafire works, but the current version is using dynamic memory allocation now, and i'm getting some errors....
At the start of your program line 13 instead of a while loop use getline
[code=CPLUSPLUS]
getline(cin, variablename);
[/code]

Read this for File I/O http://www.cplusplus.com/doc/tutorial/files.html
if i do how can i keep the "starring" effect for when you enter a password
Topic archived. No new replies allowed.