Reputation: 9
I am fairly new at coding in C. I am trying to get the project below to run but it says my program has stopped working after I enter the third input. I am not confident that my choice of using a while loop with all or booleans is the correct choice of approaching this. I almost feel like there should be an if/else statement added to the program somewhere.
I have been provided a flow chart for the project that is basically along these lines:
Get input values for inv1-4
While code !=-1
if code is !=1 proceed with loop and get input values for amounts bought/sold
else print inv1-4
#include <stdio.h>
int main(void)
{
int inv1;
int inv2;
int inv3;
int inv4;
int amount_purchased;
int amount_sold;
printf("Please provide beginning inventory amounts in cases between 1 and 4.\n\n");
printf("Enter the number of inventory for Piels (ID number 1): ");
scanf("%d", &inv1);
printf("\nEnter the number of inventory for Coors (ID number 2): ");
scanf("%d", &inv2);
printf("\nEnter the number of inventory for Bud (ID number 3): ");
scanf("%d", &inv3);
printf("\nEnter the number of inventory for Iron City (ID number 4): ");
scanf("%d", &
inv4);
while (inv1 != -1 && inv2 != -1 && inv3 != -1 && inv4 != -1) {
printf("\nEnter the number of cases of Piels (ID 1) purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv1 = inv1 + amount_purchased - amount_sold;
printf("\nEnter the number of cases of Coors (ID 2) purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv2 = inv2 + amount_purchased - amount_sold;
printf("\nEnter the number of cases of Bud (ID 3)purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv3 = inv3 + amount_purchased - amount_sold;
printf("\nEnter the number of cases of Iron City (ID 4) purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv4 = inv4 + amount_purchased - amount_sold;
}
printf("Ending inventory is as follows.\n\n");
printf("Piels (ID Number 1): %d \n", inv1);
printf("Coors (ID Number 2): %d \n", inv2);
printf("Bud (ID Number 3): %d \n", inv3);
printf("Iron City (ID Number 4): %d \n", inv4);
return 0;
}
Upvotes: 0
Views: 92
Reputation: 119
You have some basic syntax problems like
scanf("%d", &inv1)
instead of
scanf("%d", inv1)
The while loop syntax is also wrong.
while(inv2 != -1 && inv2 != -1 && inv3 != -1 && inv4 != -1)
instead of
while (inv1 || inv2 || inv3 || inv4 != -1)
And another logic issue. The while loop will run once. Because you have written the "break" statement at the end of the loop. So there is no need for any loop at all. Just write without loop
printf("\nEnter the number of cases of Piels (ID 1) purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv1 = inv1 + amount_purchased - amount_sold;
printf("\nEnter the number of cases of Coors (ID 2) purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv2 = inv2 + amount_purchased - amount_sold;
printf("\nEnter the number of cases of Bud (ID 3)purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv3 = inv3 + amount_purchased - amount_sold;
printf("\nEnter the number of cases of Iron City (ID 4) purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv4 = inv4 + amount_purchased - amount_sold;
It will work the same if it is what you meant to do.
Upvotes: 1
Reputation: 13570
I guess the problem with this
while (inv1 || inv2 || inv3 || inv4 != -1)
is that what you wrote is not what you expect. (inv1 || inv2 || inv3 || inv4 != -1)
evaluates to true when
inv1
is not 0, or inv2
is not 0, or inv3
is not 0, or inv4
is not -1. What you probably want is if either one of these variables
is -1, the loop must end. The correct condition would be in that case:
while(inv2 != -1 && inv2 != -1 && inv3 != -1 && inv4 != -1)
{
...
}
Note that in boolean algebra a && b
is equivalent to !a || !b
. And a || b
is equivalent to !a && !b
.
Also your scanf
s are wrong as many people wrote in the comments. You have to
pass a pointer to int
, not an int
. So instead of
scanf("%d", amount_sold);
it should be
scanf("%d", &amount_sold);
and this applies to all your scanf
calls, otherwise it's undefined behaviour
and the result of undefined behaviour is undefined.
edit
I just noticed that you have an unconditional break
at the end of the loop. If
you are leaving the loop anyway in the first iteration, why do you do the loop
in the first place? Or did you forget to add some if(condition)
for the
break
?
Upvotes: 2