In this for loop, you can eliminate the code which refers to strcmp(). You already have tested for it once. No more tests are needed. Do it once, do it right. One shot, one kill.
@Tater: Yep! I can tell.Code:for (numNames = 0; numNames < MAX_NAMES; numNames++){ printf("%s%s\n", names[numNames].first, names[numNames].last); if (strcmp (names[numNames].first, "END") == 0 && strcmp (names[numNames].last, "END") == 0) break; }
ChristianB, an index is just an index. The name is not important at all. Use something descriptive if you can, but just using i is a very long-standing idiom in C. Using a bunch of indexes, seems to have you a bit confused.
Think of all your indexes as all the same thing - simple counters of iteration. That's all.
If the first name is a duplicate of another name, that's OK, right? Only if both first and last names are duplicates should it be rejected?
so this is what I have so far. It doesn't print anything out. I have to be at work soon. it seems silly that i could fail a class for this, even though i've completed the rest of the course work.Code:#include <stdio.h> #include <string.h> #define MAX_NAMES 10 #define MAX_LEN 20 typedef struct { char first[MAX_NAMES]; char last[MAX_NAMES]; }Name; int Equal (Name p1, Name p2) { int i = 0; if (strcmp (p1.first, p2.first) == 0 && strcmp (p1.last, p2.last) == 0) i = 1; return i; } int main () { Name names[MAX_NAMES], name; int ndx, numNames = 0; scanf("%s%s", name.first, name.last); for(ndx = 0; ndx < MAX_LEN; ndx++){ scanf("%s%s", names[ndx].first, names[ndx].last); if (strcmp (names[ndx].first, "END") == 0 && strcmp (names[ndx].last, "END") == 0) break; } for(ndx = 0; ndx < MAX_LEN; ndx++){ if (Equal(names[ndx], name) != 1){ strcpy(names[ndx].first, name.first); strcpy(names[ndx].last, name.last); } } for (ndx = 0; ndx < MAX_LEN; ndx++){ printf("%s %s\n", names[ndx].first, names[ndx].last); } return 0; }
i know that i'm killing the loop somewhere, which is why my last printf isn't printing anything out.
as far as your question goes, yes if the entire name is a duplicate then it's rejeted. a duplicate first name, or a duplicate last name is fine though.
Last edited by christianB; 07-25-2011 at 02:59 PM.
NOPE! Sorry, didn't catch your other changes.
Your outer for loop needs to have a different index name than ndx. Use numNames or i or whatever you want, but don't use ndx, that is needed for the inner for loop.
Last edited by Adak; 07-25-2011 at 03:29 PM.
Ok... like this....
Code:#include <stdio.h> #include <string.h> #define MAX_NAMES 10 #define MAX_LEN 20 typedef struct { char first[MAX_NAMES]; char last[MAX_NAMES]; }Name; int Equal (Name p1, Name p2) { return (strcmp (p1.first, p2.first) == 0 && strcmp (p1.last, p2.last) == 0); } int main () { Name names[MAX_LEN], name; int ndx = 0, numNames = 0; while(numNames < MAX_LEN) { scanf("%s %s", name.first, name.last); getchar(); if (strcmp name.first, "END") == 0 && strcmp (name.last, "END") == 0) break; for(ndx = 0; ndx < numNames; ndx++) { if (Equal(names[ndx], name)) break; } if (ndx == NumNames) { strcpy(names[numNames].first, name.first); strcpy(names[numNames].last, name.last); numNames++; } } for (ndx = 0; ndx < NumNames; ndx++) { printf("%s %s\n", names[ndx].first, names[ndx].last); } return 0; }
That should be pretty close...
(Mods... This guy's online course is totally bogus and I'm not going to let him fail because of errors in the courseware and stupid size restrictions on his code!)
Last edited by CommonTater; 07-25-2011 at 03:42 PM. Reason: #$^ double = sign! AGAIN!
This works, but is a bit more verbose than Taters.
Good luck with your course, Christian.
Code:#include <stdio.h> #include <string.h> #define MAX_NAMES 10 #define MAX_LEN 20 typedef struct { char first[MAX_NAMES]; char last[MAX_NAMES]; }Name; int Equal (Name p1, Name p2) { int i = 1; if (strcmp (p1.first, p2.first) == 0 && strcmp (p1.last, p2.last) == 0) i = 0; return i; } int main () { Name names[MAX_NAMES], name; int ndx, numNames = 0, count=0, good=0; for(numNames = 0; numNames < MAX_NAMES; numNames++){ printf("Enter a first and last name.\n"); scanf("%s%s", name.first, name.last); if ((strcmp (name.first, "END") == 0) && (strcmp (name.last, "END")) == 0) break; for(ndx = 0, good=1; ndx < MAX_NAMES; ndx++) { if (Equal(names[ndx], name)==0) { good=0; } } if(good==1) { strcpy(names[count].first, name.first); strcpy(names[count].last, name.last); ++count; } } for (ndx = 0; ndx < count ; ndx++){ printf("%s %s\n", names[ndx].first, names[ndx].last); } return 0; }