making an ATM Program in C++ "Help"
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: making an ATM Program in C++ "Help"

  1. #1
    IT Specialist Ghost_25inf's Avatar
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    648

    making an ATM Program in C++ "Help"

    Statement of problem

    I cant get this program to work right. I keep getting this error message.
    (63) : error C2296: '+' : illegal, left operand has type 'float (__thiscall userInfo::*)(void)'

    Ive tried several diffferent ways to run the account to write to file but it doesnt seem to work. I got the Password and ID to work ok but I just cant seem to get the rest. any suggestions?

    this is the Main program.




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

    int main ()
    {
    userInfo userId;
    string userID ="";
    string userP ="";
    string ID,testId, testPassword;
    string password;
    char choice =' ';

    float Dep=0.00;
    float with=0.00;
    float bal=0.00;
    string err ="**** ERROR ****";
    ofstream outFile;
    ifstream inFile;
    inFile.open("user.txt", ios::in);

    if (inFile.is_open())
    {
    testId=userId.getID();
    system("cls");
    testPassword = userId.getP();
    system("cls");
    }

    while ( !inFile.eof())
    {
    inFile >> ID;
    inFile >> password;
    if ((ID==testId) && (password ==testPassword))
    {

    cout << "**** Welcome " << userID << " ****" << endl;
    cout << endl;
    cout << "1 for Deposit" << endl;
    cout << "2 for Withdrawl. " << endl;
    cout << "3 for Balance." << endl;
    cout << "4 for exit." << endl;
    cin >> choice;
    system("cls");
    }
    else
    {
    cout << err << endl;
    return 0;
    }


    if ( choice = '1' )
    {
    outFile.is_open();

    cout << "**** DEPOSIT ****" << endl;
    cout << endl;
    cout << "Enter the amount of deposit to savings account: ";
    cin >> Dep;
    userId.updateA();
    }
    else
    if ( choice ='2')
    {
    cout << "**** WITHDRAWL ****" << endl;
    cout << endl;
    cout << "Enter the amount of the withdrawl from savings account: $";
    cin >> with;
    }
    else
    if ( choice ='3')
    {
    inFile.is_open();
    bal = userId.getA();
    cout << "**** BALANCE ****" << endl;
    cout << endl;
    cout << "Your savings balance is $" << bal << endl;
    }
    else
    if ( choice ='4' )
    {
    cout << "**** EXIT ****" << endl;
    cout << endl;
    return 0;
    }
    else
    {
    cout << err << endl;
    return 0;
    }
    inFile.close();
    return 0;
    }
    return 0;
    }


    and this is the Class or Header file

    # include <fstream>
    # include <string>
    using namespace std;


    class userInfo
    {
    public:
    userInfo();
    void readFile (ifstream &);
    float writeFile (ofstream &);
    string getID();
    string getP();
    float getA();
    float updateA();
    private:
    string ID;
    string Pass;
    float Acc;
    };
    userInfo::userInfo()
    {
    ID = "";
    Pass = "";
    Acc = 0.00;
    // float newbal=0.00;
    // float update=0.00;
    }
    void userInfo::readFile( ifstream &inF)
    {

    inF >> ID;
    inF.ignore(1);

    inF >> Pass;
    inF.ignore(2);

    inF >> Acc;
    inF.ignore(3);
    }
    string userInfo::getID()
    {
    cout <<"enter ID: ";
    cin >> ID;
    return ID;
    }
    string userInfo::getP()
    {
    cout << "Enter Password: ";
    cin >> Pass;
    return Pass;
    }
    float userInfo::getA()
    {
    cout << "Your Account balance is $" << Acc << endl;
    return Acc;
    }
    float userInfo::writeFile(ofstream &outF)
    {
    float newbal = 0.00;
    //update = updateA;
    outF.is_open();
    newbal=(updateA + Acc);
    outF << newbal;
    return Acc;
    outF.close();
    }

    and here is the Text file

    Ghost IG1234 100.00
    S25vd2xlZGdlIGlzIHBvd2VyIQ

  2. #2
    Member
    Join Date
    Nov 2002
    Posts
    80
    I am not sure what your updateA() function is supposed to do but it looks like it returns a float value and you are trying to use it as just a statement on its own. Effecively causing the same error as just typing 0.01 or any other number on a line by itself.

    I think you need to assign this to something for instance
    Float var1;
    var1 = userId.updateA();

    A good practice when something doesn't work is to comment out everything apart from the bits you know work and then comment it in one line at a time. (sorry if I am stating the obvious)

    Waverebel

    Sorry I can't give a very detailed answer Java is my thing not C++

  3. #3
    IT Specialist Ghost_25inf's Avatar
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    648
    I was using updateA() to add the deposit and or withdrawal from the .cpp file to the header file. I usually do put comments in my programs but I find that sometimes they get in my way when I make changes.
    S25vd2xlZGdlIGlzIHBvd2VyIQ

  4. #4
    Member
    Join Date
    Nov 2002
    Posts
    80
    It looks like you have declared it to return a float value
    float updateA();
    and then not handled that value when you use it
    userId.updateA();

  5. #5
    Senior Member
    Join Date
    Mar 2003
    Location
    central il
    Posts
    1,779
    if you declare updateA as a void it should work

  6. #6
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    I had a look at your code and I noticed a few things to start with:

    1.) You're falling into one of the common newbie traps of code:

    Code:
    if ( choice = '1' ) { // do blah!!! }
    Wrong. This is assigning 1 to choice which will always equate to true. This is a logical error and it won't be picked up by the compiler because it's syntactically correct. The code you want is

    Code:
    if ('1' == choice) { // do blah!!! }
    This way you'll never miss the double = comparison time because you can't assign a value to a constant, but you can compare. Do it this way from now on and you'll save yourself (and anyone debugging your code) many hours.

    2.) You tried to execute a statement after returning from a function. Think about it. Does that look right? I took the liberty of ditching the last statement for you. The program now compiles and runs.

    Code:
    float userInfo::writeFile(ofstream &outF) 
    { 
    float newbal; 
    //update = updateA; 
    outF.is_open(); 
    newbal=updateA() + Acc; 
    outF << newbal; 
    return Acc; 
    // outF.close(); /* a statement after the function returns - wtf??? */
    }
    3.) You're sort of missing the point of OO programming. You should have a 'Person' or 'Customer' class not a *info class. OO programming is meant to encapsulate Data + Methods to work on the data. The object is not the data itself. Instances of an object contain data, the object class itself is just a blueprint for creating objects.

    So you want a class, Person for example with attributes (private variables) and methods (public functions) to access those variables. readFile and writeFile are not methods that should act on an object of type Person. They should be either in their own class or in the main program. You should also create classes for all object that are involved in transactions such as Account, Customer etc.

    4.) You're either using header files or you're not. Make up your mind. If you're going to use them, each .cpp file should have a corresponding .h file. Good header files should be written and commented so that an API user doesn't even have to look at the .cpp source. The header file should explain everything.

    That's all for now kids.
    OpenBSD - The proactively secure operating system.

  7. #7
    IT Specialist Ghost_25inf's Avatar
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    648
    ok I changed my header here is the result I came up with.


    # include <fstream>
    # include <string>
    using namespace std;


    class userInfo
    {
    public:
    userInfo();
    void readFile (ifstream &);
    float writeFile (ofstream &);
    string getID();
    string getP();
    float getA();
    float updateA();
    private:
    string ID;
    string Pass;
    float Acc;
    };
    userInfo::userInfo()
    {
    ID = "";
    Pass = "";
    Acc = 0.00;
    }
    void userInfo::readFile( ifstream &inF)
    {

    inF >> ID;
    inF.ignore(1);

    inF >> Pass;
    inF.ignore(2);

    inF >> Acc;
    inF.ignore(3);
    }
    string userInfo::getID()
    {
    cout <<"enter ID: ";
    cin >> ID;
    return ID;
    }
    string userInfo::getP()
    {
    cout << "Enter Password: ";
    cin >> Pass;
    return Pass;
    }
    float userInfo::getA()
    {
    cout << "Your Account balance is $" << Acc << endl;
    return Acc;
    }
    float userInfo::writeFile(ofstream &outF)
    {
    float depBal;
    outF.is_open();
    depBal = updateA() + Acc;
    outF << depBal;
    return Acc;
    }
    float userInfo::writeFile(ofstream &outF)
    {
    float withBal;
    outF.is_open();
    withBal = updateA() - Acc;
    outF << withBal;
    return Acc;
    }



    I also changed the Main file. Here is the results of that.

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

    int main ()
    {
    userInfo userId;
    string userID ="";
    string userP ="";
    string ID,testId, testPassword;
    string password;
    char choice =' ';

    float Dep=0.00;
    float with=0.00;
    float bal=0.00;
    string err ="**** ERROR ****";
    ofstream outFile;
    ifstream inFile;
    inFile.open("user.txt", ios::in);

    if (inFile.is_open())
    {
    testId=userId.getID();
    system("cls");
    testPassword = userId.getP();
    system("cls");
    }

    while ( !inFile.eof())
    {
    inFile >> ID;
    inFile >> password;
    if ((ID==testId) && (password ==testPassword))
    {

    cout << "**** Welcome " << userID << " ****" << endl;
    cout << endl;
    cout << "1 for Deposit" << endl;
    cout << "2 for Withdrawl. " << endl;
    cout << "3 for Balance." << endl;
    cout << "4 for exit." << endl;
    cin >> choice;
    system("cls");
    }
    else
    {
    cout << err << endl;
    return 0;
    }


    if ('1' == choice )
    {
    outFile.is_open();

    cout << "**** DEPOSIT ****" << endl;
    cout << endl;
    cout << "Enter the amount of deposit to savings account: ";
    cin >> Dep;
    userId.getA();
    }
    else
    if ('2' == choice )
    {
    cout << "**** WITHDRAWL ****" << endl;
    cout << endl;
    cout << "Enter the amount of the withdrawl from savings account: $";
    cin >> with;
    userId.getA();

    }
    else
    if ( '3' == choice )
    {
    inFile.is_open();
    bal = userId.getA();
    cout << "**** BALANCE ****" << endl;
    cout << endl;
    cout << "Your savings balance is $" << bal << endl;
    }
    else
    if ( '4' == choice )
    {
    cout << "**** EXIT ****" << endl;
    cout << endl;
    return 0;
    }
    else
    {
    cout << err << endl;
    return 0;
    }
    inFile.close();
    return 0;
    }
    return 0;
    }

    this is the problem I now come up with


    --------------------Configuration: Final2 - Win32 Debug--------------------
    Compiling...
    ATM.cpp
    \\******\c$\program files\microsoft visual studio\myprojects\1208\ATM.h(65) : error C2084: function 'float __thiscall userInfo::writeFile(class std::basic_ofstream<char,struct std::char_traits<char> > &)' already has a body
    Error executing cl.exe.

    ATM.obj - 1 error(s), 0 warning(s)
    S25vd2xlZGdlIGlzIHBvd2VyIQ

  8. #8
    IT Specialist Ghost_25inf's Avatar
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    648
    still having trouble with is program. still getting this error message.
    S25vd2xlZGdlIGlzIHBvd2VyIQ

  9. #9
    Banned
    Join Date
    Oct 2002
    Posts
    121
    goto www.experts-exchange.com

    post ur question in there and it will get answered within an hour

  10. #10
    IT Specialist Ghost_25inf's Avatar
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    648
    I tried posting my question there but every time I post I get a server problem and no text shows up on my post so I wasted points on a blank post. anyways AO has never let me down before and find the people here to be much more help than anywhere else
    S25vd2xlZGdlIGlzIHBvd2VyIQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •