C++/analyzing string backwards & result
Help me to understand how to get the program to analyze my input string backwards and forwards and tell me if it matches/don't match.
Borland Compiler, every time try a different way keep getting error messages, Below is the code in qeustion.
If need more, can send whole program.
for(i=0; i>=phrase.length(); i++);//reverses string
cout<<reverse<<"is a palendrome"<<endl;
cout <<phrase<<"is not a palendrome" <<endl;
Warning W8012 AComparing signed and unsigned values in function main()
Error E2096 .cpp 22: Illegal structure operation in function main()
Warning W8012 Comparing signed and unsigned values in function main()
Error E2235 : Member function must be called or its address taken in function main()
Error E2062 : Invalid indirection in function main()
Error E2188 28: Expression syntax in function main()
Error E2377 : If statement missing ) in function main()
Error E2054 Misplaced else in function main()
Error E2040 A:\palindrome\palindrome.cpp 37: Declaration terminated incorrectly
Ok, first let us review what the two types of messages are.
First you have errors. These prevent your program from compiling and are due to badly formed C++. They must be fixed.
Second you have warnings. These do not prevent your program from compiling - rather than badly formed code they are about things your code is doing, which is allowed, but are likely to cause problems. They should be looked into as they are often symptoms of mistakes and misunderstanding on the programmer's part (although not always). Many professional development departments, teams, whatever, have a rule that all code must not only compile but compile without any warnings.
So let us go through the errors and warnings:
WARNING: Comparing signed and unsigned values in function main()
Well integer types come in two types: signed, which support zero, positive and negative values and unsigned which only support zero and positive values. Although mixing signed and unsigned values is not always a problem it can be if negative values are compared with large unsigned values. Note that the range of positive values for a signed 2s complement integer is about half that of an unsigned integer of the same bit length. However for 32-bit integers these values are around 2 and 4 billion respectively.
My guess is that the string length() member function returns an unsigned value - because length can never meaningfully be negative and your loop counter, i, is a signed integer - such as int. Try making the type of i the same as the type returned from the string length member function. Possibly this is unsigned int, but as I do not know what type your string class is - it could be the std::string class as this class has a length member function and it returns an unsigned integral value. If it is some other string class then try looking in the class definition in the header file for the string class you are using for phrase and locate the declaration or definition of the string class's length member function to find out what it returns. Of course you could look in any documentation supplied for the class's you are using...
ERROR: 22: Illegal structure operation in function main()
Now this error has a number (other than the error number) associated with it - 22. This I would guess is the line number on which the error occurred as such values in error messages usually are line numbers. However you did not include the line numbers with the code you provided so I will have to guess which line the error occurred on and my first guess is the if line after the for line, and the use of the string type member function length as a data value: both an array and a scalar:
phrase.length[i] implies phrase.length is an array or a type that support operator. I think you need to remove the .length part and refer to phrase[i] - it is the phrase that is a string of many characters not its length.
[phrase.length-i-1] is meaningless and uses the length member function as a scalar data member. The  operator needs to be applied to something. Again I think you mean phrase[...]. and the character you are comparing with would be the nth from the end - where the end character is indeed phrase.length()-1 (note the use of the function call operator - the () - on the end of length). So you probably meant phrase[phrase.length()-1 - i].
Member function must be called or its address taken in function main()
Again this would seem to point to the bad use of phrase.length. phrase.length is a member function so either you call it:
or you can use its address and that's all you can do with it. Do not confuse the value returned from a function call to the function itself.
ERROR: Invalid indirection in function main()
Invalid indirection occurs when you have an incompatible mix of levels of indirection - i.e. pointers. So using *pointer (the dereferenced pointer so it evaluates to the thing the pointer is pointing to) when you need a pointer or using a pointer when you require a pointer to a pointer are likely to cause this error.
I would imagine this may be caused by the incorrect use of . The term pointer[i] is equivalent to *(pointer + i). You can use array in place of pointer - as a built in array type decays to a pointer (meaning an array maybe used as a pointer in many cases).
28: Expression syntax in function main()
If statement missing ) in function main()
Misplaced else in function main()
37: Declaration terminated Incorrectly
I think you have not supplied the code that goes with these errors. I count lines up to 27 if we assume that the previously discussed if line is line 22. The first refers to line 28, and the last to line 37. Start by looking at these lines of your code.
The first one may indicate that you mistyped an expression - examine the expression closely to see what might be wrong. If possible you can try breaking the expression up into simpler stages over several lines, using temporary variables to hold the intermediate results is required.
The second is a similar case - the if statement seems to be missing its closing bracket so you messed up the number (and maybe placement) of the opening and closing brackets - count the number of mismatched brackets by going along the line and adding one for each '(' you see and subtracting one for each ')' you see. If the result is not zero then you have mismatched brackets - a negative value indicates too many ')' and a positive value too many '('.
The misplacement of the else may be a knock on effect from the badly formed if statement. The compiler probably rejected the if totally and therefore was confused to find an else. Fix the previous problem and see if this error also goes away. If not then look closer at the code.
The error for line 37 indicates that possibly you were either declaring / defining something such as a variable which you did not do correctly or you did something the compiler thought was a declaration - for example compilers will parse certain variable declarations as a function declaration (a.k.a. a function prototype) - as this is what it looks like and this is what the rules say. They then get all confused when it is not what they thought.
Now a point about errors and warnings: They usually have code line number associated with them so you can easily locate the line the compiler has a problem with. Sometimes this line number is in fact a line that is beyond where the error occurred. This happens because the compiler reads the code one bit at a time and the error on a previous line may not cause problems until a following line - so you get the later line mentioned in the error message. A common cause of such a problem is a missing semi-colon statement terminator - not a problem on the line it is missing on but becomes a problem on the next line - or, if you are unlucky, a line following that - as the compiler assumes the previous statement is continuing when in fact it is a new statement altogether.
Now if you are using an integrated development environment (IDE) then it should have easy ways to show you the errors and take you to the files and file lines they occurred on in the editor - using such things as double clicking the error message line(s) with a mouse, toolbar buttons, menu options or hot keys.
If you are doing things by hand using a text editor and then compiling from a command line check to see if your editor has any of the following (you might like to check this for IDE editors as well):
- shows you what line number you are on in a status area
- can go to a line number using an editor command of some sort
- has a mode which shows you the line numbers down say the left hand side of the editor as you edit files.
Any of which are going to be useful in locating erroneous code by line number.
Although the compiler generated errors can point you to the code that it does not like, once all the code has been compiled it must be linked together - with itself and with any additional libraries. Errors from the linking phase of a build are less helpful as they the linker has no or little knowledge of the source code. Such errors are usually about unresolved symbols - whose names may be displayed decorated for C++ names so often have only a little resemblance to anything in your code (usually characters are added to a name to make it unique - so the name as it appears in your code will be in there somewhere!). The cause for these errors is usually that you forgot to include one or more items to be linked together - either an object file for one of your source code files or some library, or you forgot to define a function (including class member functions) or external global data item. The other type of common linker error is the reverse of an unresolved symbol - it is a symbol that is defined more than once. This means that you have managed to created more than one definition for a function or data item - this can be done, for example, by accidentally defining things in a header file which is included in more than one source file - each of which produces an object file containing a definition for the symbol in question.
Do not confuse defining something with declaring it. A definition reserves storage somewhere for the item defined. A declaration merely states that such a thing exists (i.e. has a definition) somewhere. An item can be declared to exist as many times as you like (so long as all declarations are the same and match the definition). However an item can only be defined once - the one definition rule or ODR) within the scope of the item. So local variables called j can be defined many times - even in the same function definition - so long as the are in different scopes or namespaces:
int j(1); // second definition of j ok.
// Hides j defined in outer scope
int j(2); // !BAD! - s second j defined in same scope
// the as first
Note that names in an inner scope or namespace hide those in an outer scope. With a namespace however you can always qualify that name to access the one you mean, using ::name to specify a name in the unnamed outermost global namespace.
Note that mostly a definition is also a declaration in C and C++ - so in general a definition not only defines the existence of something it also declares that it exists.
A function definition is the one where you define the body of the function as in function above. However a function declaration which as mention previously is also called a function prototype looks as follows:
Note that the same rules do not apply to types - e.g. classes. There you will find that the compiler needs the definition of a class more often than not every time an object of that type is defined.