C++ Bank Project Problem

Problem with strcmp

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 5690 Views - Last Post: 15 December 2010 - 01:04 PM Rate Topic: -----

#1 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

C++ Bank Project Problem

Posted 12 December 2010 - 11:29 PM

Greetings guys,

I have a simple problem that is driving me crazy. All because I'm so close into solving it, but just don't know how.

Here is what I'm trying to do.
I have a bank program that is suppose to take in your account number, pin number, and if this information matches what is on a data.in file then you will be able to continue on with other menu options; like deposit, withdraw, loads, balance etc....

so the problem is simple. I figure out a way to have the problem match your account number with the number set inside of the data.in but the pin number(a set of different number) is completely ignored.

Here is my code:
 #include <iostream>
#include <fstream>
#include <string>
using namespace std; 
//function table 

void Deposit(); 
void Withdraw();
void Transfer();  
void Load(); 
void Balance(); 
void Help(); 

int main() 
{
int  option, loop;  
char userid[15], pin[15]; 
char account[15],name[15]; 
//menue
do{//main loop
    do{  //second inside loop 
cout<<"Welcome to BIG FAT BANK FOO ATM!!"<<endl<<endl;
cout<<"Please Enter your account number: ";
cin>>userid; 
cout<<endl; 
cout<<"Please Enter your PIN number: ";
cin>>pin; 
cout<<endl<<endl; 

ifstream fin("BANKDATA.in");
while(fin>>account>>name)
{
                           
if (strcmp(userid, pin) == 0)

 {
  cout<<"Accessing account!..."; 
    loop=0;
 }//end of if
   else {cout<<"User Not Found!!"<<endl; 
cin.ignore(2);
 }//end of else                                 
}//end of inside loop

}while(loop!=0);

cout<<"Here are your options: "<<endl<<endl; 
cout<<"1: Depost  \t"<<"2: Withdraw"<<endl; 
cout<<"3: Transfer  \t"<<"4: Load"<<endl;  
cout<<"5: Balance  \t"<<"6: Help"<<endl<<endl;     
    cout<<"The exit press 0:"<<endl;
    cout<<":";
    cin>>option;  
    //if statements 
    if (option==1)
    {
        Deposit();  
    }
    if (option==2)
    {
        Withdraw();      
    }
    if (option==3)
    {
       Transfer(); 
    }
    if (option==4)
    {
         Load();        
    }
    if (option==5)
    {
       Balance();
    }
    if (option==6)
    {
       Help();
    }
   else {
        cout<<"Invaid entre!! ERROR!!!"<<endl;
        cout<<"Press any button to restart"<<endl; 
        cin.ignore(2);  
        
        } 
}while(option!=0);//end of main loop 

cout<<"Press any button to continue"<<endl;
cin.ignore(2);
return 0;     
}// end of main

void Deposit()
{
 
     
 } 
void Withdraw()
{
    
     
 }
void Transfer()
{
    
 
 }
  
void Load()
{
    
     
 } 
void Balance()
{
    
      
 }
 
void Help()
{ 
 
  
 }
 



And here is what's inside of the data.in file



12345678  
1111  


My fist test would be to make sure that the program can match up your account number with your pin number inside the data.in file. If that were to be false repeat the loop until user inputs the right code.

My second test is to display balances and other account information on that data.in file.

So the program would have to read the first two lines of numbers ( account and pin number) to access the rest of information..

My last test would be to allow the program to edit balance information, much like if a user would deposit, withdraw, or transfer money in a real bank...

So can you guys help me here?

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Bank Project Problem

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: C++ Bank Project Problem

Posted 13 December 2010 - 06:01 AM

if (strcmp(userid, pin) == 0)

Why are you comparing the userid and pin?
Was This Post Helpful? 0
  • +
  • -

#3 thogamer  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 07-November 10

Re: C++ Bank Project Problem

Posted 13 December 2010 - 06:35 AM

EDIT: Sorry I was wrong

[s]while(fin>>account>>name)[/s]


shouldn't this be
[s]while(fin>>userid>>pin)[/s]



ps. I'm new in c++ so my answer can be totally wrong

This post has been edited by thogamer: 13 December 2010 - 06:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: C++ Bank Project Problem

Posted 13 December 2010 - 04:58 PM

You don't like my answer? Why? Does it make sense to compare the user's account number and PIN? It doesn't to me.
Was This Post Helpful? 0
  • +
  • -

#5 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: C++ Bank Project Problem

Posted 13 December 2010 - 05:04 PM

View PostJackOfAllTrades, on 13 December 2010 - 05:01 AM, said:

if (strcmp(userid, pin) == 0)

Why are you comparing the userid and pin?



what i'm trying to do is compare the users input to what is stored in the data.in file.

this line of code almost works but, it ignores the pin number. So if you type in the users account number two times then you will have access to other functions within the program.

Do you know anyway to read user input currently and to compare this user input with the data inside the file data.in, to allow access with the rest of the functions in the program.

View PostJackOfAllTrades, on 13 December 2010 - 03:58 PM, said:

You don't like my answer? Why? Does it make sense to compare the user's account number and PIN? It doesn't to me.


I hit the wrong button... I was putting on my glasses... tehehee no offense dude...

View Postthogamer, on 13 December 2010 - 05:35 AM, said:

EDIT: Sorry I was wrong

[s]while(fin>>account>>name)[/s]


shouldn't this be
[s]while(fin>>userid>>pin)[/s]



ps. I'm new in c++ so my answer can be totally wrong



Doesn't make any sense..
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: C++ Bank Project Problem

Posted 13 December 2010 - 05:10 PM

Your algorithm should be this:

Get account ID from user
Get PIN from user

Create a boolean variable (a flag) to indicate whether you've found the account number (initially false)
Open file
Read account number
Read pin
If account number does not match
   While the flag is false and you've not reached the end of the file
      Read account number
      Read pin
      If account number matches set flag to true
Compare user-entered pin with pin from file


Was This Post Helpful? 0
  • +
  • -

#7 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: C++ Bank Project Problem

Posted 13 December 2010 - 06:28 PM

forgive me for saying. but how do you put that into code?
Was This Post Helpful? 0
  • +
  • -

#8 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: C++ Bank Project Problem

Posted 13 December 2010 - 11:30 PM

View Postyoubob1212, on 13 December 2010 - 05:28 PM, said:

forgive me for saying. but how do you put that into code?

Never mind.. This can be done if I use Strings... LOL Yes I figure out my C++ problem!
Was This Post Helpful? 0
  • +
  • -

#9 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: C++ Bank Project Problem

Posted 14 December 2010 - 10:38 AM

View Postyoubob1212, on 13 December 2010 - 10:30 PM, said:

View Postyoubob1212, on 13 December 2010 - 05:28 PM, said:

forgive me for saying. but how do you put that into code?

Never mind.. This can be done if I use Strings... LOL Yes I figure out my C++ problem!


Never again... I'm totally lost!!!!!!!!!!!!!!!!! I thought I had the answer but yet again I don't...
I have been working on this far too long..

Can anyone help me find the solution??!?!!?!?

#include <iostream>
#include <fstream>
#include <string>
#include <assert.h>
using namespace std;

int main () {
string line;   
string line2;   
string account; 
string pin; 
int mainloop; 

do{
// get user information
cout<<"Enter Account number: ";
getline (cin, account); 
cout<<endl;
cout<<"Enter your Pin: "; 
getline (cin, pin); 
cout<<endl; 
// Testing 

bool flag = false; 

//opening file 
ifstream myfile ("BANKDATA.in");
if (myfile.is_open())
{
while ( myfile.good() )
{
// reading the account and pin
getline(myfile, line); 
if (myfile, account != line)
     {
     while (flag = false&&!myfile.eof())
     {
     cout<<"Access denied!"<<endl;
     cin.ignore(1); 
     }//end of loop
            
     }//end of if statement
else if (myfile, account == line)
{
while(flag = true&&myfile.eof())
{
getline (myfile, line2);
if (strcmp(myfile, pin) == 0)

cout<<"Access granit!"<<endl;
cin.ignore(1); 
}
     
}

     
}// end of file is good loop
       myfile.close();
      }   //end of main testing file is open if statement            
                     
                     

 


}while(mainloop!=0);//end of main loop 



return 0; 
}



Trying to use this format..
01	Get account ID from user
02	Get PIN from user
03	 
04	Create a boolean variable (a flag) to indicate whether you've found the account number (initially false)
05	Open file
06	Read account number
07	Read pin
08	If account number does not match
09	   While the flag is false and you've not reached the end of the file
10	      Read account number
11	      Read pin
12	      If account number matches set flag to true
13	Compare user-entered pin with pin from file

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: C++ Bank Project Problem

Posted 14 December 2010 - 11:11 AM

In the following line it looks like you are trying to compare a file buffer to a std::string using strcmp().

if (strcmp(myfile, pin) == 0)


There are several things wrong.

First: You should be getting an error message saying someting like "strcmp was not declared in this scope."

So you should have posted your error/warning messages along with a proper problem description.

For now let's not worry about why strcmp was not declared and concentrate on the real problem.

The variable myfile is an ifstream ("ifstream myfile ("BANKDATA.in");") and pin is a std::string.

The strcmp() function will only work with "char *" so you don't even want to use strcmp() at all.

One problem solved (you can't use strcmp()).

Now you also can not compare two different types to each other. I would say that you want to compare the std::string pin to something else.

Am I correct about that?

If so what do you want to compare the std::string pin to?

Quote

Read pin
If account number does not match


So with this quote it looks like you want to compare the std::string pin to the account number.

Is this correct?

If it is what is the variable name for your account number?

Could it be the std::string account?

If it is how do you compare two std::strings?

Would this work "if(account == pin)"?

There are still other problems in your code but see if you can now implement the lines in question.

Then post the modified code and any error/warning messages.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: C++ Bank Project Problem

Posted 14 December 2010 - 11:33 AM

View Postjimblumberg, on 14 December 2010 - 10:11 AM, said:

In the following line it looks like you are trying to compare a file buffer to a std::string using strcmp().

if (strcmp(myfile, pin) == 0)


There are several things wrong.

First: You should be getting an error message saying someting like "strcmp was not declared in this scope."

So you should have posted your error/warning messages along with a proper problem description.

For now let's not worry about why strcmp was not declared and concentrate on the real problem.

The variable myfile is an ifstream ("ifstream myfile ("BANKDATA.in");") and pin is a std::string.

The strcmp() function will only work with "char *" so you don't even want to use strcmp() at all.

One problem solved (you can't use strcmp()).

Now you also can not compare two different types to each other. I would say that you want to compare the std::string pin to something else.

Am I correct about that?

If so what do you want to compare the std::string pin to?

Quote

Read pin
If account number does not match


So with this quote it looks like you want to compare the std::string pin to the account number.

Is this correct?

If it is what is the variable name for your account number?

Could it be the std::string account?

If it is how do you compare two std::strings?

Would this work "if(account == pin)"?

There are still other problems in your code but see if you can now implement the lines in question.

Then post the modified code and any error/warning messages.

Jim


Ok my brain is full... So I don't know what I'm doing.. I'm at the point of throwing code out into the air to see what works or not. Ignore all examples of code here!!

So this is what I'm trying to do..


1: Get user Input, on account number, and pin number.
2: Open a file and read content.
3: See if content matches user account number,and pin.
4: If not have the user to repeat until user inputs the right account numbers, and pin
5: If Correct, have the user to see other options such as Withdraw, deposit, and etc....
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: C++ Bank Project Problem

Posted 14 December 2010 - 11:45 AM

Ok so start with step 1.

You will need a couple of variables to hold what the user will enter.

What type of variables do you think you should use?

1. double
2. int
3. char
4. std::string
5. C-string (null terminated character string)

The choice of which type of variable to use will depend on what is in your data file. So please post a sample of your data file that has at least 10 entries.

Jim
Was This Post Helpful? 0
  • +
  • -

#13 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: C++ Bank Project Problem

Posted 14 December 2010 - 12:20 PM

Ok I think either char or string would do the trick...

The data file would look something like this..

bankaccount.in
// first line would be the account number
12345678
//second line would be the pin number
1111 
//third line would be the name of another .in file to where other account information would be stored.. 


Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: C++ Bank Project Problem

Posted 14 December 2010 - 12:26 PM

Quote

Ok I think either char or string would do the trick...


Remember a char will only hold 1 character. So I would suggest std::string.

Now to your input file. Are you defining your input file or is someone else?

The reason I ask what you shown me doesn't make a lot of sense.

Why would you want 1 account to know anything about a totaly different account?

Jim
Was This Post Helpful? 0
  • +
  • -

#15 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: C++ Bank Project Problem

Posted 14 December 2010 - 01:04 PM

well it's easier in the long run to have 2 different files that relate to each other..

One file would just be the account number and pin number.. IF it matches read the third line, and put that data into
ifstream myfile ("Thirdline");
The third would be the name of the next file to access.. so...
ifstream myfile ("Account_101.in");


Now well open up the second file that has information on account balance and history...

Once this file is open in our c++ program it can be viewed or change in other options in the program.. Such as Withdraw, (would take away the balance value) and deposit( that would add to balance value)

This Second file would look like this
Account101.in
//balance number
1,000
//account history would come next


Now you might be saying, "Why not have all of this information under one file?". Well that works with one person account.. But as soon as you start to have more users it will make the coding more complex, and error pron... Such as if anything is out of short it might display other users account information instead of the right one.

Here is an example of what I mean.

bankaccounts.in
//first user account
12345678 
//pin number 
1111
//current account balance 
$1,000
//And some history 
$-100 date:03/12/10
//this history can be very long, and after your done you would repeat the same information for the next user, making this file VERY huge, and very error pron in no time. 


That's about it.. The point of writing this program is to show how real banks might do their data banks, and shows an mini example on how you might get a program to do what the same job, as our larger real life banks might preform.

Were I'm having trouble in my code is how to get the code to take the users information (just his/her 8 digit account number and 4 digit pin number) and match it up with the information inside of an .in or txt file. If it matches continue onward to display other information about this account information.( we pull up the second file.. and so on..).

I believe I know how to display and edit files just fine in C++ but never tried to compare user input to a text file.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2