get rid of the redCode:(!strcmp
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
yes i have done that and well i have been fiddling around n well now i am tottally confussed of wat sympols to use ect ect atm
i have just that atm and just trying differnt symbols like ==0 ==1 >0 <0 ectect im just confussedCode:for(x = 0; x < sizeof(cities) / sizeof(*cities); x ++) { if(strcmp(cities[x], pos[i])){point[i] = x;} // assigns point a number depends what place name was enterd if(strcmp(cities[x], pos[i])) {flag=1;} } if(flag==0) { printf("\n Invalid place name please re enter: "); i--; wrong=1; }
all i could find is how to initialize strcmp and well the outcome
i still dont understand lol can anyone link me somthing so i can understand this stuff its been haunting me for a few days nowCode:Negative if s1 is less than s2. Zero if s1 and s2 are equal. Positive if s1 is greater than s2
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
So when you say "doesn't work" and "i get no error message", what do you expect to happen (that is the "works" I suppose), and how is that different when you say "doesn't work". The above is correct code for point[i] being set to x when pos[i] matches cities[x], and flag will be set to one if pos[i] is not equal to cities[x] - of course, if that's a direct copy of the code, I would prefer:
Edit: to clarify - if you have already tested that if the two strings are equal, comparing them again to see if they are "not equal" doesn't make much sense - we already know that. And strcmp() can be quite an expensive operation if the strings are nearly equal and long, so it's good practice to not do comparisons when they are not needed.Code:if(strcmp(cities[x], pos[i])==0) { point[i] = x; } else { flag=1; }
Edit2: Having read your code again, I think it looks like flag should be set to 1 when you have found the city, not the other way around. So perhaps what you really want is:
Your code as it stands would always set flag = 1 at some point, because there's only at most one city name that matches, and all others will be a non-match - so you probably want set the flag ONLY when you have a match.Code:if(strcmp(cities[x], pos[i])==0) { point[i] = x; flag=1; }
--
Mats
Last edited by matsp; 04-04-2008 at 05:38 AM. Reason: Some clarification.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
i found the problem it was that
is staticly set and not in the right orderi need to gather that info from the txt file i cant have nothing statically set i may have to do somthing with the array places[11] because that gets all the place names how would i set thisCode:const char *cities[] = {"London", "Glasgow", "Birmingham", "Manchester", "Luton", "Poole", "Stafford", "Stoke", "Cardiff", "Derby"};
Elysia, I see that you are a Programming MASTER! Congrats! Now it's time for you to set up an appointment with your counselor to learn to be more patient with people. This forum is a very very helpful tool. It's filled with people’s challenges & questions. Either you contribute to the solutions, like I know you do in many cases or simply opt out and keep YOUR opinions of peoples short comings, errors, or mistakes to yourself. Keep all your posts as factual as possible. I'm sure everyone will love you for it! It's important to keep people positive in c programming!
Yes first post, but one all in here want to see.
ROCK
When the post is the 160th of the thread, it shows quite a bit of patience, I would say.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
anyone know how i can have this constructor gather the info from text file or place array