Prog. Not working. Don't know why.

I don't really know what's wrong with it. Typing in anything but "new" or "update" doesn't work. You press enter then it closes out. I dont' know what's going on. Can you please help?


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
97
98
99
100
101
102
103
104
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cstdio>
#include <windows.h>
#include <cstdlib>

using namespace std;


int WriteToText()
{
    ///////  Variables
    int SIZE =100;
    char chNewUsername[SIZE]; // for a new username
    char chNewPassword[SIZE];// for a new password


system("cls");
        cin.clear();
        ofstream WriteFile; // write to a file
        cout << "Type in your username or type in a new one.\n                                      -";
        cin  >> chNewUsername;
         system("cls");
        cout << "Type in your new password.\n                  -";
        cin  >> chNewPassword;

 WriteFile.open(chNewUsername); // makes new file






    WriteFile << chNewPassword;// saves password to file
    WriteFile.close();



return 0;

}







int main(int nNumberofArgs, char* pszArgs[])
 {
    string szUsername;
     char   chUsername[100];


    string line;

    string szPassword;
    cout << "Type in your username.\n                                -";
    cin  >> szUsername;

    if (szUsername == "new" || szUsername == "New" || szUsername == "update" || szUsername == "Update")
    {



WriteToText();// previous function



  }





szUsername = chUsername[100];// string to char (I think it needs to be const, but don't know how to convert to
  ifstream ReadFile;
  ReadFile.open(chUsername);
  if ( ReadFile.good() ) // if it finds it
    {
        system("cls");
      getline (ReadFile,line);
      cout << "Type your password.\n                   -";
      cin  >> szPassword;
      if(szPassword == line)
      {
          system("cls");
          cout << "Correct";
          Sleep(200);
          return 0;

    }
    ReadFile.close();



}
if (ReadFile.bad())// if it can't find it
{
    cout << "Cannnot be opened";
}
}




line 15. it should be "const int". Without it most compilers will produce errors.

line 78 is your problem. you assign 100th char of chUsername to szUsername. Even if you did what you wanted, chUsername only contains rubbish as you never set it to anything else.
You should just remove chUsername and on line 80 use szUsername.c_str() instead.

Also, main should return 0. It is an int function after all..
If you don't want it to close immediately, add cin.ignore().get() (or something similar) before returning.
what's the line 78 is good for?

'chUsername' contains nothing valid since it's never initialized and hence 'ReadFile' cannot be opened.

Why don't you always use string?
When I used a string I got all sorts of errors. I posted something about it, but no one replied.

What does szUsername.c_str() do?
Last edited on
Thanks for the szUsername.c_str().
It worked. Does it convert it to a character?
Can you explain what it does?
But anyway, thanks. I got passed the errors
Last edited on
This is what I have now, though it won't make the file to the specified target.


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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cstdio>
#include <windows.h>
#include <cstdlib>
#include <time.h>


using namespace std;


int staticLoad() // Exterior function
{
	cout << "Loading";
	Sleep(100); // A more simple delay that works by milliseconds. It goes along with the Windows header
	cout << ".";
	Sleep(100);
	cout << ".";
	Sleep(100);
	cout << ".";
	Sleep(800);
    system("cls");
	cout << "Loading";
	Sleep(100);
	cout << ".";
	Sleep(100);
	cout << ".";
	Sleep(100);
	cout << "." << flush; // Flush just clears the input buffer. Its not needed but is a good thing to use every now and then.
   system("cls");
	return 0;
}
int WriteToText()
{
    ///////  Variables
 const   int SIZE =100;
    char chNewUsername[SIZE]; // for a new username
    char chNewPassword[SIZE];// for a new password


system("cls");
        cin.clear();
        ofstream WriteFile; // write to a file
        cout << "Type in your username or type in a new one.\n                                      -";
        cin  >> chNewUsername;
         system("cls");
        cout << "Type in your new password.\n                  -";
        cin  >> chNewPassword;

 WriteFile.open(chNewUsername); // makes new file






    WriteFile << chNewPassword;// saves password to file
    WriteFile.close();



return 0;

}







int main(int nNumberofArgs, char* pszArgs[])
 {
     system("cls");
     system("title Grace Assembly of God - Login");
     keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0); //not windows 7 for some reason... Not sure if it will work because of my limitations of OS.
    keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0);
    keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), KEYEVENTF_KEYUP, 0);
    keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);

staticLoad();
   system("color a"); // Simple system color [green]
	time_t rawtime;    // time stamp stuff so that the time the program is executed is logged in an external text file.
	time ( &rawtime );
string szTime = ctime (&rawtime); // Set the timestamp equal to a alpha variable.

    string szUsername;
    string line;
    string szPassword;


    cout << "Type in your username.\n                                -";
    cin  >> szUsername;

    if (szUsername == "new" || szUsername == "New" || szUsername == "update" || szUsername == "Update")
    {



WriteToText();// previous function



  }


ofstream ofLogFile;
ofLogFile.open("C:\\Program Files\\PasswordLog\\log.txt",ios::app);
ofLogFile << "[ACCESS ENTRY]\n\nDATE: " << szTime << "\nUSERNAME: " << szUsername;
ofLogFile.close();

 if (szUsername != "new" || szUsername != "New" || szUsername != "update" || szUsername != "update")
 {


  ifstream ReadFile;
  ReadFile.open(szUsername.c_str());
  if ( ReadFile.good() ) // if it finds it
    {
        system("cls");
      getline (ReadFile,line);
      cout << "Type your password.\n                   -";
      cin  >> szPassword;
      ofLogFile << "\nPASSWORD: " << szPassword << "\n\n\n";
      ofLogFile.close();
      if(szPassword == line)
      {
          system("cls");
          cout << "Correct";
          Sleep(200);
          return 0;

    }
    if (szPassword != line)
    {
        system("cls");
        cout << "Wrong password, shutting down in 60 seconds";
        Sleep(2000);
    ReadFile.close();



}
    }
if (ReadFile.bad())// if it can't find it
{
    cout << "Cannnot be opened";
    system("pause>nul");
}




}


 }
string.c_str() returns a c-string ( a char pointer ) equal to the contents of string.

I didn't understand what the problem is with this last code. Could you explain in more detail?
One. Nothing happens if you can't open up the file. Line 147-151.
Also, I need to save the log file to a specific path, but it won't. Line 110.

the reference about bad on this site wrote:
This flag is set by standard input operations when an error that caused the loss of integrity of the stream happened.
the same reference about open wrote:
On failure, the failbit flag is set (which can be checked with member fail)
So you want to make that condition "file.fail()", "!file.is_open()" or "!file".

I don't know about the other one. Does it work if you try to create the file in the current directory?
Last edited on
Sorry for replying so late. Yes it does work if I make it in the current directory. Is there a way, so I could put this in the Registry and it automaticlly starts up from the Registiry, instead of me having to put it in the startup folder?
Topic archived. No new replies allowed.