954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Error convert string to const char*

Hello everyone,

I'm having a problem with an assignment that I'm not sure how to fix I'm getting the following error:

accountdb.cpp:55: error: cannot convert std::string to const char* for argument 1 to int strcmp(const char*, const char*)

Here is my header file:

#ifndef ACCOUNTDB_H
#define ACCOUNTDB_H
#include "account.h"
#include <string>

class AccountDB
        {
                private:
                        Account AccountArray[30];
                        int ObjectsStored;
                public:
                        AccountDB();
                        AccountDB(const char*);
                        void print () const;
                        void sortAccounts();
        };

#endif


And here is where the error is comming from (accountdb.cpp):

#include "account.h"
#include "accountdb.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <cstring>
#include <fstream>

using namespace std;

AccountDB::AccountDB()
        {
                ObjectsStored = 0;
        }

AccountDB::AccountDB(const char* accountdb)
        {
                ifstream inFile;
                inFile.open(accountdb, ios::binary);
                if (!inFile)
                {
                        cout << "Unable to open file accountdb" << endl;
                        exit(-1);
                }
                inFile.read((char*) this, sizeof(AccountDB));
                inFile.close();

                sortAccounts();
        }

void AccountDB::print() const
        {

        cout << "Account Database Listing" << endl << endl;

        for (int i = 0; i < ObjectsStored; i++)
        {
        AccountArray[i].print();
        cout << endl;
        }
        }

void AccountDB::sortAccounts()
        {
        int i, j;
        Account bucket;

        for (i = 1; i < ObjectsStored; i++)
                {
                bucket = AccountArray[i];


                for (j = i; (j > 0) && (strcmp(AccountArray[j - i].getAccountNumber(), bucket.getAccountNumber() )> 0); j --)
                        AccountArray[j] = AccountArray[j - 1];
                        AccountArray[j] = bucket;
                }
        }


The sortAccounts method should sort ACCOUNTS in ascending order. I'm getting an error and I'm not sure how to fix and I've been stuck for a while now. Anyone know how I can fix it? I tried using c_str but I probably used it wrong and it ended up giving me more errors. Any way to do it while using strcmp?

Thank you for your help.

randrum1707
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Did you try

strcmp(AccountArray[j - i].getAccountNumber().c_str(), bucket.getAccountNumber().c_str() )

This should work but you could compare strings directly.

histrungalot
Posting Whiz in Training
266 posts since May 2008
Reputation Points: 76
Solved Threads: 34
 

Works, thanks a ton!

randrum1707
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You