Help with file io decision making
Dec 7, 2016 at 2:57am UTC
Hello there! I am working on a C++ program that is like a faux bank account. I have the program set up to be able to get a username, search a file for the username, find the associated "balance" with that name, and change it from there. I found the best way to change a text file is to just write a new file with the changed information inside of that. Here is the code I have so far, but it isn't giving the results I want. Keep in mind this is in a very early stage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#include <iostream>
#include <fstream>
using namespace std;
//function prototypes
void addUser();
void part1();
int main();
//global variables
string username;
string password;
int balance;
void addUser(){
//open a file in write mode
ofstream myfile;
myfile.open("userinfo.txt" , ios::app);//append the text file
if (myfile.is_open()){
cout<<"Please Create a Username: " ;
string inputCheck;//a string to compare name
cin>>inputCheck;
cout<<"Please Create a Password: " ;
cin>>password;
cout<<"Current Balance: " ;
cin>>balance;
myfile<<inputCheck<<' ' <<password<<' ' <<balance<<"\n" ;
myfile.close();
cout<<"User account '" <<inputCheck<<"' has been created" <<endl;
}//ends create a password else
//ends if userCheck is open
part1();
}
void part1()
{
cout<<"1.Add User\n2.Edit Information" <<endl;
int input;
cin>>input;
if (input == 1){
addUser();
}else if (input == 2){
//find the user
cout<<"Enter the username: " ;
string checkUser;
//string checkUser;
cin>>checkUser;
ifstream infile("userinfo.txt" ); //CREATING IFSTREAM
ofstream outfile("pleasework.txt" );
//user.open("userinfo.txt");//open the file to be read in
//if(infile.is_open()){//find the user
bool foundUser = false ;
while (infile>>username>>password>>balance){
if (checkUser==username){
foundUser = true ;
//cout<<"User: "<<checkUser<<" Pass: "<<password<<" has been logged in"<<endl;
break ;
}
}
if (foundUser){
cout<<"Current Balance: " <<balance<<endl;
cout<<"Enter new balance: " ;
int newBalance;
cin>>newBalance;
if (infile.is_open()){
outfile<<username<<' ' <<password<<' ' <<newBalance<<endl;
}
}//end input 2
else {
string content;
getline(infile, content);
outfile<<content;
}
infile.close();
outfile.close();
}
}//end main
int main(){
part1();
}
after running:
1 2 3
user1 pass1 43
user2 pass2 550
user3 pass3 65
Last edited on Dec 7, 2016 at 3:58am UTC
Dec 7, 2016 at 3:02am UTC
It's going to be something like:
open read and write filestream
for each line in the text file
if it is the line we want
edit the line and put it in the new file
else if it isnt the line we want
put the unedited line in the new file
close the files
fin
Dec 7, 2016 at 5:05am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
struct bankAc
{
string m_username;
string m_password;
string m_balance;
};
ofstream& operator << (ofstream& os, const bankAc& bA)
{
os << bA.m_username << '\t' << bA.m_password << '\t' << bA.m_balance <<'\n' ;
return os;
}
int main()
{
ifstream inFile("F:\\test.txt" );
ofstream outFile ("F:\\test1.txt" );
bankAc bA;
cout << "Enter name to search: \n" ;
string name;
cin >> name;
bool match = false ;
if (inFile)
{
string line;
while (getline(inFile, line))
{
stringstream stream(line);
stream >> bA.m_username >> bA.m_password >> bA.m_balance;
if (bA.m_username == name)
{
cout << "Name found, enter new a/c balance: \n" ;
string balance;
cin >> balance;
match = true ;
bA.m_balance = balance;
outFile << bA;
}
else
{
outFile << bA;
}
}
}
else
{
cout << "File cannot be opened \n" ;
}
if (match == false )
{
cout << "Name not found \n" ;
}
}
Topic archived. No new replies allowed.