what is wrong with my program.

hi guys, ive written this program to count: nonblank characters,number of lines, length of longest line and number of times longest line occurs.. from a sentence/sentences i enter into it.It runs with no compilation errors but i get the wrong answers for longest line and number of times longset line occurs.It looks like this:

string sentence;
int count;
int spacecount;
int currentll = sentence.length(); //current line length
int maxline; //max line length
int lineap; //longest line appearance

while (getline (cin, sentence))
{
cout<<"you have entered["<<sentence<<"]\n;

for(int i= 0; i<sentence.length(); i++)
{
if (sentence[i] != ' ')
spacecount++;
}
cout<<"number of non blank characters is: "<<spacecount<<endl;
spacecount = 0;

currentll = sentence.length()
if (currentll = maxline)
lineap++;

else if(currentll > maxline)
{
maxline = currentll;
line ap = 1;
}
}

cout<<"number of lines entered is: "<<count<<endl;
cout<<"length of longest line is :" << maxline<<endl;
cout<<"longest line appears: " <<lineap<<endl;


can anyone tell me where ive gone wrong and what i should change?
1
2
3
currentll = sentence.length()
if (currentll = maxline)
lineap++;


If I am not mistaken, you already defined currentll before. Why state it again?
Also, maxLine is stated, but how will currentll ever equal maxLine? You never assign a value to it.

Also, I think you are a bit ahead of yourself in your code, I am not going to do your assignment for you, but I can get you moving in the right direction:

1
2
3
4
5
6
7
8
9
10
currentll = sentence.length()
if (currentll = maxline)//how can currentll = maxLine if maxLine has no value or is not compared
lineap++;

else if(currentll > maxline)//on the right track here
{
maxline = currentll;//still, how can maxLine = currentll?
line ap = 1;
}
}
thanks william. im not asking you do it for me, coz i also want to learn this stuff so i can stop being a noob. Anyways wat do i do with maxline? initialize its vlaue or compare it to what?
please point me in the right way and ill figure out the code.
hey i figured it out. its working fine. thanks for your help.
Topic archived. No new replies allowed.