Help with Money-owed Bank

This is a discussion on Help with Money-owed Bank within the C++ Programming forums, part of the General Programming Boards category; So I've worked on my first big program in a DOS Screen, it's where you input a command and it'll ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    Post Help with Money-owed Bank

    So I've worked on my first big program in a DOS Screen, it's where you input a command and it'll return the function defined with it (duh) It's not working for some reason though.

    Code for main.cpp
    Code:
    #include <fstream>
    #include <stdlib.h>
    #include <iostream>
    
    using namespace std;
    
    //Storing code: "int args, char *argv[]"
    int main()
    {
        int input;
        float moneyvar;
        char naming_interger;
        float new_money;
        float math;
        int input2;
        
        cout<< "Welcome to the Payback Bank!\nType new to create an account.\nType get to see a file.\nType change to add or subtract money owed.\nType clear to clear all owed money from an account.";
        cin>> input;
        cin.ignore();
        switch ( input )
        {
               case 'NEW':
                    cout<< "Who owes you money? Please type the name:\n";
                    cin>> naming_interger;
                    ofstream new_account(naming_interger".dat");
                    cout<< "The person who owes you money is "<< naming_interger<< "\nHow much are you owed?";
                    cin>> new_money;
                    cin.ignore();
                    cout<< "Writing money to file...";
                    new_account << new_money;
                    cout<< "Done! The process was successful!\n";
                    cout<< "The person "<< naming_interger<< " owes you "<< new_money<< "\n";
                    new_account.close();
                    system("PAUSE");
                    return 0;
                    break;
               case 'GET':
                    cout<< "Who's account would you like to view?\n";
                    cin>> naming_interger;
                    cin.ignore();
                    ifstream read_account(naming_interger".dat");
                    if (read_account.fail())
                    {
                        cout<< "There was an error, perhaps the file doesn't exist?\n";
                        system("PAUSE");
                    }
                    cout<< naming_interger<< " owes you "<< read_account<< "\n";
                    read_account.close;
                    system("PAUSE");
                    break;
               case 'CHANGE':
                    cout<< "Who's account are you changing?";
                    cin>> naming_interger;
                    cin.ignore();
                    ifstream change_account(naming_interger".dat");
                    change_account>> moneyvar;
                    if (read_account.fail())
                    {
                        cout<< "There was an error, perhaps the file doesn't exist?\n"<<;
                        system("PAUSE");
                    }
                    cout<< "You loaded "<< naming_interger<< "'s account.\nHe/She owes you"<< moneyvar<< "\nHow much do you want to add? (HINT: Place a - to subtract)\n";
                    cin>> math;
                    cin.ignore();
                    cout<< "Adding money to account, please wait...\n";
                    moneyvar = moneyvar + math;
                    ofstream update_account(naming_interger".dat");
                    moneyvar>> update_account;
                    update_account.close;
                    cout<< "The account update was succesful!\n";
                    system("PAUSE");
                    break;
               case 'CLEAR':
                    cout<< "Who paid back your money?\n";
                    cin>> naming_interger;
                    cin.ignore();
                    ifstream clear_account(naming_interger".dat");
                    clear_account>> moneyvar;
                    {
                        cout<< "There was an error, perhaps the file doesn't exist?\n"<<;
                        system("PAUSE");
                    }
                    cout<< "You loaded "<< naming_interger<< "'s account.\n";
                    cout<< "Clear this account's data? (y/n)This does NOT erase the account.\n";
                    cin>>input2;
                    if (input == y)
                    {
                              cout<< "Erasing owed money data.";
                              moneyvar = 0;
                              ofstream clearing_account(naming_interger".dat");
                              clearing_account.close
                              cout<< "The account's owed money was cleared successfully!\n";
                              system("PAUSE");
                    }
                    else
                    {
                        return 0;
                    }
                    break;
               default:
                       {
                              cout<< "Failure to read command.\n";
                              system("PAUSE");
                              break;
                       }
               }
        }
    }
    ofstream new_account(naming_interger".dat"); is where it mainly fails to compile, I am using Dev-C++ 4.9.9.2.
    My guess is that you can't store a variable into a file's name, but I want to, can I get some help here?
    What this program's plot is is it loads and saved accounts on how much money somone owes you.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well... I am guessing you are a php coder... That is not how strings are concatenated in C++...

    Try changing naming_interger to a std::string (thus you need to include <string>) and putting a plus sign between the string literal and the variable.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    char is for one single character. You declare naming_integer as a char, but you expect it to store a name (thus several characters). You have to declare it as a character array char[] or better as a string and just do what master5001 said.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Even if it were an array, that is not how you concatenate a string. If you want to use a char array, that is fine.

    Example:
    Code:
    #include <cstring>
    
    ...
    
    char naming_interger[50];
    
    ...
    
    strncat(naming_interger, ".dat", 50);

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    Array Declare
    Code:
        char name[10];
    Array in action
    Code:
               case 'NEW':
                    cout<< "Who owes you money? Please type the name:\n";
                    cin>> name[10];
                    ofstream new_account(name, ".dat", 10);
                    cout<< "The person who owes you money is "<< name<< "\nHow much are you owed?\n";
                    cin>> new_money;
                    cin.ignore();
    Hm... It still gives me a compile error, debugger said what draws my attention:
    26 C:\Documents and Settings\GlitchGuy2\My Documents\C++\Bank\Bank.cpp no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(char[10], const char[5], int)'

    note C:\Dev-Cpp\include\c++\3.4.2\iosfwd:93 candidates are: std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)

    note C:\Dev-Cpp\include\c++\3.4.2\iosfwd:93 std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]

    note C:\Dev-Cpp\include\c++\3.4.2\iosfwd:93 std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT = char, _Traits = std::char_traits<char>]
    If I didn't mention, this is my first REAL language, I've programmed in Game Maker and GML, but never in PHP and I'm new to C++.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Did any of what I said or showed you suggest to rewrite your code like that? I do not mean to talk down to you or anything, I just want to make sure I am being clear and if now, it helps me to know in what way I am being cryptic.

    Code:
               case 'NEW':
                    cout<< "Who owes you money? Please type the name:\n";
                    cin>> name[10];
                    strncat(name, ".dat", 10);
                    ofstream new_account(name);
                    cout<< "The person who owes you money is "<< name<< "\nHow much are you owed?\n";
                    cin>> new_money;
                    cin.ignore();

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    Alright, thanks, and what happened was I didn't understand the string declaring, so I tried arrays, either way, my debug problem moved into the code some more... But this doesn't make sence to me...
    Code:
               case 'GET':
    That's where the problem is now, what the heck though, don't see anything wrong with it, and I included the "break;" before moving to this line.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well since this is C++ and not Visual Basic, your "case 'GET'" is erroneous. You can't do case statement with string literals. And furthermore, 'GET' is not even a properly formed string literal.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    Then how do I get through that? I changed the variable 'input' from an interger to a character, and then changed GET to g, New to n, ect. but it still fails to compile at the same place.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok, you can do 'G' just fine. I suggest doing the char thing and doing this:

    Example:
    Code:
    // At the top of your file somewhere
    #include <cctype>
    ...
    
    // now back where your code is...
    cin >> input;
    
    switch(toupper(input))
    {
    case 'G':
    // and so on...

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unfortunately, it's quite hard to know what is wrong, as you have changed the original code, and not posted the latest set of error messages. In general, we would need both the actual error messages from the compiler and the source code.

    A small trick that you may find handy: Long text lines can be broken up over several lines, like this:
    Code:
        cout<< "Welcome to the Payback Bank!\n"
         "Type new to create an account.\n"
         "Type get to see a file.\n"
         "Type change to add or subtract money owed.\n"
         "Type clear to clear all owed money from an account.";
    Code:
    cin>> name[10];
    You probably do not want to put the 10 there, because that tells the compiler to "read one character into position 10". Since your string has 10 elements numbered 0..9, element number 10 is out of range, so bad things will happen if you do that. [How bad, and when/if you see the effect varies, but it's definitely never a good thing].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,451
    strncat(name, ".dat", 10);
    the last parameter specifies number of characters to append.
    So because ".dat" is only 4 chars long - it is irrelevant and does not prevent buffer overrun.

    better yet switch to usage of std::string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    On a side note, you can set your case statements to actual words if you store them in an enumuation.

    Code:
    enum Options
    {
       GET = 'G',
       STORE = 'S'
    };
    
    switch ( choice ) // let us assume 'choice' is a char data type
    {
    // if 'G' is entered
    case GET: 
       // do some stuff
       break;
    
    // if 'S' is entered
    case STORE:
        // do some stuff
        break;
    }
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Money problem
    By Tesnik in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 08:12 AM
  2. helppp in bank databse
    By neha007 in forum C++ Programming
    Replies: 1
    Last Post: 06-02-2007, 09:43 AM
  3. Replies: 12
    Last Post: 06-03-2005, 01:13 AM
  4. Money classs
    By blackjs in forum C++ Programming
    Replies: 5
    Last Post: 12-04-2001, 09:43 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21