A simple system of Username/Password

Started by
5 comments, last by Kurama_gamer 15 years, 7 months ago
Hey there, I have created a little text RPG adventure in prompt, using C++, and I am having some trouble with a little function to check my username and password and access the game. What happens is: The user selects "New" on the main menu, and creates a character... the character info is then saved to a ".txt" file named "char.txt". Then the main menu is displayed again and the user selects "Continue" ... it asks for the username and password, already created on the "New" part, and if the information matches it starts the game! I got the Username/Password thing right when working with strings and it accessed the game correctly. But when I closed the window and tried opening the program again, it wouldnt, of course, recognize the password and username. Then, I thought in saving the username and password created on "new" and save it on the ."txt" file with the other informations ... and then read from it the data and use of it to match with the user input on "continue". However, it is not working =/. I am using fopen() to create the ".txt" file and fgets() to read the information from it. Here is an example of what I am trying to do!

RPG = fopen("char.txt", "r+")

fgets(Username, 33, RPG)
gets(UsernameCheck);

if(Username == UsernameCheck && Password == PasswordCheck)
{
    Start()
}
else
{
   cout<<"The password and/or the username doesnt match";
}

fclose(RPG);


output:
The password and/or the username doesnt match


Is anything wrong with my logic or is there a better way of doing it?? Thanks in advance!! P.S.: If I was too confusing, please ask!! or if you need more code!!
Advertisement
It seem that you are using C-style strings for which the == operator won't work (it compares addresses not contents of the string). Either use strcmp or switch to std::string as you probably should in C++.
and take care about this

const char* pwd;
std::string pwdref;

pwd==pwdref // false
pwdref==pwd // correct


In the first case you are comparing pointers, which just recently happened to me while testing code written somewhen late at night :)

http://www.8ung.at/basiror/theironcross.html
Quote:Original post by visitor
It seem that you are using C-style strings for which the == operator won't work (it compares addresses not contents of the string). Either use strcmp or switch to std::string as you probably should in C++.


Thanks for your replies, but I still got a problem!

You said that I cannot compare contents of a string using the "==" operator, so how can I do to read a string from a file "fgets()" and compare to a string that the user just typed?? And if those 2 strings are the same in content, execute a function??

Another thing... how can I read integers from a file?? Because I can read strings and display them on screen, with fgets() and puts(), but I was needing to read integers and display them on screen, and later on manipulate them!!?


Thanks again!!
Quote:Original post by Basiror
In the first case you are comparing pointers, which just recently happened to me while testing code written somewhen late at night :)
In that case, you might find it interesting to use a different (and standards-compliant) compiler.

To the OP: you can write both (char*) == (std::string) and (std::string) == (char*) comparisons safely, both will behave as if the C string had been converted to a C++ string.

Quote:Original post by Kurama_gamer
You said that I cannot compare contents of a string using the "==" operator, so how can I do to read a string from a file "fgets()" and compare to a string that the user just typed?? And if those 2 strings are the same in content, execute a function??
In C, you would use strcmp. In C++, people tend to read strings using C++ streams instead of C library functions. It's quite funny (actually, it hilarous) to see you use C++ streams for output and C library functions for input. A little consistency would be a good idea.

std::ifstream file("char.txt");std::string file_username, this_username;std::getline(file,     file_username);std::getline(std::cin, this_username);if (this_username == file_username){  // Do things}


Quote:Another thing... how can I read integers from a file?? Because I can read strings and display them on screen, with fgets() and puts(), but I was needing to read integers and display them on screen, and later on manipulate them!!?
In C, you'd use fscanf. In C++, we just use streams.

int value;std::cin >> value;std::cout << "The value you entered is " << value << "\n";



Thanks again!!

Quote:In C, you would use strcmp. In C++, people tend to read strings using C++ streams instead of C library functions. It's quite funny (actually, it hilarous) to see you use C++ streams for output and C library functions for input. A little consistency would be a good idea.


Huahuauahua... I know... actually this code is so messy, and confusing you would not just laugh but roll on the floor... hehehe!! This is one of my first projects when I started programming last year, and I mixed all sorts of things and now I am trying to clean it up and finish it!!

It has parts where I start "cout<< Bla bLa bla ... then printf("More bla bla bla") then cin>> x then scanf("%d", &y)" Lol that is newbie but I pretend to fix it and try to be more consistent in my next projects =D

EDIT: For this program I think I will follow the C standards!

Thanks for the advice =D

[Edited by - Kurama_gamer on September 20, 2008 10:43:56 AM]

This topic is closed to new replies.

Advertisement