Problem with Getline

closed account (21CSz8AR)
I am still having trouble with the function getline and was hoping some of you guys could help me fix my program. The goal is to read a sentence typed in by the user (instead of ending with a period it ends with the word "end") and then print out the largest word and how long it was.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include "stdio.h"
#include "string.h"


int main(){
    string input;
    int length;
    int Maxlength;
    int MaxStr;
    printf ("What do you want your sentence to be? ");
	scanf ("%s")
        length = strlen(input);
    while (!stringEqual(input,"end"))
    {
        input = getline();
        length = strlen(input);
        if (length > Maxlength)
        {
            Maxlength = length;
            MaxStr = input;
        }
    }
printf ("The longest string was %s with %d characters", MaxStr, Maxlength);
return 0;    
}


I am using C and I know that this a C++ forum but I was hoping you guys could overlook that and help with the problem. Thanks for your trouble!
closed account (48T7M4Gy)
while(!(strcmp(...)) etc note brackets and function
http://www.cplusplus.com/reference/cstring/strcmp/
I am using C and I know that this a C++ forum but I was hoping you guys could overlook that and help with the problem

But...
string input;
Is hardly legal C, and even in C++ you can't use it as you've tried.

scanf ("%s")
This is doubly wrong. The length is not limited in anyway and you do not provide a buffer to the function.

There is no function getline in C.
Last edited on
closed account (48T7M4Gy)
http://c-for-dummies.com/blog/?p=1112
Topic archived. No new replies allowed.