Request for help with ARRAYS.

This is a discussion on Request for help with ARRAYS. within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm trying to make a program that checks to see if a sentence is a palindrome. This is my ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    Question Request for help with ARRAYS.

    Hello, I'm trying to make a program that checks to see if a sentence is a palindrome. This is my logic: (1) Get the sentence. (2) Strip out all whitespaces, punctuation, and convert all letters to lowercase. (3) Compare the first letter to the last, and so on. (4) Output true if true, etc.

    My code is pasted below, can anyone please tell me what I'm doing wrong? Thanks in advance for your help.

    Code:
    //
    //This program checks to see if a sentence is a palindrome.
    //
    
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    #include <cctype>
    #include <fstream>
    
    using namespace std;
    
    // Function prototypes.
    char getInfo(char[]);
    char getAlphaLetters(char[], int);
    bool compareString(char[], int);
    void output(char[], int);
    
    const int SIZE=100;			//Array size.
    
    int main()
    {
    
    //Declare variables.
    
    
    	char sentence[SIZE];		//Word user enters.
    	char endSentence[SIZE];		//Stripped Sentence.
    	int numberAlpha;			//Number of alpha char in the sentence.
    
    	// Call Get Sentence function.
    	getInfo(sentence);
    	
    	// Call Make a New Array w/ Alpha characters function..
    	char getAlphaLetters(char sentence[], int numberAlpha);
    
    	// Call test for palindrome.
    	bool compareString(char endSentence[], int numberAlpha);
    
    	// Output dependant on the test.
    	void output(char endSentence[], int numberAlpha);
    
    	
    	return 0;
    }
    
    //**************************************************
    // Definitions of the functions used the program.
    //**************************************************
    
    char getInfo(char sentence[])
    // This function gets the info (the word that will be checked
    // to see if its a palindrome.
    {
    	const int SIZE=100;
    
    	cout << "Please enter a word: ";
    
    	cin.getline(sentence, SIZE);
    	//Now that's stored in sentence.
    	
    	return sentence[SIZE];
    }
    
    char getAlphaLetters(char sentence[], int numberAlpha)
    // This function creates a new array w/o the junk.
    {	
    
    	char newArray[SIZE];
    
    	for (int count=0; count < SIZE; count++)
    	{
    		if (isalpha(sentence[count]))
    			//Only perform this loop if alpha (no ws or !, etc.)
    			{
    			newArray[numberAlpha] = tolower(sentence[count]);
    			numberAlpha++;
    			}
    		else if(sentence[count] == 0)
    			break;
    	}
    	numberAlpha--;
    	endSentence[] = newArray[];
    	return endSentence;
    }
    
    bool compareString(char endSentence[], int numberAlpha)
    // This functions compares the letters and then tells user
    // whether the sentence is a palindrome.
    {
    	
    	int count;
    	
    	//Compare the elements against each other. First v. last, etc.
    	for (count = 0; count < numberAlpha; count++)
    	{
    		if(endSentence[count] == endSentence[numberAlpha])
    			numberAlpha--;
    		else 
    			return false;
    	}
    
    	return true;
    
    	numberAlpha = getAlphaLetters(sentence[SIZE], numberAlpha);
    	
    }
    
    void output(char endSentence[], int numberAlpha)
    //Return true, it's a palindrome, and so on.
    {
    
    	if ((compareString(endSentence, numberAlpha)) == true)
    		{
    		cout << "The sentence you entered " << sentence << " is a palindrome." << endl;
    		}
    	else
    		{
    		cout << "The sentence you entered " << sentence << " is not a palindrome." << endl;
    		}
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> My code is pasted below, can anyone please tell me what I'm doing wrong?
    What makes you think something is wrong? Do you get compiler errors? What are they? Does it compile but fail to run properly? Does it give incorrect output? What input do you give, what output do you get, and what output do you expect? All of that kind of information makes it easier for people to glance at the code and identify issues.

    The first thing I see is that numberAlpha is never initialized. You then pass it into a function and use it without setting its value.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You should think about using an STL string container instead of the C-style character arrays.



    Code:
    char getInfo(char sentence[])
    // This function gets the info (the word that will be checked
    // to see if its a palindrome.
    {
    	const int SIZE=100;
    
    	cout << "Please enter a word: ";
    
    	cin.getline(sentence, SIZE);
    	//Now that's stored in sentence.
    	
    	return sentence[SIZE];
    }
    Your not doing anything with the return value in main so it doesn't really make sense to return anything at all, plus, you are returning a single character that is outside the range of valid indexes. A 100 character array has valid indexes from 0 through 99, sentence[SIZE] (sentence[100]) is outside this range.


    Code:
    // Call Make a New Array w/ Alpha characters function..
    char getAlphaLetters(char sentence[], int numberAlpha);
    
    // Call test for palindrome.
    bool compareString(char endSentence[], int numberAlpha);
    
    // Output dependant on the test.
    void output(char endSentence[], int numberAlpha);
    That's not how you call functions from your main function. The return types and argument types should not be a part of the function call.



    Code:
    char getAlphaLetters(char sentence[], int numberAlpha)
    // This function creates a new array w/o the junk.
    {	
    
    	char newArray[SIZE];
    
    	for (int count=0; count < SIZE; count++)
    	{
    		if (isalpha(sentence[count]))
    			//Only perform this loop if alpha (no ws or !, etc.)
    			{
    			newArray[numberAlpha] = tolower(sentence[count]);
    			numberAlpha++;
    			}
    		else if(sentence[count] == 0)
    			break;
    	}
    	numberAlpha--;
    	endSentence[] = newArray[];
    	return endSentence;
    }
    You have a problem with return types, the function is stated to return a single character but you are trying to return a char pointer. The line in green is not going to copy the data from newArray to endSentence like I think you intend it to. Also, endSentence is not a global variable, it has only been declared and therefore only has scope within the main function. You should probably be using it as an argument to the function. You are also not initializing your numberAlpha variable.



    Code:
    bool compareString(char endSentence[], int numberAlpha)
    // This functions compares the letters and then tells user
    // whether the sentence is a palindrome.
    {
    	
    	int count;
    	
    	//Compare the elements against each other. First v. last, etc.
    	for (count = 0; count < numberAlpha; count++)
    	{
    		if(endSentence[count] == endSentence[numberAlpha])
    			numberAlpha--;
    		else 
    			return false;
    	}
    
    	return true;
    
    	numberAlpha = getAlphaLetters(sentence[SIZE], numberAlpha);
    	
    }
    The line in red above will never be executed since it occurs after the return statement.



    Try to use a string container, it will be much easier.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    2

    Question Well, I'd really like to figure out how to do this with arrays.

    First, let me post my revised code, then I'll describe my errors and questions.

    FULL CODE

    Code:
    //
    //This program checks to see if a sentence is a palindrome.
    //
    //
    
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    #include <cctype>
    #include <fstream>
    
    using namespace std;
    
    // Function prototypes.
    char getInfo();
    char getAlphaLetters(char[], int&);
    bool compareString(char[], int&);
    void output(char[], int&);
    
    const int SIZE=100;			//Array size.
    
    int main()
    {
    
    //Declare variables.
    
    
    	char sentence[SIZE];		//Word user enters.
    	char newArray[];		//Stripped Sentence.
    	int numberAlpha=0;			//Number of alpha char in the sentence.
    
    	// Call Get Sentence function.
    	sentence[SIZE] = getInfo();
    	
    	// Call Make a New Array w/ Alpha characters function..
    	newArray = getAlphaLetters(sentence, numberAlpha);
    
    	// Call test for palindrome.
    	bool compareString(newArray, numberAlpha);
    
    	// Output dependant on the test.
    	void output(newArray, numberAlpha);
    
    	
    	return 0;
    }
    
    //**************************************************
    // Definitions of the functions used the program.
    //**************************************************
    
    char getInfo(char sentence[])
    // This function gets the info (the word that will be checked
    // to see if its a palindrome.
    {
    	const int SIZE=100;
    
    	cout << "Please enter a word: ";
    
    	cin.getline(sentence, SIZE);
    	//Now that's stored in sentence.
    	
    	return sentence[SIZE];
    }
    
    char getAlphaLetters(char sentence[], int& numberAlpha)
    // This function creates a new array w/o the junk.
    {	
    
    	
    
    	char newArray[];
    
    	for (int count=0; count < SIZE; count++)
    	{
    		if (isalpha(sentence[count]))
    			//Only perform this loop if alpha (no ws or !, etc.)
    			{
    			newArray[count] = tolower(sentence[count]);
    			numberAlpha++;
    			}
    		else if(sentence[count] == 0)
    			break;
    	}
    	numberAlpha--;
    	return newArray[numberAlpha];
    }
    
    bool compareString(char newArray[numberAlpha], int& numberAlpha)
    // This functions compares the letters and then tells user
    // whether the sentence is a palindrome.
    {
    	
    	int count;
    	
    	//Compare the elements against each other. First v. last, etc.
    	for (count = 0; count < numberAlpha; count++)
    	{
    		if(newArray[count] == newArray[numberAlpha])
    			numberAlpha--;
    		else 
    			return false;
    	}
    
    	return true;
    
    }
    
    void output(char newArray[], int& numberAlpha)
    //Return true, it's a palindrome, and so on.
    {
    
    	if ((compareString(newArray, numberAlpha)) == true)
    		{
    		cout << "The sentence you entered " << sentence << " is a palindrome." << endl;
    		}
    	else
    		{
    		cout << "The sentence you entered " << sentence << " is not a palindrome." << endl;
    		}
    }

    Here are the sorts of error messages I'm receiving:

    RE:

    Code:
    	char sentence[SIZE];		//Word user enters.
    	char newArray[];		//Stripped Sentence.
    	int numberAlpha=0;			//Number of alpha char in the sentence.
    Compiler says that newArray has an unknown size. And that's right...I want it's size to be defined within the getAlpha function, and the size will be the variable numberAlpha. Why is this a problem?

    RE:

    Code:
    	// Call Make a New Array w/ Alpha characters function..
    	newArray = getAlphaLetters(sentence, numberAlpha);
    Says: "Can't convert from 'char' to 'char[]'. I don't get it.

    RE:


    Code:
    bool compareString(char newArray[numberAlpha], int& numberAlpha)
    // This functions compares the letters and then tells user
    // whether the sentence is a palindrome.
    Compiter says that 'numberAlpha' has an undeclared identifier? and that a constant expression is expected??

    I think I've been trying to figure this out for so long that I'm overlooking some pretty obvious stuff. Thank you so much for your time helping me!

    Full list of compiler errors, if you're interested:

    Code:
    --------------------Configuration: pl6 - Win32 Debug--------------------
    Compiling...
    pl6.cpp
    (40) : error C2133: 'newArray' : unknown size
    (47) : error C2440: '=' : cannot convert from 'char' to 'char []'
            There are no conversions to array types, although there are conversions to references or pointers to arrays
    (50) : error C2078: too many initializers
    (50) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    (53) : error C2182: 'output' : illegal use of type 'void'
    (53) : error C2078: too many initializers
    (83) : error C2133: 'newArray' : unknown size
    100) : error C2065: 'numberAlpha' : undeclared identifier
    (100) : error C2057: expected constant expression
    (100) : error C2466: cannot allocate an array of constant size 0
    (126) : error C2065: 'sentence' : undeclared identifier
    Error executing cl.exe.
    
    pl6.obj - 10 error(s), 1 warning(s)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> Compiler says that newArray has an unknown size. And that's right...I want it's size to be defined within the getAlpha function.
    C++ does not allow variable length arrays to be created like that. They must have a compile time size. The general solution is to use a vector or string. You can also create a dynamic array with new[]/delete[], although I'd only use that if you were in a class that doesn't allow standard library containers.

    >> Says: "Can't convert from 'char' to 'char[]'. I don't get it.
    getAlphaLetters returns a char, but newArray is a char array. You cannot return arrays from functions like that. You can return vectors and strings, so if you make newArray a vector or string you can return that. You can also make it a parameter that gets updated in the code (like numberAlpha).

    >> Compiter says that 'numberAlpha' has an undeclared identifier? and that a constant expression is expected??
    You can't use the second parameter as part of the first parameter. You also need a constant size (like the first error) if you want to specify one. You'll have to switch to a container anyway, so this will change then.

    If you don't want to or cannot switch to a container or dynamic array, then you'll have to make a large statically sized array that is big enough to handle your largest possible strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. returning char arrays!!!!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 06:05 AM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21