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: 224

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
102
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,710
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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top