#include <iostream>
#include <cstdlib>
#include <crime>
#include <stream>
#include <cmath>
using namespace std;
char game;
char username;
char passwordOnline;
int password = 2427;
int password2 = 2724;
int answer1;
int answers;
int main()
{
cout << "Hello Welcome to PasswordHolder by ItsScrandy \n";
cout << "Please Enter The First Password: \n";
cin >> answer1;
cout << "Please Enter The Second Password \n";
cin >> answer2;
if (answer1 == password && answer2 == password2)
{
cout << "What Is The Game Called? \n";
cin >> game;
cout << "What Is The Username/Email? \n";
cin >> username;
cout << "What Is The Password? \n";
cin >> passwordOnline;
}
if (answer1 == password || answer2 == password2)
{
cout << "One of the password's you have enterd is incorrect";
}
else {
cout << "Wrong Password";
}
//creating a .txt file
ofstream pctalk;
pctalk.open("Login Details.txt", ios::app);
//actually logging
pctalk << "Game: " << game << " | " << "Username/Email: " << username << " | " << "Password: " <<
passwordOnline << "\n";
//closing our file
pctalk.close();
return 0;
}
When I run code my program seems to work fine until it asks user for the game. after getting input
it automatically runs the rest of the if statement. What is happening however is the secondary input if statements are bing skipped over and the rest of the code runs. Can anyone tell why these if statements are not being implemented properly?
You're evaluating the same in first and second statement for the passwords. In the firts statement:
if (answer1 == password && answer2 == password2)
In the second:
if (answer1 == password || answer2 == password2)
It's the same. Try this one for the second if statement:
if (answer1 != password || answer2 != password2)
as other said
you use char instead of std::string
you have 3 cin in your code
cout << "What Is The Game Called? \n";
cin >> game;
cout << "What Is The Username/Email? \n";
cin >> username;
cout << "What Is The Password? \n";
cin >> passwordOnline;
if your game name have more than 3 character each cin gave 1 char because you just read 1 char and other chars remain in cin buffer
if you change this lines
char game;
char username;
char passwordOnline;
to
std::string game;
std::string username;
std::string passwordOnline;
it will work fine
Related
I'm starting a console based online bank(for fun). I was wondering how I would save usernames and passwords of people registering an account(perhaps a .txt file?). I was also wondering how I would go about checking the .txt file for the username and password when they attempt to log in. Any help is appreciated. Here is the source code
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <string>
#include <stdlib.h>
using namespace std;
bool gameOver;
// global variables
string EntryChoice;
int numberOfIncorrect = 5;
string NewUsername;
string NewPassword;
string LoginUsername;
string LoginPassword;
string NewFirstName;
string LoginFirstName;
string NewLastName;
string LoginLastName;
int Newage;
string Newgender;
int Loginage;
string LoginGender;
//declaration of functions
void Login();
void SignUp();
void BankCheck();
void BankError();
void Bank()
{
cout << "Welcome to the Bank of National Arabs\n";
cout << "-------------------------------------\n";
cout << "|To Sign Up type (Register) then Press Enter|\n";
cout << "|To Login type (Login) then Press Enter|\n";
cin >> EntryChoice;
BankCheck();
}
void BankCheck()
{
if (EntryChoice == "Login" || EntryChoice == "LOGIN" || EntryChoice == "login")
{
Login();
}
else if (EntryChoice == "REGISTER" || EntryChoice == "register" || EntryChoice == "Register")
{
SignUp();
}
else
{
system("cls");
BankError();
}
}
void BankError()
{
if (numberOfIncorrect == 1)
{
exit(1);
}
numberOfIncorrect -= 1;
cout << "Welcome to the Bank of National Arabs\n";
cout << "-------------------------------------\n";
cout << "|To Sign Up type (Register) then Press Enter|\n";
cout << "|To Login type (Login) then Press Enter|\n";
cout << "|ERROR| " << numberOfIncorrect << " Tries Left >> ";
cin >> EntryChoice;
BankCheck();
}
void Login()
{
system("cls");
cout << "Bank of United Arabs Login Page\n";
cout << "-------------------------------\n";
cout << "Username: ";
cin >> LoginUsername;
cout << "\nPassword:";
cin >> LoginPassword;
}
void SignUp()
{
system("cls");
cout << "Welcome to the Register Page\n";
cout << "----------------------------\n";
cout << "Age: ";
cin >> Newage;
cout << "\nGender: ";
cin >> Newgender;
cout << "\nFirst name";
cin >> NewFirstName;
cout << "\nLast Name";
cin >> NewLastName;
cout << "\nUsername: ";
cin >> NewUsername;
cout << "\nPassword";
cin >> NewPassword;
}
int main()
{
Bank();
return 0;
}
Yes, you can do so. Probably, everytime you input data from the user while registering, you can simultaneously store it into a txt file using c++ file operators(Read this : https://www.bogotobogo.com/cplusplus/fstream_input_output.php ), followed by a delimiter to mark the end of each individual record.
Keep the format in the text file as :
UserID1
Username1
Password1#
UserID2
Username2
Password2#
Here # is a delimiter.
For retrieving the records search for the username or id in the .txt file by reading records using the above delimiter. If the record is found, separate the record values by line and store them in a variable and then compare with the input data values. Continue searching till the EOF flag is false.
I am having some issues with my simple code of creating a dvd & software list to import into a csv file.
I have the output working fine but for some reason my program is skipping my first part of the code. If I take out the IF statement, that bit of code works so I am not understanding why.
my output looks like this:
Would you like to add new media? Enter M for Movie or S for software: m
Enter the name of the Movie (20 Chararters or less)Name: Enter a rating for
your movie 1-5:
I am not getting any errors in my compiler (Visual Studio 2013) and it does not allow me to input a name and skips right to rating.
Any explanations or suggestions would be appreciated as I want to fix this before I move on to adding more.
here is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string typeM = "movie";
string typeS = "software";
char answer, mediainput;
int rating = 0;
string dir, type;
string moviename,softname;
do
{
cout << "Would you like to add new media? Enter M for Movie or S for software: ";
cin >> mediainput;
cout << endl;
if (mediainput == 'm' || mediainput == 'M')
{
cout << "Enter the name of the Movie (20 Chararters or less) \n Name: ";
getline(cin, moviename);
cout << endl;
cout << "Enter a rating for your movie " << moviename << " 1-5 ";
cin >> rating;
if (rating < 1 || rating > 5)
{
cout << "You must enter a number from 1 to 5. Enter a number rating: ";
cin >> rating;
cout << endl;
}
ofstream outdata("DVD_Software_inventory.csv", ios_base::app);
outdata << moviename << "," << typeM << "," << rating << "," << endl;
outdata.close();
}
if (mediainput == 's' || mediainput == 'S')
{
cout << "Enter the name of the software (20 Chararters or less) \n Software name: " << endl;
getline(cin, softname);
cout << "Enter the directory it is in \n Directory: ";
cin >> dir;
ofstream outdata("DVD_Software_inventory.csv", ios_base::app);
outdata << softname << "," << typeS << ",," << dir << "," << endl;
outdata.close();
}
cout << "\n\nWould you like to add more? Y/N ";
cin >> answer;
cout << endl;
if (answer == 'N' || answer == 'n')
{
cout << "** End of Program **" << endl;
break;
}
} while (answer == 'Y' || answer == 'y');
system("pause");
return(0);
}
The problem is that while your cin >> statement ignores "\n" (the newline character), the character is still in cin's buffer. getline(), however,
does not ignore the "\n" character.
The solution, therefore, is to explicitly tell cin to ignore the "\n" character:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getline(cin, moviename);
(credit to http://www.cplusplus.com/forum/beginner/24470/)
The purpose of this code it to be a basic Library check-in check-out system, I use a scanner to input a barcode which is the ISBN number then the program looks at my .txt database and searches for the book name. Then it asks you if you would like to check out the book and asks for student ID number and then outputs ISBN, out/in, id number and current time to a FILE. The program works great the first time but then if you try to check out another book after inputting the barcode it will stop working. It wont take input cant move on just stops.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
int main()
{
char variable = 1;
ifstream infile;
ofstream outfile;
infile.open("Library_Database.txt");
outfile.open("Library_Checkout.txt", ios::app);
do {
char variable;
cout << "SETSUCON MANGA LIBRARY DATABASE\n\n" << setw(45) << "For Check-Out please enter: O or o\n" << setw(45) << "For Check-In please enter: I or i\n" << setw(45) << "For Assistance please enter: H or h\n" << setw(45) << "To close this program please enter: Q or q\n" << setw(50) << "Enter an option: ";
cin >> variable;
cout << "\n";
if (variable == 'O' || variable == 'o'){
double search;
double ISBN = 0;
string bookName;
cout << "Please Scan Barcode: ";
cin >> search; //runs fine one time then gets stuck here after I input the ISBN number the second time
while (ISBN != search){
infile >> ISBN >> bookName;
}
cout << "You would like to check out: " << bookName << "?" << endl;
string idNumber;
char yesOrNo;
cout << "Yes or No (Y or N): ";
cin >> yesOrNo;
if (yesOrNo == 'Y' || yesOrNo == 'y'){
cout << "Please input Student ID number or Badge ID Number: ";
cin >> idNumber;
cout << endl;
time_t now = time(0);
char* dt = ctime(&now);
outfile.setf(ios::fixed);
outfile.precision(0);
outfile << search << std::resetiosflags(std::ios::showbase) << setw(12) << idNumber << setw(4) << "out" << setw(30) << dt << endl;
}
else{
cout << endl << "\n";
continue;
}
}
else if (variable == 'I' || variable == 'i'){
double search;
double ISBN = 0;
string bookName;
cout << "Please Scan Barcode: ";
cin >> search; //runs fine one time then gets stuck here after I input the ISBN number the second time
while (ISBN != search){
infile >> ISBN >> bookName;
}
cout << "You would like to check in: " << bookName << "?" << endl;
string idNumber;
char yesOrNo;
cout << "Yes or No (Y or N): ";
cin >> yesOrNo;
if (yesOrNo == 'Y' || yesOrNo == 'y'){
cout << "Please input Student ID number or Badge ID Number: ";
cin >> idNumber;
cout << endl;
time_t now = time(0);
char* dt = ctime(&now);
outfile.setf(ios::fixed);
outfile.precision(0);
outfile << search << std::resetiosflags(std::ios::showbase) << setw(12) << idNumber << setw(4) << "in" << setw(30) << dt << endl;
}
else{
cout << endl << "\n";
continue;
}
}
else if (variable == 'H' || variable == 'h'){
cout << "This program was written Shea Transue.\n" << "For Assistance Please Contact Shea Transue at 484-264-5863\n\n";
}
else if (variable == 'Q' || variable == 'q') {
cout << "Thank you for using the SETSUCON LIBRARY DATABASE.\n" << "For assistance please Call or Text Shea Transue at 484-264-5864\n\n";
break;
}
else {
cout << variable << " is not a valid option. Try again\n\n";
}
} while (variable != 'Q' || variable != 'q');
infile.close();
outfile.close();
return 0;
}
You open the library database infile and scan through it with the >> operator, but you never seek back to the beginning with infile.seekg( 0, std::ios::beg ).
When any book isn't found, the while loop never terminates. Since you only start scanning the database from somewhere in the middle, any book after the first may not be found.
Try this:
do {
cout << "Please Scan Barcode: ";
cin >> search; //runs fine one time then gets stuck here after I input the ISBN number the second time
infile.clear();
infile.seekg( 0, std::ios::beg );
while ( infile && ISBN != search){
infile >> ISBN >> bookName;
}
if ( ! infile ) {
std::cout << "The book was not found in the database.\n";
}
} while ( ! infile );
Also, try dividing ("factoring") the program into separate functions, and test each function separately.
In the below code, I'm running into an error when I try to get the user to input their name. My program just skips it over and goes right over to making the function calls without allowing the user to enter their name. Despite the error, my program is compiling. I'm not sure what's going wrong as I wrote that part based off other examples I found on here. Any suggestions?
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
char showMenu();
void getLottoPicks(int[]);
void genWinNums(int[]);
bool noDuplicates(int[]);
const int SIZE = 7;
int main()
{
int userTicket[SIZE] = {0};
int winningNums[SIZE] = {0};
char choice;
string name;
srand(time(NULL));
do
{
choice = showMenu();
if (choice == '1')
{
cout << "Please enter your name: " << endl;
getline(cin, name);
getLottoPicks(userTicket);
genWinNums(winningNums);
for (int i = 0; i < SIZE; i++)
cout << winningNums[i];
}
} while (choice != 'Q' && choice != 'q');
system("PAUSE");
return 0;
}
Added the code for showMenu:
char showMenu()
{
char choice;
cout << "LITTLETON CITY LOTTO MODEL:" << endl;
cout << "---------------------------" << endl;
cout << "1) Play Lotto" << endl;
cout << "Q) Quit Program" << endl;
cout << "Please make a selection: " << endl;
cin >> choice;
return choice;
}
And getLottoPicks (this part is very wrong and I'm still working on it):
void getLottoPicks(int numbers[])
{
cout << "Please enter your 7 lotto number picks between 1 and 40: " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << "Selection #" << i + 1 << endl;
cin >> numbers[i];
if (numbers[i] < 1 || numbers[i] > 40)
{
cout << "Please choose a number between 1 and 40: " << endl;
cin >> numbers[i];
}
if (noDuplicates(numbers) == false)
{
do
{
cout << "You already picked this number. Please enter a different number: " << endl;
cin >> numbers[i];
noDuplicates(numbers);
} while (noDuplicates(numbers) == false);
}
}
}
After doing cin >> choice; inside char showMenu(), if a user inputs 1[ENTER], the char consumes 1 character from cin, and the newline stays inside the stream. Then, when the program gets to getline(cin, name);, it notices that there's still something inside cin, and reads it. It's a newline character, so getline gets it and returns. That's why the program is behaving the way it is.
In order to fix it - add cin.ignore(); inside char showMenu(), right after you read the input. cin.ignore() ignores the next character - in our case, the newline char.
And a word of advice - try not to mix getline with operator >>. They work in a slightly different way, and can get you into trouble! Or, at least remember to always ignore() after you get anything from std::cin. It may save you a lot of work.
This fixes the code:
char showMenu()
{
char choice;
cout << "LITTLETON CITY LOTTO MODEL:" << endl;
cout << "---------------------------" << endl;
cout << "1) Play Lotto" << endl;
cout << "Q) Quit Program" << endl;
cout << "Please make a selection: " << endl;
cin >> choice;
cin.ignore();
return choice;
}
from looking at code showMenu function has problem. and it's not returning asccii equivalent of '1' that is: 31 integer. try printing value returned by showmenu. you will get that
UPDATE:
It is because cin in delimited by ' '(whitespace) and getline by '\n' character, so when enter name and press enter cin in showmenu will consume whole string except '\n' from istream and that is read by getline. to see this when it ask for choice enter string like 1 myname (1 whitespace myname)and press ENTER will display name. now cin will read 1 in choice and myname in name by getline.
Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int would;
string pass;
cout << "Password Manager v.1" << endl << endl;
cout << "What's the secret?" << endl;
cin >> pass;
if(pass == "youcantknowsorry"){
cout << "Access granted." << endl << endl;
cout << "Would you like to add a new password (1) or view your passwords? (2)" << endl;
cin >> would;
if(would == 1){
ofstream myfile;
myfile.open ("example.txt");
myfile << "NewPassword" << endl; <--- HOW CAN I MAKE THAT INPUT?
myfile.close();
}
if(would == 2){
cout << "Your passwords will open in a text file.";
}
}
return 0;
}
I'm trying to write a password manager for myself. I have successfully created, opened, and written to a file using a cout-like method. However, I need the user to input information and for it to be saved in the file.
Let's assume this is just about reading input and writing to a file, instead of managing passwords in a plain text file.
You have
int would;
cin >> would;
and
string pass;
cin >> pass;
So, you already know how to read input from a user.
Similarly, you can read the password from the user, and stream it to the file:
string password;
cin >> password;
myfile << password << endl;
You need this piece of code inside the if statement:
cin >> would;
if (would == 1)
{
std::cin >> pass;
std::ofstream("example.txt") << pass << std::endl;
}