Hello.

I have been working on this program for some time, and it appears that I now have only one error (1 error 4 times).

I will greatly appreciate it if someone could show me how to fix this error.

Here are my errors: (phonebook.cpp is at the very bottom of this post) Thank you!

1>------ Build started: Project: Name, Configuration: Debug Win32 ------
1>Compiling...
1>PhoneNumber.cpp
1>Name.cpp
1>PhoneBook.cpp
1>c:\documents and settings\William\my documents\_name\phonebook.cpp(39) : error C2664: 'strcmp' : cannot convert parameter 1 from 'std::string' to 'const char *'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\documents and settings\William\my documents\_name\phonebook.cpp(48) : error C2664: 'strcmp' : cannot convert parameter 1 from 'std::string' to 'const char *'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\documents and settings\William\my documents\_name\phonebook.cpp(67) : error C2664: 'strcmp' : cannot convert parameter 1 from 'const std::string' to 'const char *'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\documents and settings\William\my documents\_name\phonebook.cpp(73) : error C2664: 'strcmp' : cannot convert parameter 1 from 'const std::string' to 'const char *'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>PhoneBookProgram.cpp
1>Generating Code...
1>Build log was saved at "file://c:\Documents and Settings\William\My Documents\Visual Studio 2008\Projects\Name\Name\Debug\BuildLog.htm"
1>Name - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Here are some pieces to this program (I have left out my exception header files).

// Name.h

#ifndef NAME_H
#define NAME_H

#include <iostream>
using std::ostream;
using std::istream;

#include <string>
using std::string;

class Name
{
    friend class PhoneBook;
    friend ostream &operator<<( ostream &, const Name & );
    friend istream &operator>>( istream &, Name & );
public:
    Name();
    ~Name();    
private:
    char *dataName;
};

#endif
// Name.cpp

#include <iostream>
using namespace std;

#include <iomanip>
using std::setw;

#include <string>
using std::string;

#include "Name.h"

#include "InvalidNameLengthException.h"

Name::Name()
{
    dataName = new char[ 25 ] = "~";
}

Name::~Name()
{
    delete [] dataName;
}

ostream &operator<<( ostream &output, const Name &name )
{
    output << left << setw( 25 ) << name.dataName;
    return output;
}

istream &operator>>( istream &input, Name &name )
{
    string noWsName;
    input >> ws;
    getline(input, noWsName);
    
    if ( noWsName.size() > 1 && noWsName.size() <= 25 )
    {
        name.dataName = &noWsName[ 0 ];
    }
    else
    {
        throw InvalidNameLengthException();
    }
    return input;
}
// PhoneNumber.h

#ifndef PHONENUMBER_H
#define PHONENUMBER_H

#include <iostream>
using std::ostream;
using std::istream;

class PhoneNumber
{
    friend class PhoneBook;
    friend ostream &operator<<( ostream &, const PhoneNumber & );
    friend istream &operator>>( istream &, PhoneNumber & );
public:
    PhoneNumber();
    ~PhoneNumber();   
private:
    char *dataNumber;
};

#endif
// PhoneNumber.cpp

#include <iostream>
using namespace std;

#include <iomanip>
using std::setw;

#include <string>
using std::string;

#include "PhoneNumber.h"

#include "InvalidNumberException.h"

PhoneNumber::PhoneNumber()
{
    dataNumber = new char[ 25 ];
}

PhoneNumber::~PhoneNumber()
{
    delete [] dataNumber;
}

ostream &operator<<( ostream &output, const PhoneNumber &number )
{
    output << right << setw( 40 ) << number.dataNumber;
    return output;
}

istream &operator>>( istream &input, PhoneNumber &number )
{
    string noWsNumber;
    input >> ws;
    getline(input, noWsNumber);
    
    if ( noWsNumber.size() == 12 )
    {
        for( int i = 0; i < 3; i++ )
        {
            if( isdigit( noWsNumber[ i ] ) )
            {}
            else
            {
                throw InvalidNumberException();
            }
        }
        
        if( noWsNumber[ 3 ] == '-' )
        {;}
        else
        {
            throw InvalidNumberException();
        }
        
        for( int i = 4; i > 3 && i < 7; i++ )
        {
            if( isdigit( noWsNumber[ i ] ) )
            {}
            else
            {
                throw InvalidNumberException();
            }
        }
        
        if( noWsNumber[ 7 ] == '-' )
        {}
        else
        {
            throw InvalidNumberException();
        }
        
        for( int i = 8; i > 7 && i < 11; i++ )
        {
            if( isdigit( noWsNumber[ i ] ) )
            {}
            else
            {
                throw InvalidNumberException();
            }
        }
        
        if( isdigit( noWsNumber[ 11 ] ) )
        {
            number.dataNumber = &noWsNumber[ 0 ];
        }
        else
        {
            throw InvalidNumberException();
        }
    }
    else
    {
        throw InvalidNumberException();
    }
    return input;
}
// PhoneBook.h

#ifndef PHONEBOOK_H
#define PHONEBOOK_H

#include <iostream>
using std::ostream;

#include<string>
using std::string;

#include "Name.h"
#include "PhoneNumber.h"

class PhoneBook
{
    friend ostream &operator<<( ostream &, const PhoneBook & );
public:
    PhoneBook();
    ~PhoneBook();
    void add( const Name&, const PhoneNumber& );
    const string& find( const Name& ) const;
private:
    string aNames[ 1000 ];
    string aNumbers[ 1000 ];
};

#endif
// PhoneBook.cpp

#include <iostream>
using namespace std;

#include<string>
using std::string;

#include<cstring>
using std::strcmp;

#include "PhoneBook.h"
#include "Name.h"
#include "PhoneNumber.h"

#include "NameNotFoundException.h"
#include "NameAlreadyAddedException.h"
#include "FullException.h"

PhoneBook::PhoneBook()
{
    for( int i = 0; i < 1000; i++ )
    {
        ;
    }
}

PhoneBook::~PhoneBook()
{
    ;
}

void PhoneBook::add( const Name &name, const PhoneNumber &number )
{
    string nameHolder = name.dataName;
    string numberHolder = number.dataNumber;
    string nineNineNine = "~";

    if( strcmp( aNames[ 999 ], nineNineNine ) == 0 )
    {}
    else
    {
        throw FullException();
    }
    
    for( int i = 0; i < 1000; i++ )
    {
        if( strcmp( aNames[ i ], nameHolder ) == 0 )
        {
            throw NameAlreadyAddedException();
        }
        if( aNumbers[ i ] == "~" )
        {
            aNames[ i ] = nameHolder;
            aNumbers[ i ] = numberHolder;
            break;
        }
    }    
}

const string& PhoneBook::find( const Name &name ) const
{
    string nameHolder = name.dataName;
    
    for( int i = 0; i < 999; i++ )
    {
        if( strcmp( aNames[ i ], nameHolder ) == 0 )
        {
            return aNumbers[ i ];
        }
    }
    
    if( strcmp( aNames[ 999 ], nameHolder ) == 0 )
    {
        return aNumbers[ 999 ];
    }
    else
    {
        throw NameNotFoundException();
    }
}

ostream &operator<<( ostream &output, const Name &phoneBook )
{
    output << "THIS IS A TEST: OHHHHH YEAH!!1!" << endl;
    return output;
}
// Filename: PhoneBookProgram.cpp

#include <iostream>
using namespace std;

#include "PhoneBook.h"

#include "NameNotFoundException.h"
#include "InvalidNameLengthException.h"
#include "InvalidNumberException.h"
#include "NameAlreadyAddedException.h"
#include "FullException.h"

int main()
{
    PhoneBook phoneBook;

    bool quit = false;    // Flag to stop looping
    while (! quit)
    {
        // Output phone book menu
        cout << endl
            << "Phone Book Menu\n"
            << "1: add a listing\n"
            << "2: find a listing\n"
            << "3: print phone book\n"
            << "q: quit\n\n"
            << "Choice: ";

        // Input text typed by user
        string choice;
        cin >> ws;            // skip initial whitespace
        getline(cin, choice); // input user text

        Name name;
        PhoneNumber number;

        if (choice == "1")       // Add a listing
        {
            cout << "Add a Listing\n";
            cout << "Enter listing name: ";
            
            try
            {
                cin >> name;
                cout << "Enter listing phone number: ";
            }
            
            catch ( InvalidNameLengthException &invalidNameLengthException )
            {
                cout << invalidNameLengthException.what() << endl;
                break;
            }
            
            try
            {
                cin >> number;
            }
            
            catch ( InvalidNumberException &invalidNumberException )
            {
                cout << invalidNumberException.what() << endl;
                break;
            }
            
            try
            {
                phoneBook.add(name, number);
            }
            
            catch ( NameAlreadyAddedException &nameAlreadyAddedException )
            {
                cout << nameAlreadyAddedException.what() << endl;
                break;
            }
        }

        else if (choice == "2")  // Find a listing
        {
            cout << "Find a Listing\n";
            cout << "Enter name to find: ";
            
            try
            {
                cin >> name;
            }
            
            catch ( InvalidNameLengthException &invalidNameLengthException )
            {
                cout << invalidNameLengthException.what() << endl;
                break;
            }
            
            try
            {
                cout << phoneBook.find(name) << endl;
            }
            
            catch ( NameNotFoundException &nameNotFoundException )
            {
                cout << nameNotFoundException.what() << endl;
                break;
            }
        }

        else if (choice == "3")  // Print phone book
        {
            cout << "Print Phone Book\n";
            cout << phoneBook;
        }

        else if (choice == "q")  // Quit
        {
            cout << "Good-bye" << endl;
            quit = true;
        }

        else                     // Invalid user input
        {
            cerr << "Invalid choice: " << choice << endl;
        }
    }
    
    system("puase");
    return 0;
}

Recommended Answers

All 6 Replies

You can't use a string object with strcmp() . 2 solutions:
1) use the compare method that's part of the string object
2) convert the string object to char* with stringname.c_str()

Thanks for the advice WaltP.
Could you please show me how to do these 2 options?
Perhaps you could show me how to correct one error and I could apply it myself to all the errors.

If you are going to use std::string then you have to learn to read all about what that class can do instead of being spoon-fed on some forum. google for std::string and you will find a list that tells you how to use it.

I figured it out without google using WaltP's advice.

Dragon, you have a condescending tongue that betrays you.

>>Dragon, you have a condescending tongue that betrays you.

Tough love isn't always pleasant. Do your homework -- study the subjects and you will learn a great deal more than what you get here. What you got from me ain't nothing. Go to ProgrammingForums.org if you want to get a tongue lashing.

two parameters of strcmp type must be char * ,not string

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.