Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for records

Status
Not open for further replies.

liepaja1

Technical User
Mar 16, 2011
1
0
0
LV
Hmm.. I have a problem. I should find and output to screen all records with people who live in city -Riga(for example). This doesn't work :-D


#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#define N 10
int main()
{
struct people
{
char city[20], name[15];
} people[25];
int i, j;

system("cls");
printf("Complete the list:\n");
for(i=0; i<N; i++)
{
printf("Name: ");
gets(people.name);
printf("City: ");
gets(people.city);
}
system("cls");
printf("List\n");
for(i=0; i<N; i++)
printf("\n%20s%15s", people.city, people.name);
printf("\n");

printf("\n\nList with people who live in Riga:");
for(i=0; i<N; i++)
if(people.city=="Riga")
printf("\n%20s%15s", people.city, people.name);
getch();
return 0;
}

 
The statement
Code:
if (people[i].city=="Riga")
compares A POINTER to i-th city member with the POINTER to "Riga" string literal. Of course, these two pointer values are different at any case.
Use strcmp function to compare C-style texts (reread your text-book;).

PS. It's so antique (DOS epoch or just before;) coding style with system("cls"), gets, printf and getch. May be better to write C++ codes?..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top