12 Replies - 14416 Views - Last Post: 21 July 2010 - 08:36 PM Rate Topic: -----

#1 Irish_Kid  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 10-July 10

ATM program using text file input/output, classes, and arrays.....HELP

Posted 10 July 2010 - 02:53 PM

i have a project due next week for my C++ debug class but he's making us write a program from scratch.
i have done an atm machine before but i havent dealt with files.

basicaly what i have o do is ceate an ATM program that first prompts for a user name and password.
there is going to be 100 users (meaning 100 different user names, password, full names, and amount of money)
i am completely lost on how 'classes' work, and on top of that he's telling us to use arrays to get the information.
i dont need any information on how to build the atm, just on scanning files to check for info, using arrays to SECURELY hold the info(he said something about the difference between public and private classes), and text file in the correct format.

PLEASE HELP!!

Is This A Good Question/Topic? 0
  • +

Replies To: ATM program using text file input/output, classes, and arrays.....HELP

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,662
  • Joined: 23-August 08

Re: ATM program using text file input/output, classes, and arrays.....HELP

Posted 10 July 2010 - 02:57 PM

Here's an object-oriented programming tutorial.

There are a ton of other tutorials, too.

There's also a search function, because as you might imagine when dealing with an Internet forum that's been around for almost ten years, there have been no shortage of questions regarding all of the topics you've touched upon in your opening post.
Was This Post Helpful? 0
  • +
  • -

#3 Irish_Kid  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 10-July 10

Re: ATM program using text file input/output, classes, and arrays.....HELP

Posted 10 July 2010 - 03:21 PM

thanks dude, ima check it out
Was This Post Helpful? 0
  • +
  • -

#4 Irish_Kid  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 10-July 10

Re: ATM program using text file input/output, classes, and arrays.....HELP

Posted 21 July 2010 - 07:03 PM

ok so i figured a bunch of shit out from random posts and from some tutorials on the site. now i am stuck on one more thing, the very last thing i need for my program is to rewrite everything back into the file to update once the progam has run it's course. what i have so far is as followed:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;

class User
{
private:
     string id;
     string firstName;
     string lastName;           
     int pin;
     double cash;

public:

     User()
     {
                                                                                //leave empty
     }

     User(string i, string f, string l, int p, double c)
     {

           id = i;
           firstName = f;
           lastName = l;           
           pin = p;
           cash = c;
           
     }
     
          
     void changePin(int p)
     {
          
          cout<<"Please select a new 4-digit numerical pin"<<endl<<"# ";
          cin>>p;
          
          while(p < 999 || p > 10000)
          {           
                      cout<<"Invalid Pin. "<<endl<<"Please select a new 4-digit numerical pin"<<endl<<"# ";
                      cin>>p;
                      system("cls");
          }
          
          pin = p;
          cout<<"You pin has been changed to "<<pin<<endl;
                    
     }
     
     int getPin()
     {
            return pin;
     }
     double getCash()
     {
            return cash;
     }
     string getFirstName()
     {
            return firstName;
     }
     string getLastName()
     {
            return lastName;
     }     
     string getId()
     {
            return id;
     }
     
     double setWithdraw(double w)
     {

            while(cash < w)
            {            
                  cout<<"You tryen to rob me? You don't have that much"<<endl
                      <<"How Much would you like to withdraw?"<<endl<<"$ ";
                  cin>>w;
            }
                  return cash -= w;
                                                  
     }
     
     double setDeposit(double d)
     {
            while(d > 10000)
            {
                   cout<<"I would be all about taking more than $10,000 from you and all, "
                       <<"but it jacks the program up and robs you."<<endl
                       <<"How much would you like to deposit?"<<endl<<"$ ";
                       cin>>d;
            }            
            return cash += d;                               
     }     
      
     bool isTrue(string tempId, int tempPin)
     {
          
          if(id == tempId && pin == tempPin)
          {                                                       
             return true;           
          }
          
          else          
          { 
             return false; 
          }        
     }     

     ifstream done;
     done.open("bank.txt");

           
     void getAll(totalUsers)
     {
             for(int count = 0; count <= totalUsers; count++)
             {
             done >> users[count] ;       
             }      
     }
  
     done.close();  
             
};



int main()
{    
	ifstream database;
	database.open("bank.txt");
    
    int ui;
    int ui2;
    int tempPin;
	int totalUsers = 0;
    int activeUser = 0;                     
    double tempCash;
    double withdraw;
    double deposit;
    string tempId;        
    string tempFirstName;
    string tempLastName;                                                             
	User users[100];

           
	while (!database.eof())
	{
		database >> tempId >> tempFirstName >> tempLastName >> tempPin >> tempCash;		        //get input from file
		User tempUser(tempId, tempFirstName, tempLastName, tempPin, tempCash);		            //use constructor to create a temp object
		users[totalUsers] = tempUser;		                                                    //assign temp object to a spot in our array
		totalUsers++;                                                                           //increment count for the next item
	}    
    database.close();



    do
    {
          cout<<"Welcome to da bank yo."<<endl
              <<"Please enter your user id: ";
          cin>>tempId;//get user id
          
          cout<<"Please enter your 4-digit numerical pin: ";
          cin>>tempPin;//get user pin
          
          system("cls");//clear
                               
          activeUser = 0;           
           
           while(users[activeUser].isTrue(tempId, tempPin) == false && activeUser < totalUsers)
           {

                  activeUser++;
                  system ("cls");      
           }                
 
    }while(activeUser == totalUsers);
    

    
     

                  
    cout<<"Welcome back "<<users[activeUser].getFirstName()<<" "<<users[activeUser].getLastName()<<", What would you like to do?"<<endl;
    cout<<"1) View Account Info"<<endl
         <<"2) Withdraw"<<endl
         <<"3) Deposit"<<endl
         <<"4) Change Pin"<<endl
         <<"5) Quit"<<endl<<endl
         <<"What action would you like to take? ";
           
    cin>>ui;
    system("cls");
    while (ui != 5)
    {       
    while (ui < 1 || ui > 5)
    {
            system("cls");
            
            cout<<"Invalid Selection, Please Choose Again"<<endl<<endl;
            
            cout<<"1) View Account Info"<<endl
                <<"2) Withdraw"<<endl
                <<"3) Deposit"<<endl
                <<"4) Change Pin"<<endl
                <<"5) Quit"<<endl<<endl
                <<"What action would you like to take? ";
                
            cin>>ui;
            system("cls");
    }
        
     
    if (ui == 1)
    {
           cout<<"Name: "<<users[activeUser].getFirstName()
               <<" "<<users[activeUser].getLastName()<<endl;
           cout<<"ID: "<<users[activeUser].getId()<<endl;
           cout<<"Dough: $"<<users[activeUser].getCash()<<endl;   
    }
     
     
    if (ui == 2)//withdraw
    {
           cout<<"How Much would you like to withdraw?"<<endl<<"$ ";
           cin>>withdraw;
           users[activeUser].setWithdraw(withdraw); 
    }
     
     
    if (ui == 3)//deposit
    {   
           cout<<"How Much would you like to deposit?"<<endl<<"$ ";
           cin>>deposit;
           users[activeUser].setDeposit(deposit);
    }
     
     
    if (ui == 4)//pin change
    {
           users[activeUser].changePin(tempPin);     
    }
     
     
    if (ui == 5)//exit
    {
            
    }
    
    cout<<"1)Back to Main Menu?"<<endl
        <<"2)Quit"<<endl;
    cin>>ui2;
    
    while(ui2 < 1 || ui2 > 2)
    {
    system("cls");
    cout<<"Invalid selection."<<endl;
    cout<<"1)Back to Main Menu?"<<endl
        <<"2)Quit"<<endl;
    cin>>ui2;              
    }
    system("cls");
    
    if(ui2 == 1)
    {      
    cout<<"1) View Account Info"<<endl
        <<"2) Withdraw"<<endl
        <<"3) Deposit"<<endl
        <<"4) Change Pin"<<endl
        <<"5) Quit"<<endl<<endl
        <<"What action would you like to take? ";
                
    cin>>ui;
    system("cls");
    }
    
    else
    {
        ui = 5;                                                                 //exit loop
    }
    }
     


    cout<<"Come back soon and gimme some more money."<<endl<<"GOODBYE"<<endl<<endl;


system ("pause");
return 0;
}


basically im stuck on the section in the class


     void getAll(totalUsers)
     {
             for(int count = 0; count <= totalUsers; count++)
             {
             done >> users[count] ;       
             }      
     }
  
     done.close();



this section, i cannot figure out how to reprint the data back to file.
can someone please help?
it's due tom. and i'm completely outa ideas!

This post has been edited by no2pencil: 21 July 2010 - 07:04 PM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#5 Irish_Kid  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 10-July 10

Re: ATM program using text file input/output, classes, and arrays.....HELP

Posted 21 July 2010 - 07:10 PM

this part of the code:

     void getAll(totalUsers)
     {
             for(int count = 0; count <= totalUsers; count++)
             {
             done >> users[count] ;       
             }      
     }
  
     done.close();


This post has been edited by no2pencil: 21 July 2010 - 07:12 PM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5393
  • View blog
  • Posts: 27,387
  • Joined: 10-May 07

Re: ATM program using text file input/output, classes, and arrays.....HELP

Posted 21 July 2010 - 07:11 PM

... as previously noted, you are not using code tags.

:code:

Please use them thank you.
Was This Post Helpful? 0
  • +
  • -

#7 Irish_Kid  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 10-July 10

Re: ATM program using text file input/output, classes, and arrays.....HELP

Posted 21 July 2010 - 07:17 PM

this is the section:

     done.open("bank.txt")

     void getAll(totalUsers)
     {
             for(int count = 0; count <= totalUsers; count++)
             {
             done >> users[count] ;       
             }      
     }
  
     done.close();



Was This Post Helpful? 1
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: ATM program using text file input/output, classes, and arrays.....HELP

Posted 21 July 2010 - 07:49 PM

you declare that section of code within your class! - thats wrong. You can only declare variables and functions within classes, you cannot initialize or call functions within a calls, those must be done within the functions within the class

     void getAll(sizee_t totalUsers) //you need a type and a variable
     {
		 ifstream bank_stream("bank.txt");
         for(int count = 0; count <= totalUsers; count++) //what happens if count is greater than the size of the array?
         {
				done >> users[count] ;   // what is users? where is is coming from? is it a std::string?    
         }      
		 one.close();  
     }


Was This Post Helpful? 1
  • +
  • -

#9 Irish_Kid  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 10-July 10

Re: ATM program using text file input/output, classes, and arrays.....HELP

Posted 21 July 2010 - 07:55 PM

totalUsers is the size of the array (the total entries in the text file) and thats why count cannot go passed it to avoid a crash

the users[] is the array that all the information is held in...thats where im stuck so i don't have code for that section

i need to send the entire array back to the text file so it is saved for next use
Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: ATM program using text file input/output, classes, and arrays.....HELP

Posted 21 July 2010 - 08:16 PM

Heres a very simple on how you could dump your data to a file

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class User
{
public:
	string id;
	string firstName;
	string lastName;           
	int pin;
	double cash;
};

void dumpToFile(User users[], size_t size, const char *path)
{
	std::ofstream bank_stream(path); //open file
	if(!bank_stream) //file could not open
		return; //exit function, go back to who ever called me
	for(int i=0; i<size; i++)
		bank_stream<<users[i].id<<' '<<users[i].firstName<<' '<<users[i].lastName<<' '<<users[i].pin<<' '<<users[i].cash<<endl; //write data line to file
	bank_stream.close(); //close file
	
}


int main()
{
	User users[2];
	for(int i=0; i<2; i++)
	{
		cout<<"ENTER FIRST NAME : ";
		cin>>users[i].firstName;
		cout<<"ENTER LAST NAME : ";
		cin>>users[i].lastName;
		cout<<"ENTER ID : ";
		cin>>users[i].id;
		cout<<"ENTER CASH :";
		cin>>users[i].cash;
	}
	dumpToFile(users, 2, "dumpedData.txt");
	cout<<"I JUST DUMPED THE DATA TO A FILE"<<endl;

	cin.ignore();
	cin.get();


	return 0;
}



Was This Post Helpful? 1
  • +
  • -

#11 Irish_Kid  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 10-July 10

Re: ATM program using text file input/output, classes, and arrays.....HELP

Posted 21 July 2010 - 08:25 PM

im assuming it shuld look something like this:


     void getAll(int totalUsers)
     {
             ifstream done;
             done.open("bank.txt")
             ;
             for(int count = 0; count <= totalUsers; count++)
             {
                     done << id<<" "<< firstName<<" "<< lastName<<" "<< pin<<" "<< cash;       
             }
             
             done.close();      
     }




but i need to find a way to call each index of the aay and thro that info back out

*but i need to find a way to call each index of the array and thro that info back out*
Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: ATM program using text file input/output, classes, and arrays.....HELP

Posted 21 July 2010 - 08:34 PM

Quote

but i need to find a way to call each index of the aay and thro that info back out


Use a for loop
Was This Post Helpful? 1
  • +
  • -

#13 Irish_Kid  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 10-July 10

Re: ATM program using text file input/output, classes, and arrays.....HELP

Posted 21 July 2010 - 08:36 PM

dude, your a fucking life saver
thank you so much man this worked pefectly!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1