...

View Full Version : One of my first scripts (sorta) Login/Calculator, C++



twodayslate
08-29-2007, 10:50 PM
//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.

oracleguy
08-29-2007, 11:05 PM
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.

twodayslate
08-29-2007, 11:06 PM
Ok, will fix that.

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

oracleguy
08-29-2007, 11:58 PM
That still doesn't completely fix the problem. It should just be:


else
{
cout << "Username and//or password are incorect, please try again.";
}

twodayslate
08-30-2007, 02:08 AM
whoops!
{
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?

sage45
08-30-2007, 06:57 AM
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...
//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.

twodayslate
09-01-2007, 07:33 PM
Is there a site like http://php.net/ for C++?

So bool (http://www.functionx.com/cpp/keywords/bool.htm) is either true or false.

-= I assume is subtract one to value.

I do not get how it "loops" back to the login.

//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;
}

sage45
09-02-2007, 12:09 AM
Is there a site like http://php.net/ for C++?Try http://www.cplusplus.com/
So bool (http://www.functionx.com/cpp/keywords/bool.htm) is either true or false.You got it...
-= I assume is subtract one to value.Using -= is the same as saying variableA = variableA - 1
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.
{
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-

twodayslate
09-02-2007, 05:44 AM
//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!

sage45
09-02-2007, 05:48 PM
It works fine for me...

As for your next question, two methods that are often used are:
#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-
#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.
// 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-

twodayslate
09-04-2007, 11:06 PM
Have I not grasped the loop concept?
http://img213.imageshack.us/img213/4193/deleteeu9.gif

Thanks a bunch!

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum