Thread: Invalid conversion from char to char*

  1. #1
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52

    Invalid conversion from char to char*

    Hi there. I was making a program which emulates a bank account of sorts using classes.
    Code:
    void Account :: Start()
    {
         Account Temp;
         int i;
         for(i = 0; i < 3; i++)
         {
              Balance[i] = 0; 
              cout << "\nEnter Account Number for User " << (i+1) << " : ";
              cin >> ACNo[i];
              cout << "\nEnter Name of User " << (i+1) << " : ";
              cin.ignore();
              cin.getline(Name[i], 50);
              cout << "\nEnter Account Type(S = Savings, C = Current) for User " << (i+1) << " : ";
              cin >> ACType[i];
         }
    }
    In this section of the code, however, I get an error for trying to enter Name[i]which is the same as I have given in my post title. The specifications of the class are as follows -:
    Code:
    const int n = 50;
    
    class Account
    {
          private:
                  char Name[n];
                  int ACNo[n];
                  char ACType[n];
                  float Balance[n];
          
          public:
                 void Start();
                 void Deposit();
                 void Withdraw();
                 void Display();
    };
    I gather Name[i] is wrong anyway as i goes uptill only 3.
    Is there a way to get around these complications?
    Last edited by SVXX; 05-10-2009 at 03:03 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am curious why each Account has n account numbers, types and balances. It does make sense for the name of the account (or account holder?) to be a C-style string of up to n-1 characters, and this is directly related to your problem. This:
    Code:
    cin.getline(Name[i], 50);
    should be:
    Code:
    cin.getline(Name, 50);
    Or better yet, use a std::string instead of an array of char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Well I did try what you asked but the problem is, I'm storing an array of users and their bank account details. If I put Name instead of Name[i], then in the Display function it only shows a random character out of the entire Name string.
    My school's computer lab uses Turbo C++ 4.5, which is worse. I'm the only one who bothers to keep up to date with the new standards. There has to be a way to fit all this in for the old version, and that would mean not being able to use std::string, sadly.
    Without the n types, it gives me an invalid indirection/invalid types for int[int] subscript error.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SVXX
    Well I did try what you asked but the problem is, I'm storing an array of users and their bank account details. If I put Name instead of Name[i], then in the Display function it only shows a random character out of the entire Name string.
    Ah, but the solution is simple: instead of trying to cram the modeling of an account and a list of accounts into a single class, separate them into two classes.

    Quote Originally Posted by SVXX
    My school's computer lab uses Turbo C++ 4.5, which is worse. I'm the only one who bothers to keep up to date with the new standards. There has to be a way to fit all this in for the old version, and that would mean not being able to use std::string, sadly.
    Among the various publications of Bjarne Stroustrup, College of Engineering Chair in Computer Science Professor at Texas A&M University, designer and original implementer of C++, is a paper on Learning Standard C++ as a New Language. Maybe you could persuade the relevant school administration to review this paper and consider its implications.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Quote Originally Posted by laserlight View Post
    Ah, but the solution is simple: instead of trying to cram the modeling of an account and a list of accounts into a single class, separate them into two classes.
    Could you elaborate what you meant by this? The modeling of an account and the list of accounts..
    And even if I did persuade the administration to do so, it wouldn't help. They say the "level" of C++ I attempt is college level. School level is set by the Central Board, and they recommend Turbo C++ 4.5, so I can't help about that.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SVXX
    Could you elaborate what you meant by this? The modeling of an account and the list of accounts.
    Something like this:
    Code:
    class Account
    {
    public:
        enum {NameSize = 50};
    
        void Deposit();
        void Withdraw();
        void Display();
    private:
        char Name[NameSize];
        int ACNo;
        char ACType;
        float Balance;
    };
    
    class AccountList
    {
    public:
        enum {Size = 50};
    
        // ...
    private:
        Account Accounts[Size];
    };
    So, you have an Account class that models a single account, then you have an AccountList class that models a list of accounts. From the looks of it, Start() should not be a member function of either class since it is meant to populate an AccountList, and to do this it should use the public member functions of AccountList instead of being a member function itself.

    (Note that it may not be necessary or desirable to expose the sizes of the name and list of accounts.)

    Quote Originally Posted by SVXX
    And even if I did persuade the administration to do so, it wouldn't help. They say the "level" of C++ I attempt is college level. School level is set by the Central Board, and they recommend Turbo C++ 4.5, so I can't help about that.
    At Central Board level, I expect that even if you did manage to galvanise a change, it would only be applied starting sometime after your finish your course. In the meantime, you would at least have a large advantage over the rest of your cohort when it comes to applying for a job that requires skills in C++, since they would get laughed out of the interview room while you probably won't (unless you screw up when demonstrating your general programming skills, of course).
    Last edited by laserlight; 05-10-2009 at 03:51 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Indeed now..lol. Thanks for the encouragement in the interview part :P
    Start() is used to initialize the data members. Let's see, I'll try to apply your concept and let you know.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SVXX
    Start() is used to initialize the data members.
    It does more than that. Currently, your implementation of Start() prints to standard output to request for input, then reads from standard input to populate the member variables. Rather, you should define a constructor to initialise the member variables and provide member functions to enable say, the setting of member variables, then write your function to request for and read in user input and use that to populate the Account and AccountList objects via the member functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid conversion from (void *) to unsigned char
    By k0k33 in forum C Programming
    Replies: 1
    Last Post: 02-19-2009, 08:57 AM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. invalid conversion from 'const char*' to 'char'
    By howzer in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2006, 12:58 PM
  4. invalid conversion from 'char' to 'const char*'
    By LiKWiD in forum C++ Programming
    Replies: 10
    Last Post: 03-20-2005, 03:50 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM