Results 1 to 4 of 4
Hey, if anyone is able to have a look at my code and help me figure out why the loop won't exit that would be great.
Its a simple program ...
- 08-08-2009 #1Just Joined!
- Join Date
- Mar 2008
- Location
- Canada
- Posts
- 4
C Loop Exiting Problem
Hey, if anyone is able to have a look at my code and help me figure out why the loop won't exit that would be great.
Its a simple program to check for whether your input is the longest string, if it is then the string is replaced by it and then displayed, if not the most recent longest string is displayed.
The problem is that the while Loop in main does not seem to exit when the string_Length function returns a negative number or if i add a strcmp() into the conditions of the main while loop.
Any help would be great thank you.
Code:#include <stdio.h> #include <string.h> #define MAXSTRING 1000 int string_Length(char string[], int max); void copy(char to[], char from[]); int main(void){ int current = 0; int max = 0; char exit[] = "exit"; char currentString[MAXSTRING]; char maxString[MAXSTRING]; max = 0; /* ...................PROBLEM STARTS HERE ...........................*/ while((current = string_Length(currentString, MAXSTRING)) > 0 || (strcmp(currentString, exit)) == 0)){ if(max < current){ max = current; copy(maxString, currentString); } if(max > 0) printf("Longest String Entered is: %s\n", maxString); } return 0; } int string_Length(char string[], int max){ char exit[] = "exit"; int c = 0; int i = 0; for(i = 0; (i < (max-1)) && (c = getchar()) != EOF && c != '\n'; i++) string[i] = c; if(c == '\n'){ string[i] = c; i++; } string[i] = '\0'; /* ....................OR PROBLEM HERE ....................*/ if((strcmp(string, exit)) == 0){ return -2; } return i; } void copy(char to[], char from[]){ int i = 0; while((to[i] = from[i]) != '\0') i++; }
- 08-08-2009 #2Just Joined!
- Join Date
- May 2006
- Location
- San Jose, CA
- Posts
- 67
At a quick glance.. i believe your comparison strcmp(currentString, exit) == 0 should be !=. Strcmp returns 0 if it's a match, +ve number if the string is larger and -ve if it's smaller than the string being compared to. Also why are you checking for the same exit condition at 2 different places?
Edit: So your problem gave me a C "itch" and I haven't itched this itch in a long time. So here goes nothing
. The problem you are having is in the way you have declared the exit variable, and they way that is being interpreted by strcmp as it compares stuff against it. You need to declare it as an char array so like this
Also you can get rid of the strcmp you are doing in the main function, just declare exit as i declared above in your string_length function, and your program will work fine.Code:char exit[4]; exit[0] = 'e'; exit[1] = 'x'; exit[2] = 'i'; exit[3] = 't';
- 08-09-2009 #3
So...a few comments.
First of all, strcmp() returns 0 when the two arguments are the same, and nonzero when different (KingX is correct on this point).
I am confused by your string_Length function. It is a very bad name: it does not tell me the length of a string, but instead reads a new string and returns its length. You may want to rename the function or split it into two functions.
For reading the string, you should look into the fgets() function. This function reads an entire line in one function call, which your while loop is doing.
You should also look into the strlen() function in string.h. This is a function that tells you the length of a string
. And while we're on the subject, your copy() function duplicates the standard strcpy() function, also in string.h.
Having said all of that, KingX is correct that your problem is the loop in your main() function. If the last string that was read is "exit", string_Length() returns -2, but the second condition is true (as the string is "exit"), so the loop continues. You want the second condition to use "!=" instead of "==".
KingX, however, is incorrect about how to declare strings. Your declaration of exit is perfectly valid. His method is technically correct, but it is certainly more complicated than necessary.DISTRO=Arch
Registered Linux User #388732
- 08-09-2009 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
From your code and your question(s), I have to think that this is a school exercise? If so, please refrain from posting here. The forum's terms of service are quite clear on this point.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote