Cant use a variable name?

This is a discussion on Cant use a variable name? within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, another question on my banker program, here it is. Im trying to have a variable in my class ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    11

    Cant use a variable name?

    Hey everyone, another question on my banker program, here it is. Im trying to have a variable in my class that creates a file when the class is initiallized. But, when i open the file using a varialbe string instead of a static string..it doesnt work. Heres what im talking about.

    When i put this in, it compiles without any error..look in the bold


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    class Banker
    {
    public:
    	
    	const static int CHECKING = 1;
    	const static int SAVINGS = 0;
    
    
    	Banker(string name, int iDeposit);
    	 ~Banker();
    	int GetBalance(int account);
    	int Deposit(int amount, int account);
    	int Transfer(int amount, Banker reciever);
    protected:
    	int cBalance;
        int sBalance;
    	string bName;
    
        ofstream oAccount;
        ifstream rAccount;
    
    
    };
    
    Banker::Banker(string name, int iDeposit)
    {
         cBalance = iDeposit;
         bName = name;
         oAccount.open("Blah.txt", ios::app);
    
    
    
    }
    
    
    
    
    int main()
    {
    	cout<< "Welcome to the Cullen Familly Banking Program.\n";
    	cout<< "Please select a choice:\n\n";
    	cout<< "1. Deposit\n";
    	cout<< "2. Check Balance\n";
    	cout<< "3. Make a transfer\n\n";
        cin.get();
    	return 0;
    }

    Now, but when i make this simple adjustment i get errors...again look in the bold


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    class Banker
    {
    public:
    	
    	const static int CHECKING = 1;
    	const static int SAVINGS = 0;
    
    
    	Banker(string name, int iDeposit);
    	 ~Banker();
    	int GetBalance(int account);
    	int Deposit(int amount, int account);
    	int Transfer(int amount, Banker reciever);
    protected:
    	int cBalance;
        int sBalance;
    	string bName;
    
        ofstream oAccount;
        ifstream rAccount;
    
    
    };
    
    Banker::Banker(string name, int iDeposit)
    {
         cBalance = iDeposit;
         bName = name;
         oAccount.open(bName + ".txt", ios::app);
    
    
    
    }
    
    
    
    
    int main()
    {
    	cout<< "Welcome to the Cullen Familly Banking Program.\n";
    	cout<< "Please select a choice:\n\n";
    	cout<< "1. Deposit\n";
    	cout<< "2. Check Balance\n";
    	cout<< "3. Make a transfer\n\n";
        cin.get();
    	return 0;
    }
    Here are the errors that i get when trying to compile this...:

    c:\cpp stuff\banking\bankingmain.cpp: In method `Banker::Banker(basic_string<char,string_char_trai ts<char>,__default_alloc_template<false,0> >, int)':
    c:\cpp stuff\banking\bankingmain.cpp:36: no matching function for call to `ofstream:pen (basic_string<char,string_char_traits<char>,__defa ult_alloc_template<false,0> >, ios:pen_mode)'
    C:\DEV-C_~1\Include\G__~1\fstream.h:78: candidates are: void ofstream:pen(const char *, int = ios:ut, int = 436)



    Any help much appreciated..thanks, justin

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    oAccount.open((bName + ".txt").c_str(), ios::app);
    Open expects a C-style string, not a std::string.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    so should i convert it to a character array?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so should i convert it to a character array?
    No, you should use the code I gave you. It creates a new string with the suffix and then calls c_str() on that string, returning a C-style string that's acceptable to open.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    thank you, i didnt see the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 08:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 02:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 02:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM

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