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)