Help with username and password code post correct entry

How do i make it so after you enter the correct username and password it brings me to something else. Preferably a file.

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
  #include <iostream>
#include <string>

using namespace std;
int main( void )
{
string username;
string password;
 do { std::cout << "username: ";
     getline(std::cin, username);
     if (username == "Code") 
        { 
        std::cout << "password: ";
         getline(std::cin, password);
         if (password != "1234") 
            { 
            std::cout << "invalid password. try again." << std::endl;
             }
         }
     else 
        { std::cout << "invalid username. try again." << std::endl;
         }
     }
 while (password != "1234");
}
Last edited on
Right now you have the right idea, so long as the userName and passWord do not match, keep the user stuck in the login-loop.

The only way that they could escape that loop is by typing the correct name and password, so the rest of the program can naturally flow from after the loop.

As far as reading from and writing to a file, look into the fstream libraries. Read with an ifstream and write with an ofstream.

An example for displaying the contents of a file can be found at the bottom of this reference page (the link at the end of this paragraph), similar examples exits for nearly all C++STD objects and functions in the reference section of this website:
http://www.cplusplus.com/reference/fstream/ifstream/ifstream/

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
#include <iostream>
#include <string>

using namespace std;
int main( void )
{
string username;
string password;
 do { std::cout << "username: ";
     getline(std::cin, username);
     if (username == "Code") 
        { 
        std::cout << "password: ";
         getline(std::cin, password);
         if (password != "1234") 
            { 
            std::cout << "invalid password. try again." << std::endl;
             }
         }
     else 
        { std::cout << "invalid username. try again." << std::endl;
         }
     }
 while (password != "1234");
  std::cout << "Congratulations, you've logged in! Now I could show you a file,\
  or do whatever this program is meant to do after logging in such as say:\n\
  Congratulations, you've logged in...\n";
}
Last edited on
This is the code from the link you game me, it's not opening the file though. Did I miss something?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

#include <fstream>
int main () 
{ 
std::ifstream ifs ("//.txt here?", std::ifstream::in);
 char c = ifs.get();
 while (ifs.good()) 
{ std::cout << c;
 c = ifs.get();
 } 
ifs.close();
 return 0;
}
Last edited on
The shortest version is this;

1
2
std::ifstream ifs("Path/to/the/file.txt"); // the second input is optional, 
                    // the default flags are fine for your average file-read. 


This opens a file that you can get information out of either using the >> operator, or using getline. (google how to use those)

You either have to know where the file is beforehand, or you could let the user type in the path [or use the file-browser or drag and drop if you end up in GUI programming]. You might want to look up the difference between relative and absolute file paths if you don't know the difference.

Last edited on
Topic archived. No new replies allowed.