I wrote this string palindrome program that reads input from a text file. All the syntax are correct but I have an error I couldn't understand. Any help is appreciated, thanks!!!

First, this is the error message:

Error 1 error LNK2001: unresolved external symbol "char * newToCheck"
(?newToCheck@@3PADA)

And this...

Error 2 error LNK1120: 1 unresolved externals

This is my code so far:

#include <iostream>
#include <fstream>

using namespace std;

const int strSize = 0xFF;
ifstream infile;
char checkPhrase[strSize], origPhrase[strSize], newToCheck[];
int cleanup (char*, int), newCount;
bool palindrome (char*, int, int);

int main()
{	
	infile.open ("C:\\Documents and Settings\\student\\Desktop\\PalindromeData.rbh");

	if (!infile)
	{
		cout << "File not found.  Check your directory and/or file and try again.\n\nEnter any key to exit..." << endl;
		cin.get();
		exit(1);
	}
	
	while (!infile.eof())
	{
		infile.getline(origPhrase, sizeof(checkPhrase));
		for (int i = 0; i < strSize; i++)
			checkPhrase[i] = origPhrase[i];
		newCount = cleanup (checkPhrase, strlen(checkPhrase));
                newToCheck [newCount];
		for (int n = 0; n < newCount; n++)
			newToCheck[n] = checkPhrase[n];
		if (palindrome (newToCheck, 0 , newCount - 1)) 
			cout << "The phrase " << origPhrase << " is a Palindrome\r\n.";
		else 
			cout << "The phrase " << origPhrase << " is not a Palindrome\r\n.";
	}

	infile.close();
	cin.get();
	return 0;
}

int cleanup (char* phrase, int size)
{
    int currentChar = 0;
    for(int i = 0; i < size; i++)
    {
        if(((int)phrase[i] >= 48 && (int)phrase[i] <= 57) || ((int)phrase[i] >= 65 && (int)phrase[i] <= 90) || ((int)phrase[i] >= 97 && (int)phrase[i] <= 122))
        {
              phrase[currentChar] = tolower(phrase[i]);
              currentChar++;
        }
    }
    return currentChar;
}

bool palindrome (char* phrase, int start, int end)
{
    if(phrase[start] == phrase[end])
    {
        if(start == end || start + 1 == end)
        {
            return true;                         
        }
        else
        {
            return palindrome(phrase, start + 1, end - 1);  
        }
    }
    else
    {
        return false;
    }
}

Dani AI

Generated

Short summary: the linker error means newToCheck was declared but no storage was actually provided. spotted the missing storage and fixed the immediate crash, but there are a few safer, cleaner approaches worth using so the program is robust and easier to maintain.

Quick fixes

  • Remove the meaningless statement newToCheck[newCount]; — it does nothing.
  • Allocate storage either with a fixed-size array or dynamically, and remember to add a terminating '\0'. Example options:
char newToCheck[strSize];   // allocate enough space (declare inside main if possible)
...
newToCheck[newCount] = '\0';

or

char *newToCheck = new char[newCount + 1];
...
newToCheck[newCount] = '\0';
delete[] newToCheck;

Better C++ style

  • Use std::string and std::getline to avoid manual buffer management and common bugs (off-by-one, missing null terminator, buffer overflow). Build a cleaned string using std::isalnum/std::tolower (cast to unsigned char when calling these). Example pattern:
std::string orig;
while (std::getline(infile, orig)) {
    std::string cleaned;
    for (unsigned char c : orig)
        if (std::isalnum(c)) cleaned.push_back(std::tolower(c));
    bool isPal = std::equal(cleaned.begin(),
                            cleaned.begin() + cleaned.size()/2,
                            cleaned.rbegin());
    // print result
}

Other practical tips

  • Avoid while (!infile.eof()); use while (std::getline(...)) or while (infile.getline(...)).
  • Make the palindrome check safer: return true when start >= end (covers both even/odd lengths) or use an iterative two-pointer loop to avoid deep recursion.
  • Include <cctype>, <cstring> / <string>, and <algorithm> as needed, and always watch character casts into std::is*/std::to* functions.

These changes fix the linker/runtime issue and make the program clearer and safer going forward.

Recommended Answers

All 2 Replies

I have no idea what newToCheck is for.

Line 8 defines it as a char pointer. No size so there's no storage.
Line 29 seems to serve no purpose at all.
Line 31 uses newToCheck[n] but the location has no storage space, so even if you tried to run the program you'll probably crash.

ahh thank you so much! I missed that part. I forgot storage on line 8!

Thanks again!

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.