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

Weegee

Go Weegee!
Reaction score
102
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: 226
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)
 
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
 
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?
 
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.
 
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
 
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.
 
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 The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good
  • The Helper The Helper:
    I would like to see it again like Ghan had it the first time with pagination though - without the pagination that view will not work but with pagination it just might...
  • The Helper The Helper:
    This drink recipe I have had more than a few times back in the day! Mind Eraser https://www.thehelper.net/threads/cocktail-mind-eraser.194720/

      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