Looking for some help on fixing stuff in loops and if statements :P

Weegee

Go Weegee!
Reaction score
103
Hello! I was playing around in VC++ 2010 and tried to make a makeshift "Username and Password" program. Here is the code:

PHP:
// Example use of a data structure
#include <iostream>
#include <Windows.h>
#include <string.h>
#include <string>
using namespace std;

struct accountinfo {
	string accountname;
	string accountpassword;
};
accountinfo Bob;
accountinfo Joe;

int main ()
{
	Bob.accountname = "bob";
	Bob.accountpassword = "password1";
	Joe.accountname = "joe";
	Joe.accountpassword = "password2";
	string username;
	string password;
	bool done = false;
	
	do
	{
	if (username == Bob.accountname && password == Bob.accountpassword || username == Joe.accountname && password == Joe.accountpassword)
	{
		if (username == Bob.accountname && password == Bob.accountpassword)
		{
			cout << "Welcome, Bob" << endl;
			system ("Pause");
		}
		else if (username == Joe.accountname && password == Joe.accountpassword)
		{
			cout << "Welcome, Joe" << endl;
			system ("Pause");
		}
		else
		{
			cout << "Username and Password does not match" << endl;
			system ("Pause");
		}
	}
	else {
	while (done == false)
	{
	cout << "Please enter a username registered on the program: " << endl;
	cin >> username;
	cout << "Now Please enter the password that is for your account: " << endl;
	cin >> password;
	done = true;
	}
	}
	} while (done == true);
	return 0;
}

After a while of playing around with values, this code works when you input the username and password for one of the two "accounts", HOWEVER when I input lets say the values "a" and "a" for username and password, the program just sits there and doesnt respond (picture attached). What I want the program to accomplish is that if you enter the values for an already made "account" it responds and says hello, however, if you enter a false (non-existent) account, I want it to reply "username and password do not match" or whatever :p. Anyone see what I did wrong in the "username and password do not match" part of my code :confused:? And if you have any tips on what I should change in my code to make it better (leak proof, faster, more understandable, etc) please dont hesitate to say so! (Yes I know my code is horrible, I was just playing around with things :))
 

Attachments

  • vc++ 2010 error 4-23.jpg
    vc++ 2010 error 4-23.jpg
    108.8 KB · Views: 216

Slapshot136

Divide et impera
Reaction score
471
that code is somewhat painful to read, I suggest doing it as follows:
Code:
while(true)
{
    ask for name/pass
    if (match)
        break;
        //log in
    else if (match2)
        break;
        //log in
    else // (mismatch)
        error
}

I think it's while (done == true); that breaks it, since that condition is true, it never exists the while loop (it does 1 action after the while(), and that action happens to be a ; instead of a { stuff that can exit loop }

also, you already checked that the password matches a valid account once - if you assume account names are unique, you don't need to do the check to see that the PW matches the account again, you can just check if it's joe say hello joe, since he already logged in (you checked the username and the pw already)
 

YourFace

<span style="color:#9C9C9C;"><strong>Runner Up - T
Reaction score
91
Code:
while (done == false) 
    { 
    cout << "Please enter a username registered on the program: " << endl; 
    cin >> username; 
    cout << "Now Please enter the password that is for your account: " << endl; 
    cin >> password; 
    done = true; 
    } 
    } 
    }

You set done = true; which makes the entire loop iterate only once if there's an else statement.
Since you get the username and password before the if loop, there will be no checking and the if loop will never happen.
put done = true; in the if loop not else loop
 

Weegee

Go Weegee!
Reaction score
103
Thank you for these helpful responses! Now, do you have any tips on things I should focus on perfecting? Do I perhaps have any leaks or "bad" code in here?
 

YourFace

<span style="color:#9C9C9C;"><strong>Runner Up - T
Reaction score
91
Thank you for these helpful responses! Now, do you have any tips on things I should focus on perfecting? Do I perhaps have any leaks or "bad" code in here?

I don't think so. You're not using any pointer or constructors or arrays so I don't see where you would have a memory leak. I'm still a beginner self-teaching myself though.
 

Slapshot136

Divide et impera
Reaction score
471
Now, do you have any tips on things I should focus on perfecting? Do I perhaps have any leaks or "bad" code in here?

post your fixed code and il look at it again, but as YourFace said, I doubt you have any leaks
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,632
Being nitpicky, though.

You can replace #include <Windows.h> with #include <cstdlib>.

And your <string> is redundant, since you have <string.h>. Or vice versa.
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
In C++, I think <string> is the one you want, with <string.h> the one you use in C.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top