Last edited by tabstop; 11-26-2008 at 03:11 PM. Reason: where did for come from?
So instead of a while statement, would it be better to use If?
Or would it be more efficient to change the while loop? Thanks!
Code:if (c != '\n'){ ++possiblemax; }
Code:while((c=getchar()) != '\n'){ ++possiblemax; }
So I changed my code a little bit...I don't think it ever reaches the part where c is equal to a new line within the while loop though. How would I fix this?
Code:#include <stdio.h> #define MAXLINE 10 int getLine(char line[], int maxline); void copy(char to[], char from[]); //prints longest input line main(){ int len; //current line length int max; //maximum length seen so far int possiblemax; //possible maximum length int realmax; //the real maximum length int c; //the char char line[MAXLINE]; //current input line char longest[MAXLINE]; //longest line saved here max = possiblemax = realmax = 0; while ((len = getline(line, MAXLINE)) > 0) //checks to see if array ends with a new line if (line[len - 1] != '\n'){ //while c is not end of file while ((c = getchar()) != '\n'){ //if c is not equal to a new line if (c != '\n'){ //if c is not a new line add one to possible max ++possiblemax; } //else if is equal to a new line else if (c == '\n'){ //if possiblemax is greater than real max //set realmax to possiblemax //reset possible max if (possiblemax > realmax){ realmax = possiblemax; possiblemax = 0; } //otherwise reset possiblemax else possiblemax = 0; } } } if (len > max){ max = len; copy(longest, line); } if (max > 0){ //there was a line printf("length:%d", realmax); putchar('\n'); printf("%s", longest); } return 0; } //read a line into s, return length int getline(char s[], int lim){ int c, i; for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) s[i] = c; if(c == '\n'){ s[i] = c; ++i; } s[i] = '\0'; return i; } //copy from into to: assume to is big enough void copy(char to[], char from[]){ int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; }
So now I thought about it again and tried a different approach. My program now doesn't freeze anymore. However it doesn't return anything back...just tells me to press any key to continue.
Code:#include <stdio.h> #define MAXLINE 10 int getLine(char line[], int maxline); void copy(char to[], char from[]); //prints longest input line main(){ int len; //current line length int max; //maximum length seen so far int possiblemax; //possible maximum length int realmax; //the real maximum length int c; //the char char line[MAXLINE]; //current input line char longest[MAXLINE]; //longest line saved here max = possiblemax = realmax = 0; while ((len = getline(line, MAXLINE)) > 0) //checks to see if array ends with a new line if (line[len - 1] != '\n'){ //if c is not equal to a new line if ((c = getchar()) != '\n'){ //if c is not a new line add one to possible max ++possiblemax; } //else if is equal to a new line else if ((c = getchar()) == '\n'){ //if possiblemax is greater than real max //set realmax to possiblemax //reset possible max if (possiblemax > realmax){ realmax = possiblemax; possiblemax = 0; } //otherwise reset possiblemax else possiblemax = 0; } } if (len > max){ max = len; copy(longest, line); } if (max > 0){ //there was a line printf("length:%d", realmax + MAXLINE); putchar('\n'); printf("%s", longest); } return 0; } //read a line into s, return length int getline(char s[], int lim){ int c, i; for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) s[i] = c; if(c == '\n'){ s[i] = c; ++i; } s[i] = '\0'; return i; } //copy from into to: assume to is big enough void copy(char to[], char from[]){ int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; }
bump..I still need help...
You do realize the code you've posted doesn't compile? (There is no definition of the symbol "getline" when the function is called, because your prototype is for getLine.)
Okay..I fixed it..it didn't seem to be having any problems compiling in Microsoft Visual C++ though. I think I know why it did not compile in c++ bloodshed though.
Here is the correct code without the spelling mistake. However it still does the same thing..gives me a length only up to 9.Code:#include <stdio.h> #define MAXLINE 10 int getline(char line[], int maxline); void copy(char to[], char from[]); //prints longest input line main(){ int len; //current line length int max; //maximum length seen so far int possiblemax; //possible maximum length int realmax; //the real maximum length int c; //the char char line[MAXLINE]; //current input line char longest[MAXLINE]; //longest line saved here max = possiblemax = realmax = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max){ max = len; copy(longest, line); } //checks to see if array ends with a new line if (line[len - 1] != '\n'){ //if c is not equal to a new line if ((c = getchar()) != '\n'){ //if c is not a new line add one to possible max ++possiblemax; } //else if is equal to a new line else if ((c = getchar()) == '\n'){ //if possiblemax is greater than real max //set realmax to possiblemax //reset possible max if (possiblemax > realmax){ realmax = possiblemax; possiblemax = 0; } //otherwise reset possiblemax else possiblemax = 0; } } realmax = realmax + max; if (max > 0){ //there was a line printf("length:%d", realmax); putchar('\n'); printf("%s", longest); } return 0; } //read a line into s, return length int getline(char s[], int lim){ int c, i; for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) s[i] = c; if(c == '\n'){ s[i] = c; ++i; } s[i] = '\0'; return i; } //copy from into to: assume to is big enough void copy(char to[], char from[]){ int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; }
You realize this will only happen when there are two enter-keys in a row?Code:else if ((c = getchar()) == '\n'){ //if possiblemax is greater than real max //set realmax to possiblemax //reset possible max if (possiblemax > realmax){ realmax = possiblemax; possiblemax = 0; } //otherwise reset possiblemax else possiblemax = 0; }
I am not sure what you mean by two enter keys in a row...can you explain it please? As in press enter twice? I realized I had to do that, but I do not see why my coding would produce such a result.
Well, what do you think getchar does if not take characters from input? For this piece of code to execute, the if statement (with a getchar in it) would have to be false, meaning that enter key has been eaten -- but this else if has another getchar in it, which means we need to have another enter-key.
Edit: Oh: and if you want to go through the whole line, shouldn't you have a while loop (loop until c == \n, rather than just check the very next letter)?
Last edited by tabstop; 11-28-2008 at 04:40 PM.
Didn't you tell me that loop would never stop? Also I removed that getchar() from my else if, but it seems like I still have to press enter twice.
It sure does look like it is infinitely going on with this code though. I also moved the (c = getchar()); to the outside of the if and else statements.Code:#include <stdio.h> #define MAXLINE 10 int getline(char line[], int maxline); void copy(char to[], char from[]); //prints longest input line main(){ int len; //current line length int max; //maximum length seen so far int possiblemax; //possible maximum length int realmax; //the real maximum length int c; //the char char line[MAXLINE]; //current input line char longest[MAXLINE]; //longest line saved here max = possiblemax = realmax = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max){ max = len; copy(longest, line); } //checks to see if array ends with a new line if (line[len - 1] != '\n'){ (c = getchar()); //while c is not equal to a new line while (c != '\n'){ //while c is not a new line add one to possiblemax ++possiblemax; } //if is equal to a new line if (c == '\n'){ //if possiblemax is greater than real max //set realmax to possiblemax //reset possible max if (possiblemax > realmax){ realmax = possiblemax; possiblemax = 0; } //otherwise reset possiblemax else possiblemax = 0; } } realmax = realmax + max; if (max > 0){ //there was a line printf("length:%d", realmax); putchar('\n'); printf("%s", longest); } return 0; } //read a line into s, return length int getline(char s[], int lim){ int c, i; for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) s[i] = c; if(c == '\n'){ s[i] = c; ++i; } s[i] = '\0'; return i; } //copy from into to: assume to is big enough void copy(char to[], char from[]){ int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; }
The while and the getchar need to be together, as the only thing that can make your while loop stop is the getchar! You need to change c inside the loop, not ten lines away.
Oh I get what you are saying..However now my program does nothing at all, no matter how many "enter-keys" I put in. Assuming my while loop works, I think it may have to do with when my code actually reaches a new line.
Also I must say thank you for your patience with me! =]Code:#include <stdio.h> #define MAXLINE 10 int getline(char line[], int maxline); void copy(char to[], char from[]); //prints longest input line main(){ int len; //current line length int max; //maximum length seen so far int possiblemax; //possible maximum length int realmax; //the real maximum length int c; //the char char line[MAXLINE]; //current input line char longest[MAXLINE]; //longest line saved here max = possiblemax = realmax = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max){ max = len; copy(longest, line); } //checks to see if array ends with a new line if (line[len - 1] != '\n'){ //while c is not equal to a new line while ((c = getchar()) != '\n'){ //while c is not a new line add one to possible max ++possiblemax; } //if is equal to a new line if (c == '\n'){ //if possiblemax is greater than real max //set realmax to possiblemax //reset possible max if (possiblemax > realmax){ realmax = possiblemax; possiblemax = 0; } //otherwise reset possiblemax else possiblemax = 0; } } realmax = realmax + max; if (max > 0){ //there was a line printf("length:%d", realmax); putchar('\n'); printf("%s", longest); } return 0; } //read a line into s, return length int getline(char s[], int lim){ int c, i; for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) s[i] = c; if(c == '\n'){ s[i] = c; ++i; } s[i] = '\0'; return i; } //copy from into to: assume to is big enough void copy(char to[], char from[]){ int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; }
Your indentation is lying to you, perhaps. Do you want to be able to type in more than one line?
Maybe add the pair of braces, so that the while loop sees more than just the reading-in part.Code:while ((len = getline(line, MAXLINE)) > 0) { if (len > max){ max = len; copy(longest, line); } //checks to see if array ends with a new line if (line[len - 1] != '\n'){ //while c is not equal to a new line while ((c = getchar()) != '\n'){ //while c is not a new line add one to possible max ++possiblemax; } //if is equal to a new line if (c == '\n'){ //if possiblemax is greater than real max //set realmax to possiblemax //reset possible max if (possiblemax > realmax){ realmax = possiblemax; possiblemax = 0; } //otherwise reset possiblemax else possiblemax = 0; } } } realmax = realmax + max; if (max > 0){ //there was a line printf("length:%d", realmax); putchar('\n'); printf("%s", longest); } return 0; }