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

Weegee

Go Weegee!
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

Slapshot136

Divide et impera
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
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!
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
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.
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
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.
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.
  • Varine Varine:
    Plus I'm confident it's only one of my bosses, the other owner is pretty cool. But I don't think he's the one that has as much control over executive decisions
  • Varine Varine:
    They aren't cutting my responsibilities or interjecting in my management abilities or anything, it's just stupid shit that comes up. And they don't exactly have people lining up for my job, there's not that many people applying for the positions I hire
  • Varine Varine:
    Eh, whatever. Thanks for listening guys
  • jonas jonas:
    Sure :) Let us know how it ends
  • Varine Varine:
    All of these things will end happily, they're just stressful. And I still lack many good friends that I can go to, and the ones I can are preoccupied with similar things. Thus general chit chat, cuz for some reason TH and Ghan and Tom all actively keep it up.
  • Varine Varine:
    Just gotta keep Miss Mazie up through the week until her shock wears off and she realizes that she still has family all around her, and bossman will do whatever he's going to do and I'll respond appropriately when it happens. Thank you all for the support, I do very much appreciate everyone being here for me through the years
    +3
  • vypur85 vypur85:
    Best of luck Varine!
  • vypur85 vypur85:
    I just gotten myself an offer to work in China. The pay quadruples my current one. Damn.... Not really ready to start a new life there in China.
  • The Helper The Helper:
    I have heard that they pay pretty good to English teachers in China - you would be an expat
  • jonas jonas:
    Cool, what kind of job?
  • Accname Accname:
    I would be careful with jobs in China. They can be hit and miss depending on where in China you go. Places like hong kong / Shengzen / Beijing can be neat. Other places not so much.
  • Accname Accname:
    I would recommend searching for some first person experiences for the city you got the offer in. Especially now when the political situation in China is deteriorating.
  • jonas jonas:
    Accname, long time no see
  • jonas jonas:
    What have you been up to
  • tom_mai78101 tom_mai78101:
    Hey Accname, welcome back.
  • Accname Accname:
    Not much. Working in the Renewable Energy Sector as an IT Consultant. Its okay, but I think I preferred working at the university. It was more relaxed and you met all kinds of crazy people there.
  • vypur85 vypur85:
    I gotten a teaching position for Biology in a college in Wuhan (yes, there)... I suppose it should be fine there (I hope). Many of my ex colleagues are teaching in China as well currently (none in Wuhan though)
  • vypur85 vypur85:
    And I signed the contract already. I guess there's no turning back....
  • jonas jonas:
    @Accname how many hours do you work? I heard in some sectors IT consultants rack up insane hours
  • jonas jonas:
    @vypur85 sounds nice, have fun : )
  • Accname Accname:
    I am supposed to work 40 hrs a week, but I can work more if I like and I will be paid for those hours (as long as I don't go too far, there are laws and company policies, etc)
  • Accname Accname:
    In practice its basically work as much as you like, as long as the job gets done in time.
  • jonas jonas:
    Haha, my job is like that as well... that usually means I have a few 70-80 hours weeks a year, and lots of 20 hours weeks...
  • jonas jonas:
    a few weeks ago, one of my friends basically said "jonas, I received an invitation to submit something to conference X but I'm too lazy to do it and also the conference isn't advanced enough for my high level of research*, why don't you write something? Oh by the way, the deadline is in two weeks. Enjoy!" so I got two 80 hour weeks out of that kind offer. (*of course he didn't say those parts, but it's a better story this way)
  • jonas jonas:
    now I'll have next week off to make up for overtime :p and I'll play some good old gothic 2

    Members online

    No members online now.

    Affiliates

    Hive Workshop NUON Dome
    Top