5 Replies - 527 Views - Last Post: 16 November 2011 - 07:39 AM Rate Topic: -----

#1 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Having an error with std::string and operator+ used to concatenate

Posted 16 November 2011 - 06:20 AM

I'm having an error with line 49 in my program.

Quote

|49|error: invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'|

Here is line 49: string filePathName = "C:.......\\" + userInput[0] + "\\" + userInput[1] + ".txt";

Here is my whole code:
#include <cctype>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

void addWord(string userInput, string filePathName);
void findWord(string userInput);
bool isProper(string userInput);

int main(void)
{
	string userInput;
	cout << "Enter the name of the identity. It must only consist of letters and numbers." << endl;
	cin >> userInput;
	// The next 2 lines check if userInput is only alphanumeric.
	bool checksIfProper = isProper(userInput);
	if(!checksIfProper) return 0;
	/* cin.sync will not be enabled because the user may have a slow computer etc which may take
	 * time to process so they may want to include the next input operation with their word. */
	findWord(userInput);
	cout << "Press enter to exit . . ." << endl;
	cin.sync();
	cin.get();
	return 0;
}

bool isProper(string userInput)
{
    int iterator = 0;
    // This for will check if the string contains a non alphanumeric letter. If so, it returns false. Otherwise it returns true.
    for(; iterator < userInput.length()-1; ++iterator)
    {
        if(!isalnum(userInput[iterator]))
        {
            cout << "Your string contains a character that is non-alphanumeric." << endl;
            cout << "Press enter to exit . . ."                                  << endl;
            cin.sync();
            cin.get();
            return false;
        }
    }
    return true;
}

void findWord(string userInput)
{
    string filePathName = "C:......\\" + userInput[0] + "\\" + userInput[1] + ".txt";
	ifstream inFile;
	inFile.open(filePathName.c_str());
	if(inFile.fail())
	{
		cout << "There is an absence or misplacement of essential files!" << endl;
		return;
	}
	string wordInFile;
	do
	{
		inFile >> wordInFile;
	}
	while(wordInFile != userInput && !inFile.eof());
	inFile.close();
	if(wordInFile == userInput)
		cout << "The identity was found." << endl;
	else
	{
		cout << "The identity was not found. Would you like to add it to the database? (Y/N)" << endl;
		char wannaAdd;
		while(true)
		{
			cin >> wannaAdd;
			if(wannaAdd == 'y' || wannaAdd == 'Y' || wannaAdd == 'n' || wannaAdd == 'N')
				break;
			cout << "Unrecognized entry. Please try again." << endl;
		}
		if(wannaAdd == 'y' || wannaAdd == 'Y')
			addWord(userInput, filePathName);
	}
}

void addWord(string userInput, string filePathName)
{
	ifstream inFile;
	inFile.open(filePathName.c_str());
	if(inFile.fail())
	{
		cout << "There is a misplacement of essential files!" << endl;
		cout << "Did you modify the path of the database?" << endl;
		return;
	}
	ofstream outFile;
	outFile.open(filePathName.c_str(), ios::app);
	outFile << userInput << endl;
	outFile.close();
}


Thanks for reading this question.

This post has been edited by hulla: 16 November 2011 - 06:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Having an error with std::string and operator+ used to concatenate

#2 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: Having an error with std::string and operator+ used to concatenate

Posted 16 November 2011 - 06:37 AM

The + operator is only overloaded for strings if one of the operands is a std::string. Make the first string a std::string:
string filePathName = string("C:......\\") + userInput[0] + "\\" + userInput[1] + ".txt";


for(; iterator < userInput.length()-1; ++iterator)

Why -1?
Was This Post Helpful? 1
  • +
  • -

#3 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Having an error with std::string and operator+ used to concatenate

Posted 16 November 2011 - 06:57 AM

Oh I get it. This is called a cast, right?

Thanks for catching that mistake with the -1. :) Wouldn't have seen it coming.
Was This Post Helpful? 0
  • +
  • -

#4 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: Having an error with std::string and operator+ used to concatenate

Posted 16 November 2011 - 07:10 AM

Yes, it's a cast. It can also be written as:
(string) "C:......\\"

In this case it calls the string constructor, but you can also overload cast operators.
Was This Post Helpful? 0
  • +
  • -

#5 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Having an error with std::string and operator+ used to concatenate

Posted 16 November 2011 - 07:10 AM

No, in his example he is constructing a new string (passing a c-style one). After that the std::string operator+ overload is used.
Was This Post Helpful? 1
  • +
  • -

#6 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: Having an error with std::string and operator+ used to concatenate

Posted 16 November 2011 - 07:39 AM

View Postanonymouscodder, on 16 November 2011 - 04:10 PM, said:

No, in his example he is constructing a new string (passing a c-style one). After that the std::string operator+ overload is used.

That's what I said.

View PostHezekiah, on 16 November 2011 - 04:10 PM, said:

In this case it calls the string constructor, but you can also overload cast operators.

A constructor can be used for a cast.
#include <iostream>

class MyClass {
	public:
		MyClass(int num) {
			std::cout << "Constructor called, num = " << num << '\n';
		}
};

int main() {
	MyClass(1);
	(MyClass) 2;
	static_cast<MyClass>(3);
	return 0;
}


Output:
Constructor called, num = 1
Constructor called, num = 2
Constructor called, num = 3
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1