By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,583 Members | 3,481 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,583 IT Pros & Developers. It's quick & easy.

Another vending machine problem. This is only the first part tho

P: 1
This is the first part of a program for school. am only trying to get the first part working yet but am not having any joy. Any pointers where im gone wrong


#include <stdio.h>

int main(void){
/* int set [5] = {A1, A2, A3, A4, A5};*/
int i;
int A=0.90;
int B=1.20;
int C=1.10;
int D=0.75;
int E=1.01;
int selection;
int cost;
int payment;
int change;
int stock;

printf(" A B C D E\n ");
printf(" Mars Bounty Curly Wurly Toffee Crisp Double Decker\n ");
printf(" 2 2 2 2 2\n ");
printf(" 0.90 1.20 1.10 0.75 1.01\n");
printf("Please make your selection from the list above \n");
scanf("%d", &selection);

if (selection==A) {
printf("You have selected Mars. Please enter how much money you are inputing\n");
scanf("%d",&payment);}
if (payment == 0.90) {
printf("Please take your selection and thank you for your custom\n");}
else if (payment < 0.90){
printf("Please add some more coins \n");}
else if (payment > 0.90) {
payment - 0.90 == change;}
for (stock = 2; stock<2; stock--); {
}
Jan 5 '19 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 2,400
Please enclose your source code in [CODE/] tags. This preserves your indentation and numbers each line.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3.  int main(void){
  4.     /* int set [5] = {A1, A2, A3, A4, A5};*/
  5.     int i;
  6.     int A=0.90;
  7.     int B=1.20;
  8.     int C=1.10;
  9.     int D=0.75;
  10.     int E=1.01;
  11.     int selection;
  12.     int cost;
  13.     int payment;
  14.     int change;
  15.     int stock;
  16.  
  17.     printf(" A B C D E\n ");
  18.     printf(" Mars Bounty Curly Wurly Toffee Crisp Double Decker\n ");
  19.     printf(" 2 2 2 2 2\n ");
  20.     printf(" 0.90 1.20 1.10 0.75 1.01\n");
  21.     printf("Please make your selection from the list above \n");
  22.     scanf("%d", &selection);
  23.  
  24.     if (selection==A) {
  25.         printf("You have selected Mars. Please enter how much money you are inputing\n");
  26.         scanf("%d",&payment);}
  27.     if (payment == 0.90) {
  28.         printf("Please take your selection and thank you for your custom\n");} 
  29.     else if (payment < 0.90){
  30.         printf("Please add some more coins \n");}
  31.     else if (payment > 0.90) {
  32.         payment - 0.90 == change;}
  33.     for (stock = 2; stock<2; stock--); {
  34.         }
Jan 17 '19 #2

Expert 100+
P: 2,400
  1. Line 4. This comment is confusing because there is no set array.
  2. Lines 6-10. These variables are declared int but they are assigned floating point values.
  3. Line 20. I don't know if "" can be used in a string constant. Maybe it is ok.
  4. Line 22. You input an integer value into an int variable but you prompted the user to enter a floating point value. Probably should change format string and variable type.
  5. Line 24. You compare [probably] floating point values for equality. That won't work well. Refer to What Every Computer Scientist Should Know About Floating-Point Arithmetic.
  6. Line 26. (same comment as for line 22)
  7. Line 27. (same comment as for line 24)
  8. Line 32. "payment - 0.90" is an rvalue. It belongs to the right of the equals sign. "==" is a logical operator, you want the assignment operator "=".
  9. Line 33. You initialize stock to 2 and then loop while stock is less than 2. 2 is not less than 2 so it won't loop.
  10. Line 33. The semicolon terminates the loop so the brackets on lines 33 and 34 are not part of the loop.
  11. Looks like the rest of the program (return statement and closing brace) are missing.
Jan 17 '19 #3

Post your reply

Sign in to post your reply or Sign up for a free account.