//make a location marker for key items
#include <stdio.h>
#include <string.h>
struct find {int index; char name[50]; char location[50]; };
int main() {
char locate_item[50];
//using struct to add items
struct find item1;
item1.index = 1;
strcpy(item1.name, "guitar");
strcpy(item1.location, "Usually near the table in the living room area.\n");
struct find item2;
item2.index = 2;
strcpy(item2.name, "ipad");
strcpy(item2.location, "Usually on the table or charging on the bed.\n");
//using while and if statements to get user feedback and display the appropriate location
while (locate_item != item1.name || locate_item != item2.name) {
printf("what is the item you want to find? \n");
scanf("%s", locate_item);
printf("You entered %s\n", locate_item);
if (locate_item == item1.name) {
printf("%s", item1.location);
} else if (locate_item == item2.name) {
printf("%s", item2.location);
} else {
printf("Incorrect entry. Please try again.\n");
}
}
return 0;
}
Asked
Active
Viewed 45 times
-2

Allan Wind
- 23,068
- 5
- 28
- 38

bzs
- 1
- 1
-
This is the compiled message I get for my code....... what is the item you want to find? guitar You entered guitar Incorrect entry. Please try again. what is the item you want to find? – bzs Apr 14 '22 at 07:16
-
1Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour] and read about [ask] good questions. Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and learn how to **[edit]** your questions to improve them. – Some programmer dude Apr 14 '22 at 07:17
-
It doesn't show any error messages but I still cant understand the problem. Please Help – bzs Apr 14 '22 at 07:18
-
1And you seem to have missed something very basic in your text-book, or skipped a class, because comparing strings can't be done with `==` (or `!=`). – Some programmer dude Apr 14 '22 at 07:18
-
Also you can't use uninitialized variables. Their values will be *indeterminate* (look at them as garbage), and using an uninitialized arrays as a string can often lead to *undefined behavior*. – Some programmer dude Apr 14 '22 at 07:19
-
Thankyou so much. I am new to the game and learning C from youtube videos. Your help means a lot to me. – bzs Apr 14 '22 at 07:35
-
Also would you please point out the location of error from your second feedback. – bzs Apr 14 '22 at 07:36
-
2Youtube videos to learn programming seams to be a hit and miss, and mostly miss. Get [some good books](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and if possible take some classes. – Some programmer dude Apr 14 '22 at 07:51
-
Ok I will do that. Thanks. Much appreciated. – bzs Apr 14 '22 at 07:54
1 Answers
0
locate_item != item1.name || locate_item != item2.name
compares pointer, however, you want to use strcmp()
to compare two strings by value. strcmp()
returns 0 if the two strings are the same.
In either case, your program doesn't make a lot of sense. You probably want to find locate_item
in the an array of items
(renamed from find
) along these lines:
#include <stdio.h>
#include <string.h>
struct item {
int index;
char name[50];
char location[50];
};
int main() {
struct item items[] = {
{ 1, "guitar", "Usually near the table in the living room area." },
{ 2, "ipad", "Usually on the table or charging on the bed." },
{ 0 }
};
for(;;) {
printf("what is the item you want to find? \n");
char locate_item[50];
scanf("%s", locate_item);
//printf("You entered %s\n", locate_item);
for(int i = 0; items[i].name[0]; i++) {
if(!strcmp(items[i].name, locate_item)) {
printf("%s\n", items[i].location);
goto done;
}
}
printf("Incorrect entry. Please try again.\n");
}
done:
return 0;
}

Allan Wind
- 23,068
- 5
- 28
- 38