Inputing to text file - c++

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

Related

read /write file in c++, choose option

I'm trying to write a code for signup / login while being able to write and read from the file. So far I am able to write in the file and asked for the users input and displayed in the file (signup).
My problem is now,
How do I do the login part, in which when the user chooses login, they are able to choose what username they want based on a selection of usernames and the input they have made while choosing the first option?
How can this information be read and displayed in the file?
Expected output for user login
Choose you username:
0:mike
1:Linda
2:Martha
Expected input from the user
your choice: "Key in choice"
So far the code displayed works, but I am not sure what to do for my next step.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
int option;
int age,height;
string name;
string database;
ofstream file_out;
ifstream file_in;
cout << " For sign up type 1" <<endl;
cout << " For log in type 2" <<endl;
cin >> option;
if ( option == 1 ) {
file_out.open("database.txt");
cout << "Input name: \n";
cin >> name;
cout << "Input age: \n";
cin >> age;
cout << " Input height: \n";
cin >> height;
//write file
file_out << name << endl;
file_out << age << endl;
file_out << height << endl;
file_out.close();
} else if (option == 2) {
//read file
file_in.open("database.txt");
cout << "choose your username: " << endl;
// input line of code
while ( getline(file_in, database));
//input line of code
cout << database << endl;
You can do something like this. Print all the usernames, but before printing out check if they are numbers with help of the stof function, if they are numbers we don't have to do anything, but if it is a string it will throw an exception, so in the catch block we print it and store it into dictionary/map for easy retrieval.
#include <fstream>
#include <iostream>
#include <map>
int main(void) {
std::fstream file;
file.open("secret.txt", std::ios::in);
std::map<int, std::string> map;
std::string username;
int count = 1;
while (!file.eof()) {
file >> username;
try {
std::stof(username);
} catch (...) {
std::cout << count << ". " << username << "\n";
map.insert(std::make_pair(count, username));
count++;
}
}
int choice;
std::cout << "Choose your username: ";
std::cin >> choice;
username = map.at(choice);
std::cout << "Your username is set to " << username;
return 0;
}

C++ Menu Ordering System won't accept multiple words when inputting to the txt file

My code below, whenever I input the menu items, will automatically end the program if I add more than one word such as "Tequila Sunrise" rather than "Tequila". How do I fix this to make it so I can add items with more than one word in the name?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// Restaurant Side Title
cout << "Welcome to the Menu Ordering System. \n\n\n";
// Create a txt file to store the menu
fstream MenuWriteFile("drinksmenu.txt");
fstream MenuPricesFile ("drinksprices.txt");
// Write the menu to the file
string myDrinks;
double drinksPrices;
int i;
cout << "Please enter your first drink: ";
cin >> myDrinks;
MenuWriteFile << myDrinks << endl;
cout << "Price: ";
cin >> drinksPrices;
MenuPricesFile << drinksPrices << endl;
cout << "To add another drink type 1, if not type 0: ";
cin >> i;
while (i == 1) {
cout << "Please enter your next drink: ";
cin >> myDrinks;
MenuWriteFile << myDrinks << endl;
cout << "Price: ";
cin >> drinksPrices;
MenuPricesFile << drinksPrices << endl;
cout << "To add another drink type 1, if not type 0: ";
cin >> i;
}
MenuWriteFile.close();
MenuPricesFile.close();
// Create a text string, which is used to output the text file
string myText;
// Read from the text file
ifstream MenuReadFile("drinksmenu.txt");
while (getline (MenuReadFile, myText)) {
cout << "\n\n" << myText << "\n";
}
// Close the file
MenuReadFile.close();
return 0;
}
Use std::getline():
while (i == 1) {
cout << "Please enter your next drink: ";
std::getline(std::cin >> std::ws, myDrinks);
....

If statement does not complete code and skips getting user input

#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

How do I save passwords and usernames to a .txt file in c++. I still want to be able to retrieve them if the user tries to login

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.

How to read text from a dat file without it showing hexadecimals? C++

I'm writing a program where i ask a user to input a username and password and then its stored in a dat file. Then its supposed to output the username and password but it just give me hexadecimals such as 0x9ffd18. here the code is
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream myfile;
myfile.open ("password.dat");
string username;
string password;
cout << "This is being saved to a file" << endl;
cout << "Please enter your username" << endl;
getline (cin, username);
myfile << username;
cout << "Please enter your password" << endl;
getline (cin, password);
myfile << password;
cout << myfile << endl;
}
cout << myfile << endl;
doesn't output what you have been writing to your file, but a void* interpretation of myfile's address, which is printed as hex value by default.
You need to close your file (or seekg() to the starting position), open it again, and read the values as they were written.
My suggestion to solve the problem:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream myfile;
myfile.open ("password.dat", ios::in | ios::out | ios::trunc);
string username;
string password;
cout << "This is being saved to a file" << endl;
cout << "Please enter your username" << endl;
getline (cin, username);
myfile << username;
cout << "Please enter your password" << endl;
getline (cin, password);
myfile << password;
char data[100];
myfile.seekg(0,ios::beg);
myfile >> data;
cout << "Output: " << endl;
cout << data << endl;
myfile.close();
}
You have a couple of problems here. One is the simple act of rewinding the file and reading back the contents. Another (probably more important, at least in the long term) is to write the file in a way that it can be read back unambiguously.
The problem right now is that you write the user name and password with nothing to define where one ends and the other starts. The most obvious candidate would be a new-line. You read both in using getline, which stops reading at a new-line, so we know neither can contain a new-line.
Other than that, I'd #include <string> since you're using std::string, and get rid of the using namespace std; and the endls (almost always mistakes). The result might look something like this:
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::fstream myfile("password.dat", std::ios::in | std::ios::out | std::ios::trunc);
std::string username;
std::string password;
std::cout << "Please enter your username: ";
std::getline(std::cin, username);
myfile << username << '\n';
std::cout << "\nPlease enter your password: ";
std::getline(std::cin, password);
myfile << password;
myfile.seekg(0);
std::string read_back_user_name;
std::string read_back_password;
std::getline(myfile, read_back_user_name);
std::getline(myfile, read_back_password);
std::cout << "The user name is: " << read_back_user_name << "\n";
std::cout << "The password is: " << read_back_password << "\n";
}
If you really want to just copy the entire content of the file to cout, you can do something like this:
myfile.seekg(0);
cout << myfile.rdbuf();
You print not the data you inputed to file, but the std::fstream object converted to address. To fix that, use method rdbuf().
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream myfile;
myfile.open ("password.dat"); //open for input
string username;
string password;
cout << "This is being saved to a file" << endl;
getline (cin, username);
myfile << username; //input first string
cout << "Please enter your password" << endl;
getline (cin, password);
myfile << password; //input second string
myfile.close(); //close so data is sent from buffer
myfile.open("password.dat"); //open for output
cout << "Your file: " << myfile.rdbuf() << endl; //print all characterf from file
myfile.close();
}