Reputation: 89
int substance[5]={0.15, 0.8, 35.3, 401, 46};
printf("Please select the substance: (1)Oak (2)Glass (3)Lead (4)Copper (5)Steel)");
scanf("lf%", substance[0])
In the array, I have the data for different material,
I want the user to type 1-5 and select one of the material. say, user select 2, the the following calculation will use number 0.8
Upvotes: 0
Views: 71
Reputation: 35154
There are some issues.
First, your substance
-array should contain double
-values, so its type should be double
, too (not int
).
Second, if you ask the user for input, store the input in a separate variable (not in the substance
-array).
Try the following and use a debugger to see how a program behaves:
double substance[5]={0.15, 0.8, 35.3, 401, 46};
printf("Please select the substance: (1)Oak (2)Glass (3)Lead (4)Copper (5)Steel)");
int position=-1;
scanf("%d", &position);
double actualSubstance = -1;
if (position > 0 && position <= 5) {
actualSubstance = substance[position-1];
}
// actualSubstance will contain the selected substance, or -1 if an invalid selection has been taken
Upvotes: 3