problem with one line

hi,
Iam using a Dev-c++4 compiler and i am stuck on one line of a program to show the word of a sentence e.g. the last word is Ireland should show up on screen when program is run
ANSI C++ forbids comparison between pointer and integer

This message shows up on my compiler i have tryed eveything to get rid of it.

It applys to this line of code : for(i =(intlength -1); Array != ' '; i--)
,
It should be Array[i] or something similar.

But imo. You should use a string and it's built in functons of .find_last_of(' '); and .substr() to extract the last word. Much safer.
hi,
Still cant compile the program
here it is in full :

#include <iostream>
#include <string>
using namespace std;


int main()

{

string strSentence;
cout <<"\nInput a sentence: ";
getline(cin,strSentence);
int intlength,i;
intlength = strSentence.length();
char Array[intlength];
char LastWord[intlength];
for(i =(intlength -1); Array != ' '; i--)
LastWord = Array;
cout << "\n The last word";



cout << "\n\t The length of this string:"<<strSentence.length();


//Display the string inputted by the user

cout << endl;


system("PAUSE");
return 0;
}//end main
What are you doing with Array[intlength]? Your sentence is stored in strSentence. You shouldn't mix up strings and char-arrays.

Either you have a char array containing the string, and you use the for-loop to find the position of the last whitespace.
for(i=intlength-1;Array[i]!=' ';i--){}

(Do not forget to add the braces, otherwise the command LastWord = Array will be executed in every loop)

Then you can have LastWord (with should be a pointer to char, not an array) point to the position i in the Array.
LastWord = Array[i]

And then you can cout LastWord.

Or you are using strings. With the sentence stored in strSentence you can find the location of the last whitespace by
int position = strSentence.find_last_of(' ')

And you can extract the last string with
string lastword = strSentence.substr(position)
Topic archived. No new replies allowed.