Can't remember an easy if else statement

Okay guys, so I had to step away from my computer for a while and haven't been coding much, once I got back I decided to start learning Python and now I can't remember anything in C++. I should know how to do this but I just can't remember.. Anyways, I'm just trying to make a small console program that compares the user input to a string but like I said, I can't remember how this would be done.
This is what I'm stuck with now.
I think learning Python has tainted my brain because I'm trying to use Python syntax, and could have this easily written in Python, but I want to come back to C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "stdafx.h"
#include <iostream>


using namespace std;



int main()
{
	string password = ("C++PROGRAM");

	cout << "Please enter your password" << endl;
	//i want to get user input here
	if {
		USERINPUT == password;
		cout << "Great! You now have full access!" << endl;
	}
	else {
		cout << "Hmm.. That isn't correct!" << endl;
		//if else loop back to cout << "Please enter your password"
	}
	
	



    return 0;
}

closed account (48T7M4Gy)
TheIdeasMan is right, you should go through the tutorial, rather than poking around in the dark and going up a steep incline the hard way.

This answers your immediate question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

int main()
{
    std::string USERINPUT = "*";
    std::string password = "C++PROGRAM";

	std::cout << "Please enter your password: ";
	while( std::cin >> USERINPUT && USERINPUT != password) 
	{
	    std::cout << "Hmm.. That isn't correct!\n";
	}
	
	std::cout << "Great! You now have full access!\n";
	
    return 0;
}
closed account (48T7M4Gy)
LOL. Why did the word 'relentless' just spring into my mind?
closed account (48bpfSEw)
If you have written a lot of code with python and want to use them in your C++ sources. Here is a way to mix both languages together:
https://docs.python.org/2/extending/embedding.html

Thanks a lot guys. I appreciate your help.

But this code is now faulty, I really need to just start over with C++ so I can understand what is wrong, because I don't understand why this is wrong..
Try this code yourself. I have included _getch() so you can see what is happening in the console before continuing.. If you insert the INCORRECT string - i.e. not C++PROGRAM, it just continues to the next line it doesn't loop back asking you to enter your password again.. But if you do insert the correct string ( password ) it will do the code inside of the while, meaning it will display "Hmm.. That isn't correct!".. Then on a new line it will also display, "Great! You now have full access".. Why is it doing this? I don't understand. Thank you for your time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>


using namespace std;



int main()
{
	std::string password = "C++PROGRAM";
	std::string USERINPUT;
		
		std::cout << "Please enter your password" << endl;
		while ( std::cin >> USERINPUT && USERINPUT != password);
		{
			cout << "Hmm.. That isn't correct!\n";
		}
	
		cout << "Great! You now have full access..";



		_getch();
    return 0;
}
Last edited on
Line 17: The ; terminates the while loop. Lines 18-20 are going to execute once unconditionally. Get rid of the ;
Topic archived. No new replies allowed.