How to use the Getline function

closed account (21CSz8AR)
This is a problem that I am required to do for a c class and was just wondering if you guys could help! I am not really sure what is going wrong but was hoping some of you could help explain it to me. In reference, the problem is supposed to read a sentence submitted by the user and then once they type the word "end" say the longest word and how many characters it has.

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
26
#include "strlib.h"
#include "stdio.h"


main(){
    string input;
    int length;
    int Maxlength;
    int MaxStr;
    printf ("What do you want your sentence to be? ");

        input = getline();
        length = StringLength(input);
    while (!StringEqual(input,"end")
    {
        input = GetLine();
        length = StrLen(input);
        if (length > Maxlength)
        {
            Maxlength = length;
            MaxStr = input;
        }
    }
printf ("The longest string was %char with %d characters", MaxStr, Maxlength);
return 0;    
}
Last edited on
closed account (21CSz8AR)
I am doing it in C, so I will try your suggestion but it might not work solely because of that, thanks for your advice though!
closed account (z05DSL3A)
gentleguy, The pointless "Does that help you?" will likely get reported, stop doing that and you will be reported a lot less. It's just an observation.
I will stop when I have reached a certain post count.

But that's why it is discouraged. Here you acknowledge openly that you make unnecessary posts. Why not simply stop the unnecessary posts?
It isn't a small detail. He is pointing it out because you saying that implies that you think post count means anything on this site. The fact that you made a comment over how you had 1k posts on your other account and now will stop posting pointless posts after you hit a certain number implies you think the post count somehow qualifies experience or means something on this site. To put it plainly, it doesn't. The consecutive posts instead of editing the first one and that comment makes it look like you are posting just to simply pad your count which will result in your account being closed again by admin.
Last edited on
closed account (48T7M4Gy)
Just a handful of bloopers to be fixed up and then we can call this thread quits I reckon.

Who wants to fix the first blooper, deliberate or otherwise? :)

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
26
#include "strlib.h" // <-- fix required <stdlib> maybe
#include "stdio.h"


main(){
    string input;
    int length;
    int Maxlength;
    int MaxStr;
    printf ("What do you want your sentence to be? ");

        input = getline(); // <-- fix required
        length = StringLength(input);// <-- fix required
    while (!StringEqual(input,"end")// <-- fix required
    {
        input = GetLine();// <-- fix required
        length = StrLen(input);// <-- fix required
        if (length > Maxlength)
        {
            Maxlength = length;
            MaxStr = input;
        }
    }
printf ("The longest string was %char with %d characters", MaxStr, Maxlength);
return 0;    
}
Last edited on
@closed account (21CSz8AR)

As you say this is for "C class", but you are using both C syntax and C++ syntax. You wrote length = StringLength(input); it should be length = strlen(input);, but that will not work because input is defined as a C++ string which does not work with a C program. length = input.length() does work, but it is not C. There might be a work around for that, but it would not be a C program.

The general concept of the program works and can be made to work, but it becomes more a C++ program.

Hope that helps,

Andy

Correction meant to say, the last printf statement does not work ant there might be a work around... Yes there is a work around, but it is a C++ style syntax.

Played with your program and this is what I came up with:
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
26
27
28
	string input;  // c++ style
	//char input[100];  //  Does not work with c++ syntax and style
	int length{ 0 }, lc{ 1 };  // need to initalize the two ints before use
	int Maxlength{ 0 };
	string MaxStr;
	printf("What do you want your sentence to be? [end] to quit \n");
	printf("%d. ", lc++);

	getline(cin, input);  //  c++ syntax
	printf("\n");
	length =  input.length();  //  c++ syntax
	while (input.compare("end") !=  0)  // c++ syntax
	{
		printf("%d. ", lc++);
		getline(cin, input);
		printf("\n");
		length = input.length();
		if (length > Maxlength)
		{
			Maxlength = length;
			MaxStr = input;
		}
	}
	//  MaxStr.c_str() is c++ syntx to change a string into a c-string.
	//  With out c_str() the printf will print garbage.
	printf("The longest string was \"%s\" with %d characters\n", MaxStr.c_str(), Maxlength);

	cout << "\nThe longest string was \"" << MaxStr << "\" with " << Maxlength << " characters" << endl;

This is just the code between the braces of main. My header includes are different from what you have. I used:
1
2
3
4
5
6
7
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <conio.h>
#include <iomanip>
#include <Windows.h> 

although not all are needed it is a header file I use for general quick use.
Last edited on
gentleguy is trying to artificially boost his post count, in order to deceive people into thinking he is more experienced than he really is. He has already admitted to lying about his C++ experience, to fraudulently obtain a position of mentor, under an older account.

It is behaviour like this that has gotten several of his previous accounts banned here.

I can absolutely assure anyone reading this thread that TheIdeasMan is not the only person reporting gentleguy's abuse of these forums.
Last edited on
Stop trolling. Admin doesn't just remove the text. If he doesn't like your post or it is not appropriate to the thread, he will delete the whole post so the Aug 22, 2016 at 5:32pm would no longer be there and it would go to your last troll post.
Last edited on
Huh... I hadn't considered that gentleguy might have actually deleted the text of his post himself, and then dishonestly tried to misrepresent it as admin deleting the text. But you're right - admin would probably just have deleted the entire post, not left the post there with a weird edit.
Topic archived. No new replies allowed.