Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Senior Coder twodayslate's Avatar
    Join Date
    Mar 2007
    Location
    VA
    Posts
    1,042
    Thanks
    67
    Thanked 39 Times in 39 Posts

    One of my first scripts (sorta) Login/Calculator, C++

    Code:
    //My program gallery, login script, calculator and more!
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdio>
    #include <ctime>
    #include <cmath>
    using namespace std;
    
    int main()
    	{
    		cout << "Username: ";
    		string username;
    		cin >> username;
    		cout << "Password: ";
    		string password;
    		cin >> password;
    		
    			string correctusername ="Bob";
    			string correctpassword ="123";
    				
    
    		if (username==correctusername && password==correctpassword) 
    		  {
    		  	cout << "Welcome, " << username << "! What would you like to do? Please type in \none of the following comands.\n";
    			cout << "\-Calculator\n";
    			cout << "-Exit\n";	
    			string comand;
    			cin >> comand;
    			
    			string calculator = "calculator"; //can be caps or not
    			if (comand==calculator)
    				{
    				int calculatorprograminput;
    				cin >> calculatorprograminput;
    				double calculatorprogramfinal = calculatorprograminput;
    				cout << "= " << calculatorprogramfinal*1.0 << "\n Having problems, just displays the first digit, does not do the math.";	
    				}
    
    			string exit = "exit";
    			if (comand==exit)
    				{
    				cout << "Goodbye!";	
    				}
    		  }	
    		else 
    			{ 
    				if (username!=correctusername && password==correctpassword)
    					{ cout<<"Username not in database, please try again."; }
    
    				else {  
    					if (username==correctusername && password!=correctpassword)
    					{ cout << "Password does not match username, please try again.";}
    					else { cout << "Username and password are incorect, please try again."; }
    				}
    			}
    		
    		
    		
    	return 0; 
    	}
    Just started last week. I went ahead of my class and started with if/elses so I do not know much, thanks for the help.
    Basically the numbers do not add up for the calculator and I want to know what you guys think of the login script.
    twitter | Quality Hosting - $5.95/mo*
    Feel free to PM me!

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    It is bad security to tell the user that they got the username wrong but a password right. Because then they know they have a valid password and they can just go through all the users which is usually a much shorter list than possible passwords.

    It is better practice to just say "Invalid username or password" if they either isn't valid.
    OracleGuy

  • Users who have thanked oracleguy for this post:

    twodayslate (08-29-2007)

  • #3
    Senior Coder twodayslate's Avatar
    Join Date
    Mar 2007
    Location
    VA
    Posts
    1,042
    Thanks
    67
    Thanked 39 Times in 39 Posts
    Ok, will fix that.
    Code:
    else 
    			{ 
    				if (username!=correctusername && password==correctpassword)
    					{ cout<<"Username not in database, please try again."; }
    
    				else {  
    					cout << "Username and//or password are incorect, please try again."; }
    				
    			}
    Fixed
    Last edited by twodayslate; 08-29-2007 at 11:10 PM.
    twitter | Quality Hosting - $5.95/mo*
    Feel free to PM me!

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    That still doesn't completely fix the problem. It should just be:
    Code:
    else 
    { 
    	cout << "Username and//or password are incorect, please try again.";
    }
    OracleGuy

  • #5
    Senior Coder twodayslate's Avatar
    Join Date
    Mar 2007
    Location
    VA
    Posts
    1,042
    Thanks
    67
    Thanked 39 Times in 39 Posts
    whoops!
    Code:
    { 
    					int passwordcount = 3;
    					int wrongsusernamepasswordcount = 5;
    				if (username==correctusername && password!=correctpassword)
    					{ cout<<"Password is incorrect, " << passwordcount << " attempts left."; }
    
    				else {  
    					cout << "Username and/or password are incorect, "<< wrongsusernamepasswordcount <<" attempts left."; }
    				
    			}
    There, this should not be so insecure. It just tells the user when they have the wrong password.

    How do you get it so when you do not type in the right password/username it lets you retry without having to type in the program name through the command prompt.
    Then how do you make the count go down?
    Last edited by twodayslate; 08-30-2007 at 02:16 AM.
    twitter | Quality Hosting - $5.95/mo*
    Feel free to PM me!

  • #6
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Quote Originally Posted by twodayslate View Post
    whoops!How do you get it so when you do not type in the right password/username it lets you retry without having to type in the program name through the command prompt.
    Then how do you make the count go down?
    Use a loop...
    Code:
    //My program gallery, login script, calculator and more!
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdio>
    #include <ctime>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	//Sets the password counter for the while loop
    	int passwordCount = 3;
    	// Sets the username counter for the while loop
    	int usernameCount = 5;
    	// Sets a loop status so that even if the password counter is true and
    	// the username counter is true then this will be false causing the program
    	// to gracefully fall out of the loop.
    	bool loopStatus = true;
    	// While loop with the condition tested at the beginning.  If any of the conditions
    	// are false the program will fall out of the loop and end.
    	while (passwordCount != 0 && usernameCount != 0 && loopStatus == true)
    	{
    		cout << "Username: ";
    		string username;
    		cin >> username;
    		cout << "Password: ";
    		string password;
    		cin >> password;
    
    		string correctusername ="Bob";
    		string correctpassword ="123";
    
    		if (username==correctusername && password==correctpassword)
    		{
    			// If the username and password are correct set the loop status to false.
    			loopStatus = false;
    			cout << "Welcome, " << username << "! What would you like to do? Please type in \none of the following comands.\n";
    			cout << "\-Calculator\n";
    			cout << "\-Exit\n";
    			string comand;
    			cin >> comand;
    
    			string calculator = "calculator"; //can be caps or not
    			if (comand==calculator)
    			{
    				int calculatorprograminput;
    				cin >> calculatorprograminput;
    				double calculatorprogramfinal = calculatorprograminput;
    				cout << "= " << calculatorprogramfinal*1.0 << "\n Having problems, just displays the first digit, does not do the math.";
    			}
    
    			string exit = "exit";
    
    			if (comand==exit)
    			{
    				cout << "Goodbye!";	
    			}
    		}
    		else 
    		{
    			if (username==correctusername && password!=correctpassword)
    			{
    				// if the password is not correct subtract one from the password counter
    				passwordCount -= 1;
    				cout<<"Password is incorrect, " << passwordCount << " attempts left.";
    			}
    			else
    			{  
    				// if the username is not correct subtract one from the username counter
    				usernameCount -= 1;
    				cout << "Username and/or password are incorect, "<< usernameCount <<" attempts left.";
    			}
    		}		
    	}
    return 0; 
    }
    HTH,

    -saige-

    P.S. --> I would recommend that you give the same number of tries for both the username and password. It is more secure and then you can loose a condition as well as an if...then statement. But that's just me.
    Last edited by sage45; 08-30-2007 at 07:06 AM.
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • Users who have thanked sage45 for this post:

    twodayslate (08-31-2007)

  • #7
    Senior Coder twodayslate's Avatar
    Join Date
    Mar 2007
    Location
    VA
    Posts
    1,042
    Thanks
    67
    Thanked 39 Times in 39 Posts
    Is there a site like http://php.net/ for C++?

    So bool is either true or false.

    -= I assume is subtract one to value.

    I do not get how it "loops" back to the login.
    Code:
    //My program gallery, login script, calculator and more!
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdio>
    #include <ctime>
    #include <cmath>
    using namespace std;
    
    int main()
    	{
    	int wrongpasswordCount = 3;
    	int wrongsusernamepasswordCount = 5;
    	bool loopStatus = true;	
    	while (wrongpasswordCount != 0 && wrongsusernamepasswordCount != 0 && loopStatus == true)
    	{
    		cout << "Username: ";
    		string username;
    		cin >> username;
    		cout << "Password: ";
    		string password;
    		cin >> password;
    		
    			string correctusername ="twodays";
    			string correctpassword ="123";
    				
    
    		if (username==correctusername && password==correctpassword) 
    		  {	
    			loopStatus = false;
    		  	cout << "Welcome, " << username << "! What would you like to do? Please type in \none of the following comands.\n";
    			cout << "\-Calculator\n";
    			cout << "-Exit\n";	
    			string comand;
    			cin >> comand;
    			
    			string calculator = "calculator"; //can be caps or not
    			if (comand==calculator)
    				{
    				int calculatorprograminput;
    				cin >> calculatorprograminput;
    				double calculatorprogramfinal = calculatorprograminput;
    				cout << "= " << calculatorprogramfinal*1.0 << "\n Having problems, just displays the first digit, does not do the math.";	
    				}
    
    			string exit = "exit";
    			if (comand==exit)
    				{
    				cout << "Goodbye!";	
    				}
    		  }	
    		else 
    			{ 
    				
    				if (username==correctusername && password!=correctpassword)
    					{ wrongpasswordCount -= 1;
    					cout<<"Password is incorrect, " << wrongpasswordCount << " attempts left.\n"; }
    
    				else {  wrongsusernamepasswordCount -= 1;
    					cout << "Username and/or password are incorect, "<< wrongsusernamepasswordCount <<" attempts left.\n"; }
    				
    			}
    		
    		
    	}	
    	return 0; 
    	}
    Last edited by twodayslate; 09-01-2007 at 07:36 PM.
    twitter | Quality Hosting - $5.95/mo*
    Feel free to PM me!

  • #8
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Quote Originally Posted by twodayslate View Post
    Is there a site like http://php.net/ for C++?
    Try http://www.cplusplus.com/
    Quote Originally Posted by twodayslate View Post
    So bool is either true or false.
    You got it...
    Quote Originally Posted by twodayslate View Post
    -= I assume is subtract one to value.
    Using -= is the same as saying variableA = variableA - 1
    Quote Originally Posted by twodayslate View Post
    I do not get how it "loops" back to the login.
    Follow the logic [Refer to the comments in bold.] You can also use F10 in your debugger to step through the code if you are using Visual Studio.
    Code:
    {
    	int wrongpasswordCount = 3;
    	int wrongsusernamepasswordCount = 5;
    	bool loopStatus = true;
    	// While this statement remains true, the program loops.
    	// When wrongpasswordCount = 0 OR wrongusernamepasswordCount = 0 OR
    	// loopStatus = false, the condition is false and causes the loop to exit
    	while (wrongpasswordCount != 0 && wrongsusernamepasswordCount != 0 && loopStatus == true)
    	{
    		cout << "Username: ";
    		string username;
    		cin >> username;
    		cout << "Password: ";
    		string password;
    		cin >> password;
    		
    		string correctusername ="twodays";
    		string correctpassword ="123";
    				
    		// If username is correct AND password is correct,
    		// the condition is true.
    		// If username is not correct OR password is not correct
    		// the condition is false.
    		if (username==correctusername && password==correctpassword) 
    		{	
    			// If the condition is true set loopStatus to false
    			loopStatus = false;
    		}
    		// If the condition is false go here
    		else 
    		{ 
    			// Determine which was incorrect
    			if (username==correctusername && password!=correctpassword)
    			{
    				// If the password was wrong subtract 1 from the
    				// wrongpasswordCount variable.
    				// When the wrongpasswordCount variable reaches
    				// zero the loop will exit. 
    				wrongpasswordCount -= 1;
    			}
    			// If the username was wrong go here
    			else
    			{
    				// If the username was wrong subtract 1 from the
    				// wrongusernamepasswordCount variable.
    				// When the wrongusernamepasswordCount variable
    				// reaches zero the loop will exit. 
    				wrongsusernamepasswordCount -= 1;
    			}
    		}
    	// End of While loop, if the loop condition was true,
    	// return to the beginning of the loop and check the
    	// condition again.
    	}	
    	return 0; 
    }
    I removed any additional code that did not have anything to do with the logic.

    HTH,

    -saige-
    Last edited by sage45; 09-02-2007 at 12:13 AM.
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • Users who have thanked sage45 for this post:

    twodayslate (09-02-2007)

  • #9
    Senior Coder twodayslate's Avatar
    Join Date
    Mar 2007
    Location
    VA
    Posts
    1,042
    Thanks
    67
    Thanked 39 Times in 39 Posts
    Code:
    //My program gallery, login script, calculator and more!
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdio>
    #include <ctime>
    #include <cmath>
    using namespace std;
    
    int main()
    	{
    	  // User has three trys to login with the correct password until they are shut off
    	int wrongpasswordCount = 3;
              // User has five trys to login with a correct username and password until they are shut off
    	int wrongsusernamepasswordCount = 5;
    	bool loopStatus = true;	
    	while (wrongpasswordCount != 0 && wrongsusernamepasswordCount != 0 && loopStatus == true)
    	{
    		cout << "Username: ";
    		string username;
    		cin >> username;
    		cout << "Password: ";
    		string password;
    		cin >> password;
    		
    			string correctusername ="Zac";
    			string correctpassword ="123";
    				
    
    		if (username==correctusername && password==correctpassword) 
    		  {	
    			loopStatus = false;
    		  	cout << "\nWelcome, " << username << "! ";
    
    			bool loopCommand  = true;
    			while (loopCommand  == true)
    			{
    				cout << "\nWhat would you like to do? Please type in one of the following command .\n";
    				cout << "\-Calculator\n";
    				cout << "-Exit\n";	
    				string command;
    				getline(cin, command);
    			
    				string calculator = "calculator"; //can be caps or not
    				if (command==calculator)
    					{
    					int calculatorprograminput;
    					cin >> calculatorprograminput;
    					double calculatorprogramfinal = calculatorprograminput;
    					cout << "= " << calculatorprogramfinal*1.0 << "\n Having problems, just displays the first digit, does not do the math.";	
    					}
    			
    				string exit = "exit";
    				if (command==exit)
    					{
    					loopCommand = false;
    					cout << "Goodbye!";	
    					}
    			}
    		  }	
    		else 
    			{ 
    				
    				if (username==correctusername && password!=correctpassword)
    					{ wrongpasswordCount -= 1;
    					cout<<"Password is incorrect, " << wrongpasswordCount << " attempts left.\n"; }
    
    				else {  wrongsusernamepasswordCount -= 1;
    					cout << "Username and/or password are incorect, "<< wrongsusernamepasswordCount <<" attempts left.\n"; }
    				
    			}
    		
    		
    	}	
    	return 0; 
    	}
    OK, I think I get the loop thing. However, when I tried it it displays the command twice.

    Also how do you get the command feature to accept capital or lower case letters. (EX: calculator or CALculatOr)

    Thank you so much for the help, you are explaining things so well!
    Last edited by twodayslate; 09-02-2007 at 05:47 AM.
    twitter | Quality Hosting - $5.95/mo*
    Feel free to PM me!

  • #10
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    It works fine for me...

    As for your next question, two methods that are often used are:
    Code:
    #include <cctype>
    #include <string>
    using namespace std;
    int main()
    {
    	string s="hello";
    	for (int j=0; j<s.length(); ++j)
    	{
     		s[j]=toupper(s[j]);
    	} // s now contains "HELLO"
    }
    -and-
    Code:
    #include <cctype>
    #include <algorithm>
    #include <string>
    using namespace std;
    int main()
    {
    	string s="hello";
    	transform(s.begin(), s.end(), s.begin(), (int(*)(int)) toupper);
    }
    You can also specify tolower, just replace toupper with tolower. One gotcha is making sure that your conditional string (the string you set as the truth part of the condition) is already in upper or lower case.
    Code:
    	// This is the truth part of the condition
    	// This is what must be found in order for the condition to be true.
    	string truth = "I AM TRUE";
    	If (someVariable == truth)
    	{
    		//Do some stuff
    	}
    HTH,

    -saige-
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #11
    Senior Coder twodayslate's Avatar
    Join Date
    Mar 2007
    Location
    VA
    Posts
    1,042
    Thanks
    67
    Thanked 39 Times in 39 Posts
    Have I not grasped the loop concept?
    http://img213.imageshack.us/img213/4193/deleteeu9.gif

    Thanks a bunch!
    Code:
    bool loopCommand  = true;
    			while (loopCommand  == true)
    			{
    				cout << "\nWhat would you like to do? Please type in one of the following commands.\n";
    				cout << "\-Calculator\n";
    				cout << "-Exit\n";	
    				string command;
    				getline(cin, command);
    					transform(command.begin(), command.end(), command.begin(), (int(*)(int)) toupper);
    			
    				string CALCULATOR = "CALCULATOR"; //can be caps or not
    				if (command==CALCULATOR)
    					{
    					int calculatorprograminput;
    					cin >> calculatorprograminput;
    					double calculatorprogramfinal = calculatorprograminput;
    					cout << "= " << calculatorprogramfinal*1.0;	
    					}
    				command = "";
    
    				string EXIT = "EXIT";
    				if (command==EXIT)
    					{
    					loopCommand = false;
    					cout << "Goodbye!";	
    					}
    			} //end bool loopCommand while
    twitter | Quality Hosting - $5.95/mo*
    Feel free to PM me!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •