Two weeks I have been working on this program. The user will enter data into the structure then using strcmp I will search for a piece of information and print the results out. A very straightforward program, but I can't get it to print out.
If I enter co all lower and then search co, it won't print that array element. Did I miss a step, because stepping through in a linear fashion everything lines up and should work.
Thank you for any assistance
Code:
/*
*Name:
*Assignment: Structures input assignment
*Date Written: 7 August 2013
*Course: CS133U Summer 2013
*Purpose: Accept user input into
*a structure
*
*Sources: "C A Reference Manual"
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Defining a structure with
//a tag of Customer
typedef struct{
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
int zip;
char phone[15];
int accountId;
}Customer;
int main(){
//information is an array of 10
//Customer structures
Customer information[10];
//Char state is for searching
char state[3];
//i is for first for loop
//search is for second for loop
//count is for strcmp result
int i, search, count;
//For loop to accept user input
for (i = 0; i <= 2; i++){
printf("Type customer name %d: ", i);
scanf(" %s", information[i].firstName);
printf("Type customer last name %d: ", i);
scanf(" %s", information[i].lastName);
printf("Type customer street %d: ", i);
scanf(" %s", information[i].street);
printf("Type customer city %d: ", i);
scanf(" %s", information[i].city);
printf("Type customer state %d: ", i);
scanf(" %s", information[i].state);
printf("Type customer zip code %d: ", i);
scanf(" %d", &information[i].zip);
printf("Type customer phone number %d: ", i);
scanf(" %s", information[i].phone);
printf("Type customer account id %d: ", i);
scanf(" %d", &information[i].accountId);
}
//Search for the state in the struct
printf("Enter state to search: ");
scanf(" %s", state);
//Same thing as the first for loop,
//start at zero and increment
for (search = 0; search <= 2; search++){
//Using strcmp to search the elements
if(strcmp(information[search].state, state) != 0){
count = 1;
break;
}
else
count = 0;
}
//Below is where it either prints a result if > 1 or prints a
//message that it couldn't be found
if (count >= 1){
printf("Found data for: \n"
"Customer number: %d\n"
"Account: %d\n"
"Name: %s %s\n"
"Address: %s %s %s %d\n"
"Phone number: %s\n",
informationcopy[search].accountId,
informationcopy[search].accountId,
informationcopy[search].firstName,
informationcopy[search].lastName, informationcopy[search].street,
informationcopy[search].city, informationcopy[search].state,
informationcopy[search].zip, informationcopy[search].phone);
}
else
printf("Not found\n");
return 0;
}
If I enter co all lower and then search co, it won't print that array element. Did I miss a step, because stepping through in a linear fashion everything lines up and should work.
Thank you for any assistance