Ok so I tried to search and didnt come up with anything so if this has been posted before, I am sorry. I am new to C programming and have done well in my class so far up till now, the teacher says that this is the hardest program and I have spent hours on it with nothing. I had my mom look at it who works for IBM, sister who has taken the class before and my brother in law who is a computer engineer and none could tell me what I have done wrong.

#include <stdio.h>
#include <stdlib.h>

#define NAME_LEN 3


       
   
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 fillInTheCustInfo(int i)
{
Customer cust;
printf("Enter Data for Customer %d", i);
printf("\nEnter First Last Phone: ");
scanf("%s %s %s", cust.firstName, cust.lastName, cust.phone);
printf("\nEnter Address (Street City State ZIP): "); 
scanf("%s %s %s %d", cust.street, cust.city, cust.state, &cust.zip);
return cust;
}

 void printCustArray(Customer cust)
{
//printf("The Data for Customer %d is", i); this line keeps getting flagged also
printf("\nFirst: %s, Last: %s Phone: %s", cust.firstName, cust.lastName, cust.phone);
printf("\nAddress (Street City State ZIP): %s %s %s %d", cust.street, cust.city, cust.state, cust.zip); 
}

 
int main() {
Customer custArray[NAME_LEN];
int i;
char stateCode[3];

for(i = 0; i < NAME_LEN; ++i)
{
custArray[i] = fillInTheCustInfo(i);
} 



      
printf("Enter 2-character state code: ");
scanf(" %c",&stateCode);
 
  for(i = 0; i < NAME_LEN; ++i)
{
if (custArray[NAME_LEN].state == stateCode); //this is the line I am having a problem with
printCustArray(custArray[i]);
}



system("pause");
return (0);
}

my output is

Enter Data for Customer 0
Enter First Last Phone: Doug Jones 123-456-7890

Enter Address (Street City State ZIP): Main Portland CO 12345
Enter Data for Customer 1
Enter First Last Phone: Doug Washington 123-456-7890

Enter Address (Street City State ZIP): Main Portland WA 12345
Enter Data for Customer 2
Enter First Last Phone: Another Colorado 123-456-7890

Enter Address (Street City State ZIP): Main Portland CO 12345
Enter 2-character state code: CO

First: Doug, Last: Jones Phone: 123-456-7890
Address (Street City State ZIP): Main Portland CO 12345
First: Doug, Last: Washington Phone: 123-456-7890
Address (Street City State ZIP): Main Portland WA 12345
First: Another, Last: Colorado Phone: 123-456-7890
Address (Street City State ZIP): Main Portland CO 12345Press any key to continue
. . .

Mine is listing all info entered.
The output is supposed to look like

Enter Data for Customer 0
Enter First Last Phone: Doug Oregon 123-456-7890
Enter Address (Street City State ZIP): Main Portland OR 12345


Enter Data for Customer 1
Enter First Last Phone: Doug Washington 123-456-7890
Enter Address (Street City State ZIP): Main Portland WA 12345


Enter Data for Customer 2
Enter First Last Phone: Doug California 123-456-7890
Enter Address (Street City State ZIP): Main Portland CA 12345


Enter Data for Customer 3
Enter First Last Phone: Doug Nevada 123-456-7890
Enter Address (Street City State ZIP): Main Portland NV 12345


Enter Data for Customer 4
Enter First Last Phone: Doug Colorado 123-456-7890
Enter Address (Street City State ZIP): Main Portland CO 12345


Enter Data for Customer 5
Enter First Last Phone: Another Colorado 123-456-7890
Enter Address (Street City State ZIP): Main Portland CO 12345


Enter Data for Customer 6
Enter First Last Phone: Doug Arizona 123-456-7890
Enter Address (Street City State ZIP): Main Portland AZ 12345


Enter Data for Customer 7
Enter First Last Phone: Doug Florida 123-456-7890
Enter Address (Street City State ZIP): Main Portland FL 12345


Enter Data for Customer 8
Enter First Last Phone: Doug Georgia 123-456-7890
Enter Address (Street City State ZIP): Main Portland GA 12345


Enter Data for Customer 9
Enter First Last Phone: Doug Jones 123-456-7890
Enter Address (Street City State ZIP): Main Portland CO 12345


Enter 2-character state code: CO
Data for Customer 4
Account: 4
Name: Doug Colorado
Addr: Main Portland CO 12345
Phone: 123-456-7890


Data for Customer 5
Account: 5
Name: Another Colorado
Addr: Main Portland CO 12345
Phone: 123-456-7890


Data for Customer 9
Account: 9
Name: Doug Jones
Addr: Main Portland CO 12345
Phone: 123-456-7890

Recommended Answers

All 4 Replies

printf("Enter 2-character state code: ");
scanf(" %c",&stateCode);
 
  for(i = 0; i < NAME_LEN; ++i)
{
if (custArray[NAME_LEN].state == stateCode); //this is the line I am having a problem with
printCustArray(custArray[i]);
}

1) You're instruction say to enter a 2 char state code, but %c will only handle one char - and it's NOT going to make it into a string if you add two char's with a loop, still using %c.

Strings in C MUST have an end of string char at the end, and %c won't put it there.
%s WILL put it there. ;)

2) You need to use strcmp(string1, string2) == 0 to determine if strings are equal in C. (or roll your own strcompare function).

if((strcmp(string1, string2)) == 0)
{
   do something in here
}

Is a standard idiom. You need to include string.h to use strcmp() (or have a smart compiler include it automatically for you).

Give that a shot. ;)

Ah man I am so close, thanks for all your help, now my output is:

Enter Data for Customer 0
Enter First Last Phone: Doug Jones 123-456-7890

Enter Address (Street City State ZIP): Main Portland CO 12345
Enter Data for Customer 1
Enter First Last Phone: Doug Washington 123-456-7890

Enter Address (Street City State ZIP): Main Portland WA 12345
Enter Data for Customer 2
Enter First Last Phone: Another Colorado 123-456-7890

Enter Address (Street City State ZIP): Main Portland CO 12345
Enter 2-character state code: CO
The Data for Customer 4200038 is

First: Doug, Last: Jones Phone: 123-456-7890
Address (Street City State ZIP): Main Portland CO 12345
The Data for Customer 4200038 is

First: Another, Last: Colorado Phone: 123-456-7890
Address (Street City State ZIP): Main Portland CO 12345
Press any key to continue . . .

Everything is right but the cust. account ID # my new code is:

#include <stdio.h>
#include <stdlib.h>

#define NAME_LEN 3


       
   
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 fillInTheCustInfo(int i)
{
Customer cust;
printf("Enter Data for Customer %d", i);
printf("\nEnter First Last Phone: ");
scanf("%s %s %s", cust.firstName, cust.lastName, cust.phone);
printf("\nEnter Address (Street City State ZIP): "); 
scanf("%s %s %s %d", cust.street, cust.city, cust.state, &cust.zip);
return cust;
}
printCustArray(Customer cust)
{
printf("The Data for Customer %d is\n", cust.accountId);
printf("\nFirst: %s, Last: %s Phone: %s", cust.firstName, cust.lastName, cust.phone);
printf("\nAddress (Street City State ZIP): %s %s %s %d\n", cust.street, cust.city, cust.state, cust.zip); 
}

 
int main() {
Customer custArray[NAME_LEN];
int i;
char stateCode[3];

for(i = 0; i < NAME_LEN; ++i)
{
custArray[i] = fillInTheCustInfo(i);
} 



      
printf("Enter 2-character state code: ");
scanf(" %s",&stateCode);
 
for(i = 0; i < NAME_LEN; ++i)
{
if(!strcmp(custArray[i].state, stateCode))

printCustArray(custArray[i]);
}



system("pause");
return (0);
}

You never assign a value to accountId. ;) ;)

Thanks for the help, it is working fine now. I should have seen that but it was 12:30 at night when I was working on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.