4 Replies - 216 Views - Last Post: 17 August 2019 - 03:20 PM Rate Topic: -----

#1 SC0TTii   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-August 19

Need Help With Code (BEGINNER)

Posted 17 August 2019 - 02:44 PM

So I am a beginner at C++ and just started a few days ago. My issue here is that everything works up until I want to "login," where even though I have the email and username logged, it doesn't work for some reason. There are no errors, it just seems to be a logical error i'm missing. All help is appreciated!

#include <iostream> //input output stream
#include <iomanip> //input output manipulation
#include <limits> //limits for data types
#include <string> // //std::string, std::stoi
using namespace std; //using standard namespace

int main() {

	//global variables
	string uEmail = "";
	string uUsername = "";
	string uPassword = "";
	bool uContinue = true;

	while (uContinue) {

		cout << "1. Enter Email\n" << flush;
		cout << "2. Enter Username\n" << flush;
		cout << "3. Enter Password\n" << flush;
		cout << "4. Display Information\n" << flush;
		cout << "5. Login\n" << flush;
		cout << "" << endl;
		cout << "6. QUIT APPLICATION\n" << flush;

		int uInput = 0;
		cin >> uInput;

		while (uInput < 1 || uInput > 6) {

			cout << "" << endl;
			cout << "Error: Please try again >> " << flush;
			cin >> uInput;
			cout << "" << endl;
			cout << "" << endl;
		}

		if (uInput == 1) {

			cout << "" << endl;
			cout << "Please enter preferred email address: " << flush;
			cin >> uEmail;
			cout << "" << endl;
			cout << "Your email has been set to: " << uEmail << "." << endl;
			cout << "" << endl;

		}
		else if (uInput == 2) {

			cout << "" << endl;
			cout << "Please enter preferred username: " << flush;
			cin >> uUsername;
			cout << "" << endl;
			cout << "Your username has been set to: " << uUsername << "." << endl;
			cout << "" << endl;

		}
		else if (uInput == 3) {

			cout << "" << endl;
			cout << "Please enter preferred password: " << flush;
			cin >> uPassword;
			cout << "" << endl;
			cout << "Your password has been set to: " << uPassword << "." << endl;
			cout << "" << endl;

		}
		else if (uInput == 4) {

			cout << "" << endl;
			cout << "Email: " << uEmail << endl;
			cout << "Username: " << uUsername << endl;
			cout << "Password: " << uPassword << endl;
			cout << "" << endl;

		}
		else if (uInput == 5) {

			//breaks out of loop
			uContinue = false;

			cout << "" << endl;
			cout << "Please enter email or username: " << flush;

			string newInput = "";
			cin >> newInput;

			while (newInput != uEmail || newInput != uUsername) {

				cout << "" << endl;
				cout << "INCORRECT. PLEASE TRY AGAIN >> " << flush;
				cin >> newInput;
				cout << "'Email: " << uEmail << ", 'Username: " << uUsername << "'." << endl;
				cout << "" << endl;

			}

			if (newInput == uEmail) {

				cout << "EMAIL: " << uEmail << endl;

			}
			else
			{
				cout << "USERNAME: " << uUsername << endl;
			}

		}
		else if (uInput == 6) {

			cout << "" << endl;
			cout << "Closing application . . .";
			cout << "" << endl;
			return -1;

		}

		

	}

	return 0;

}


Is This A Good Question/Topic? 0
  • +

Replies To: Need Help With Code (BEGINNER)

#2 jimblumberg   User is offline

  • member icon

Reputation: 5745
  • View blog
  • Posts: 17,601
  • Joined: 25-December 09

Re: Need Help With Code (BEGINNER)

Posted 17 August 2019 - 03:05 PM

Look closely at this snippet: while (newInput != uEmail || newInput != uUsername) {

Be careful when using negative logic, if newInput is equal to either the email or user name then this statement will execute.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7020
  • View blog
  • Posts: 23,840
  • Joined: 05-May 12

Re: Need Help With Code (BEGINNER)

Posted 17 August 2019 - 03:06 PM

Look at you condition:
newInput != uEmail || newInput != uUsername


It will always be true.

Let's say uEmail == "email" and uUsername == "name". Let's try the variations.

The user enters "email":
"email" != "email" || "email" != "name"
       false       ||        true
                  true



The user enters "name":
"name" != "email" || "name" != "name"
      true        ||       false
                 true



The user enters "joker":
"joker" != "email" || "joker" != "name"
       true        ||        true
                  true


Was This Post Helpful? 1
  • +
  • -

#4 SC0TTii   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-August 19

Re: Need Help With Code (BEGINNER)

Posted 17 August 2019 - 03:12 PM

Oh I see, so would changing it to '&&' fix it logically? Since it would short circuit if a condition were false? Forgive me if I am wrong. :bored:
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7020
  • View blog
  • Posts: 23,840
  • Joined: 05-May 12

Re: Need Help With Code (BEGINNER)

Posted 17 August 2019 - 03:20 PM

Give it a try and see what happens. We live in the modern world nowadays where you can have a compiler on your own computer unlike many years ago when you would have to type up your program, have the program converted to punch cards, then have the punch cards run overnight on the mainframe, and you pick up the results the next day.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1