views:

79

answers:

3

I must be going insane. This is incredibly simple so I am apparently overlooking something:

Here is my code:


int salesarray[20];    
scanf("%d",&sales_input);    
printf("sales_input is %d",sales_input);    
salesarray[i] = sales_input;    
printf("salesValue is %d",i,salesarray[i]);

Here is what I will see:

sales_input is 2salesValue is 1

Can anyone explain why my array is not being updated properly? salesValue should be changing to the value 2. Or I am reading it wrong here...

+5  A: 

Fix this:

printf("salesValue is %d",i,salesarray[i]);

shouldn't it be?

printf("salesValue is %d", salesarray[i]);
AlexKR
What is wrong with it? I'm sorry -- its 3:36 AM, I must not be seeing something here.
BSchlinker
@BSchlinker, you want to output i or salesarray[i] ?
AlexKR
+2  A: 
printf("salesValue is %d",i,salesarray[i]);

is missing a %d (you are not printing the salesarray value at all):

printf("salesValue is %d %d",i,salesarray[i]);

Try compiling with -Wall -Werror to make warnings into errors. -Werror would have showing you the problem from the beginning

laura
A: 

Do you want to print i or salesarray[i]?
You only have a single %d in the printf "format string" ...

printf("salesValue is %d",i,salesarray[i]);
/*                        ^ ^^^^^^^^^^^^^ */
pmg