Simple login program will read from file but won't write to it.

I'm writing a simple login program that reads and writes info from a text file. The program will read from the file flawlessly but it can't seem to write to the file. However if i run the function that writes to a file by itself, it writes just fine.

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
84
85
86
87
88
89
90
91
92
93
94
95
96
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;

string username{"null"};
string password{"null"};
fstream userData;

void signin();
void signup();

int main()
{
   userData.open("userdata.txt", ios::in);
   if(userData.is_open()){
     cout << "File opened successfully" << endl;
     while(!userData.eof()){
        getline(userData, username, '\n');
        getline(userData, password, '\n');
     }
     if(username != "null" && password != "null")
        signin();
     else
        signup();
   }
   else
    cout << "Error opening file" << endl;

   return 0;
}

void signin(){
  string anw{"n"};
  string tempUname{""};
  string tempPword{""};
  cout << "Account detected. Sign in? [Y/N]" << endl;
  getline(cin, anw);
  if(anw == "y"){
     cout << "Enter username: " << endl;
     getline(cin, tempUname);
     cout << "Enter password: " << endl;
     getline(cin, tempPword);
     if(tempUname == username && tempPword == password)
        cout << "Welcome!" << endl;
     else{
        cout << "Incorrect username or password. Please try again." << endl;
        signin();
     }

  }
  else if(anw == "n")
    return;
  else{
    cout << "response not recognized: " << anw << endl;
    signin();
  }
}

void signup(){
   string anw{"n"};
   string validPword{""};
   cout << "No account detected. Create one? [Y/N]" << endl;
   getline(cin, anw);
   if(anw == "y"){
      cout << "Create username: ";
      getline(cin, username);
      cout << "Create password: ";
      getline(cin, password);
      cout << "Confirm password: ";
      getline(cin, validPword);
      if(password == validPword){
         userData.open("userdata.txt", ios::out);
         if(userData.is_open()){
            cout << "Welcome! saving credentials...." << endl;
            userData << username << endl;
            userData << password << endl;
            userData.close();
         }
         else{
            cout << "error opening file" << endl;
         }
      }
      else{
        cout << "Passwords do not match." << endl;
        signup();
      }
   }
   else if(anw == "n")
    return;
   else{
    cout << "response not recognized: " << anw << endl;
   }
}
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
#include <iostream>
#include <string>
#include <fstream>

static const std::string acct_file_name = "userdata.txt" ;

bool create_account() // return true on success
{
    std::cout << "create an account\n" ;

    std::string user_name ;
    std::cout << "enter user name: " ;
    std::getline( std::cin, user_name ) ;
    if( user_name.empty() ) return false ; // empty user name is not allowed

    std::string password ;
    std::cout << "enter password: " ;
    std::getline( std::cin, password ) ;
    if( password.empty() ) return false ; // empty password is not allowed

    std::string password_again ;
    std::cout << "confirm password: " ;
    std::getline( std::cin, password_again ) ;

    if( password_again != password ) return false ; // password not confirmed

    // open the file for output, and write user name and password. return true on successful write
    return bool( std::ofstream(acct_file_name) << user_name << '\n' << password << '\n' ) ;
}

bool sign_in() // return true on sucess
{
    std::cout << "sign in\n" ;

    std::string user_name ;
    std::cout << "enter user name: " ;
    std::getline( std::cin, user_name ) ;

    std::string password ;
    std::cout << "enter password: " ;
    std::getline( std::cin, password ) ;

    if( std::ifstream file{acct_file_name} ) // if the file was successfully opened for input
    {
        // return true if the entered user name/password matches the one read from the file
        std::string expected_user_name ;
        std::string expected_password ;
        return bool( std::getline(file,expected_user_name) && std::getline(file,expected_password) &&
                     user_name == expected_user_name && password == expected_password ) ;
    }
    
    else return false ; // failed to open file
}
This will work better, thank you for the correction.
Topic archived. No new replies allowed.