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.

Recommended Answers

All 2 Replies

Did you try

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

This should work but you could compare strings directly.

Works, thanks a ton!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.