Re: Problems w/my arrays. Any Advice?

From: Barry Schwarz (
Date: 11/07/04

Date: Sun, 07 Nov 2004 14:53:35 -0800

On 7 Nov 2004 09:18:48 -0800, (agentxx04) wrote:

>Hi. Our professor assigned us to create a program which stores coded
>information about sales for customers. I know where to problem is, I
>just don't know how to correct it. When the program is executed it's
>supposed to look like this:
>customer number? 12
>first letter of customer's last name? J
>amount of purchase? 57.62
>customer number? 92
>first letter of customer's last name? W
>amount of last purchase? 103.04
>customer number? 0
>Enter the number of the customer you want to display: 92
>first letter of customer's last name: W
>amount of last purchase: 103.04
>Enter the number of the customer you want to display: 0
>End of Program.
>This is what I got:

Excessive vertical white space snipped from above, just to save

>/*This program was designed to store coded information about sales for
>customers. Each customer has a number from 1 to 100. The user will be
>propmpted for their customer number, the first letter of their last
>name, &
>the amount of their last purchase. This program will continue to aks
>user for this information until the user enters 0 in place of their
>#include <stdio.h>
>#include <conio.h>
>int main() {
> int num, i;
> char lastname[27];
> float tempnum;
> float purchase[99];
> printf("Customer number:");
> scanf("%d", &num);
> do{
> if ((num>0)&& (num<100)){
> printf("1st letter: ");
> getche();

Is there some reason you could not use the standard function

> lastname[27] = getche();

This invokes undefined behavior. lastname is an array of 27 char.
the valid elements are lastname[0] through lastname[26]. lastname[27]
does not exist. The most probable result is that you have stepped on
another variable in your program. When you later update that
variable, it will destroy what you thought you saved here.

> printf("\n");
> printf("Amount of purchase: $");

The absence of a '\n' means that this may not be displayed until the
system has some reason to flush the output buffer. If you want to
omit the '\n' to allow the subsequent input to remain on the same
line, insert the statement
here. This will force the system to flush the buffer.

> scanf("%f", &tempnum);
> purchase[100] = tempnum;

More undefined behavior for the same reason. Did you mean to use

> printf("\n");

On most desktop systems, when the user enter the purchase amount
followed by the ENTER key, your display will echo the input, including
the newline implied by the ENTER key.

> printf("Customer number:");
> scanf("%d", &num);

Please learn to indent consistently. It is one of the best return on
investments you will ever see.

> }
> }while(num!=0);
> do{
> printf("Enter Number of the customer you want to
> display:");

Is this really two lines in your program? How did you ever get it to
compile? Did you cut and paste your source code or did you retype it?

> scanf("%d", &num);
> if(num!=0){
> printf("This is the 1st letter of their last name: %
> c\n", lastname[num]);

You never stored anything in lastname[num]. In fact, since num can
range from 1 to 99, lastname is too small. Why did you size it at 27?

> printf("This is the last amount of their purchase %d:
> \n", purchase[num]);

You also never stored anything in purchase[num].

> printf("\n");
> }
> }while(num!=0);
>return 0;
>This problem is the else if(num!=0) part. When I enter a number that
>doesn't equal 0 it's supposed to reveal the 1st letter of the
>customer's last name & amount of their purchase. I can't get this
>program to display the 2 arrays I have correctly.

<<Remove the del for email>>