anyone know how i can make it soooo if i was to put in france it would check and say sorry invalid place name please re enter i have been trying for ages and still cant get it to work
anyone know how i can make it soooo if i was to put in france it would check and say sorry invalid place name please re enter i have been trying for ages and still cant get it to work
right this is the code im stuck on
The user will input a place name saved in pos and if pos is equal to cities which contains = "London", "Glasgow", "Birmingham","Manchester", "Luton", "Poole", "Stafford", "Stoke", "Cardiff", "Derby" ..... i want it to to put out a error message and ask for re input of the place sooo if i was to enter France it would check cities find out its not there and ask for re input can anyone helpCode:char pos[2][16]; const char *cities[] = {"London", "Glasgow", "Birmingham", "Manchester", "Luton", "Poole", "Stafford", "Stoke", "Cardiff", "Derby"}; scanf("%15s", pos[i]); size_t x; for(x = 0; x < sizeof(cities) / sizeof(*cities); x ++) { if(!strcmp(cities[x], pos[i])){point[i] = x;} }
thankyou
What you can do is use a flag, a bool, and initialize it to false.
Then loop through your array and check if you can find a string that matches what the user inputted.
If you find one, set flag to true and break the loop.
That way you know that if the flag is false, no match was found, and if it's true, then the city was found.
hmmm im a little confused about how i would implement this
atm im messing around with this idea
Code:scanf("%16s", pos[i]); size_t x; for(x = 0; x < sizeof(cities) / sizeof(*cities); x ++) { if(!strcmp(cities[x], pos[i])==0){point[i] = x;} else if (!strcmp (cities[x],pos[i])!=0) { flag++; } } while(flag==10) { printf("Invalid place name please re enter: "); wrong=1; i++; }
Thread has a 129 posts and it appears that the OP has made little progress.
Surely, someone can post a basic input validation function consisting of about 12 lines of code to help this guy out
Well, the last 130 posts didn't help him much with a basic homework assignment, an assignment that would only take approximately 80 lines of code to write. Maybe it's time to spoon feed him actual code.
And what will this accomplish? You're just handing code over to someone. Then they'll make the same thing over and over and come back for help.
It's pretty much the same as handing over some code just to make someone pass their test. In other words, cheating. They won't get anywhere.
The only thing it'll accomplish is to bring an end to this long drawn out thread a lot sooner. I would strongly encourage the OP to discuss his/her homework assignment with his/her instructor for additional guidance and direction since it's apparent he/she is not making much progress on a very basic homework assignment.