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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sger9
    New Member
    • Jan 2019
    • 1

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

    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",&pay ment);}
    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--); {
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Please enclose your source code in [CODE/] tags. This preserves your indentation and numbers each line.

    Code:
    #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--); {
            }

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      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.

      Comment

      Working...