Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//This is just a part of a big code. In this function I want ot print all the results in the linked list having the same zipcode as the one entered by the user. But the scanf to input the zipcode from the user doesn't seem to work. When i print the zipcode it says NULL//


C++
void searchzip()
{
char code[6];
printf("\nPlease enter the zip code that you want to search :\n");
scanf("%s",code[6]);
printf("\n%s",code[6]);
struct student *temp=head;
do
{
if(strcmp(code,temp->zip)==0)
	{
	printf("\nFirst name: %s Last name: %s Score: %f ZipCode: %s",temp->firstname,temp->lastname,temp->score,temp->zip);
	}
temp=temp->next;
}
while(temp!=NULL);
}
Posted
Updated 11-May-15 11:21am
v3

1 solution

First of all,
C++
scanf("%s",code);

But then, it's not clear why scanning it as string ("%s"), why not using gets_s. If you want zip code, it could be
C++
int zipCode;
scanf("%d", &zipCode);
For further progress, 1) use the debugger, 2) read the documentation: http://www.cplusplus.com/reference/cstdio/scanf[^].

—SA
 
Share this answer
 
v3
Comments
[no name] 11-May-15 17:07pm    
The program askes us to have the zipcode as an string.
Sergey Alexandrovich Kryukov 11-May-15 17:26pm    
This is wrong, but for string use just gets_s. Anyway, I answered... :-)
—SA
CPallini 11-May-15 17:23pm    
5.
Sergey Alexandrovich Kryukov 11-May-15 17:26pm    
Thank you, Carlo.
—SA
Sascha Lefèvre 11-May-15 17:31pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900