Reputation: 642
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define RECORDS 2
int main() {
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 ;
Customer customerArray[RECORDS];
typedef int records;
records i = 0;
printf("================================================================\n");
for(i = 0; i < RECORDS; ++i)
{
printf("Enter data for customer %d\n", i + 1);
printf("Enter firstname, last name, phone\n");
scanf("%s %s %s", customerArray[i].firstName, customerArray[i].lastName, customerArray[i].phone);
scanf("%*c");
printf("Enter Address (Street City State ZIP\n");
scanf("%s %s %s %d", customerArray[i].street, customerArray[i].city, customerArray[i].state, &customerArray[i].zip);
scanf("%*c");
}
for(i = 0; i < RECORDS; ++i) {
char input[3]= {'\0'};
printf("please enter state:\n");
scanf("%s",input);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
char *custstate = customerArray[i].state;
char *statelookup = input;
if (strcmp(custstate, statelookup) == 0) {
printf("\nData for customer number:%s", customerArray[i].accountId);
printf("\nAccount:%s", customerArray[i].accountId);
printf("\n Name:%s %s",customerArray[i].firstName, customerArray[i].lastName);
printf("\nAddress:%s %s %s %d",customerArray[i].street, customerArray[i].city, customerArray[i].state, customerArray[i].zip);
printf("\nPhone:%s",customerArray[i].phone);
}
else
printf("Could not find a match");
}
return 0;
}
Having trouble figutring out why one of member of this struct is not incremeting correctly. The account id produces inaccurate output. and also the customer number is not producing correctly. e.g 1, 2 ect..
The output for said members looks like this...
Data for customer number:1606416396 Account:1606416396
Where it should look like
Data for customer 1 Account 1
Not sure what I am doing wrong. Still learning C. Total noob. any help would be greatly appreciated!
I am thinking I have to null terminate the members. But not sure if that's true or even how.
Upvotes: 1
Views: 127
Reputation: 5836
All Variables , you use in your program reside mainly in two areas
By Default , all automatic program variables and Structure variable are , stored onto the stack , and hence like all stack variables , they are not initialized by the compiler , and hence will contain an unpredictable value , usually known as a junk value.So it is always better to initialize variables explicitly during declaration.
The variables which mainly reside in Data Segment are mostly Global and Static variables. These variables are automatically initialized to 0 , during declaration , so you won't find any junk value.
Example
static int i =0;
is the same as
static int i;
Do take a look at this page , for more info
http://www.geeksforgeeks.org/memory-layout-of-c-program/
Upvotes: 0
Reputation: 21773
You never initialize the field accountId
. In C, instead of getting a error for using an uninitialized field, it just uses a random value (because the RAM will have a random value from whatever memory was last there). So, the huge meaningless value is explained as being a leftover from some other program. To fix this, you have to set it to something before accessing it.
Upvotes: 3