Thread: this is what happens when i enter zero.help me please

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    8

    this is what happens when i enter zero.help me please

    this is the code but i don't understand why it breaks immediately when i enter zero it must ask the user for yes or no. i used scanf for it.i need help only for the if(x==0) part(starting with line 104) you dont need to look at the rest of the code thank you
    Code:
    #include <stdio.h>int main()
    {
    int x;
    char y,n,a;
    float z,e,p;
    float sum=0;
    printf("****************Online Shopping*****************\n");
        printf("1- Apple    5$\n");
        printf("2- Orange    7$\n");
        printf("3- Tomate    9$\n");
        printf("4- Meat       50$\n");
        printf("5- Chicken    25$\n");
        printf("6- Milk       2.5$\n");
        printf("7- Yoghurt    4.2$\n");
        printf("8- Egg       1.1$\n");
        printf("9- Lemonate    1.9$\n");
        printf("10-Coke      2.3$\n");
        printf("************************************************\n");
            while(x!=-1 && x!=0){
                     scanf("%d",&x);
                    if(x!=-1 && x!=0){
            scanf("%f",&z);
                    if (x == 1){
                          
                
                    printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Apple: %.2f$\n", z, 5 * z);
                    sum += 5 * z;}
                
                if (x == 2){
                
                    printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Orange: %.2f$\n", z, 7 * z);
                    sum += 7 * z;}
                
                if (x == 3){
                
                    printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Tomate: %.2f$\n", z, 9 * z);
                    sum += 9 * z;}
                
                if (x == 4){
                
                    printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Meat: %.2f$\n", z, 50 * z);
                    sum += 50 * z;}
                
                if (x == 5){
                
                    printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Chicken: %.2f$\n", z, 25 * z);
                    sum += 25 * z;}
                
                if (x == 6){
                
                    printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f lt Milk: %.2f$\n", z, 2.5*z);
                    sum += 2.5*z;}
                
                if (x == 7){
                
                    printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Yoghurt: %.2f$\n", z, 4.2*z);
                    sum += 4.2*z;}
                
                if (x == 8){
                
                    printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f box Egg: %.2f$\n", z, 1.1*z);
                    sum += 1.1*z;}
                
                if (x == 9){
                
                    printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f lt Lemonade: %.2f$\n", z, 1.9*z);
                    sum += 1.9*z;
                                     }
                     
                                     }
                     if (x ==-1){
            printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): -1\n");
                               scanf("%f",&e);
                            printf("Your total price is %.2f$. How much money you enter: %.2f$\n",sum,e);
                   
    
    
                
                          
                            if(e>=sum){
                        printf("Total price has been paid. Your remaining money %.2f$\n",e-sum);
                printf("Have a good day...");
                               break;
                             }
                              if(e<sum){
                                  scanf("%f",&p);
                                  printf("Your money is not enough. You must add %.2f$ for total price. How much money you enter: %.2f$\n",sum-e,p);
                                   e+=p;
                
                           
                          
                              
                              
                               if(e>=sum){
                            printf("Total price has been paid. Your remaining money %.2f$.\n",e-sum);
                printf("Have a good day...");
                             break;
                             }
                          
    
    
                      }
                         
                        if(x==0){
        
                scanf("%c",&a);
                if(a=='y'){
                printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): Are you sure (y/n) : y\n");
                printf("Have a good day");
                break;
                }
                if(a=='n'){
                printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): Are you sure (y/n) : n\n");
                continue;
                } 
            
                }
                           }   
                }
                
             return 0;
    
                           }
    Attached Images Attached Images this is what happens when i enter zero.help me please-ss1-jpg 
    Last edited by kyper78; 11-02-2018 at 10:58 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,648
    Did a monkey indent your code?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Your indentation needs work.
    Code:
    #include <stdio.h>
    int main()
    {
      int x;
      char y, n, a;
      float z, e, p;
      float sum = 0;
      printf("****************Online Shopping*****************\n");
      printf("1- Apple    5$\n");
      printf("2- Orange    7$\n");
      printf("3- Tomate    9$\n");
      printf("4- Meat       50$\n");
      printf("5- Chicken    25$\n");
      printf("6- Milk       2.5$\n");
      printf("7- Yoghurt    4.2$\n");
      printf("8- Egg       1.1$\n");
      printf("9- Lemonate    1.9$\n");
      printf("10-Coke      2.3$\n");
      printf("************************************************\n");
      while (x != -1 && x != 0) {
        scanf("%d", &x);
        if (x != -1 && x != 0) {
          scanf("%f", &z);
          if (x == 1) {
            printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Apple: %.2f$\n",
                 z, 5 * z);
            sum += 5 * z;
          }
          if (x == 2) {
            printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Orange: %.2f$\n",
                 z, 7 * z);
            sum += 7 * z;
          }
          if (x == 3) {
            printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Tomate: %.2f$\n",
                 z, 9 * z);
            sum += 9 * z;
          }
          if (x == 4) {
            printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Meat: %.2f$\n",
                 z, 50 * z);
            sum += 50 * z;
          }
          if (x == 5) {
            printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Chicken: %.2f$\n",
                 z, 25 * z);
            sum += 25 * z;
          }
          if (x == 6) {
            printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f lt Milk: %.2f$\n",
                 z, 2.5 * z);
            sum += 2.5 * z;
          }
          if (x == 7) {
            printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Yoghurt: %.2f$\n",
                 z, 4.2 * z);
            sum += 4.2 * z;
          }
          if (x == 8) {
            printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f box Egg: %.2f$\n",
                 z, 1.1 * z);
            sum += 1.1 * z;
          }
          if (x == 9) {
            printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f lt Lemonade: %.2f$\n",
                 z, 1.9 * z);
            sum += 1.9 * z;
          }
        }
        if (x == -1) {
          printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): -1\n");
          scanf("%f", &e);
          printf("Your total price is %.2f$. How much money you enter: %.2f$\n",
                 sum, e);
          if (e >= sum) {
            printf("Total price has been paid. Your remaining money %.2f$\n",
                   e - sum);
            printf("Have a good day...");
            break;
          }
          if (e < sum) {
            scanf("%f", &p);
            printf("Your money is not enough. You must add %.2f$ for total price. How much money you enter: %.2f$\n",
                 sum - e, p);
            e += p;
            if (e >= sum) {
              printf("Total price has been paid. Your remaining money %.2f$.\n",
                     e - sum);
              printf("Have a good day...");
              break;
            }
          }
          if (x == 0) {
            scanf("%c", &a);
            if (a == 'y') {
              printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): Are you sure (y/n) : y\n");
              printf("Have a good day");
              break;
            }
            if (a == 'n') {
              printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): Are you sure (y/n) : n\n");
              continue;
            }
          }
        }
      }
    
      return 0;
    }
    Your variable names are awful.
    Use descriptive names like productID, productQuantity, ProducePrice.

    Now with better indentation, it's clear to see that the x==0 on line 93 just isn't going to happen because the x==-1 on line 70 is a guard for it.
    That is, if you ever make it past line 70, then line 93 will always evaluate to false.

    Perhaps a brace is in the wrong place.

    Also, since you're mixing %c with other formats, it's a good idea to have a leading space in the format, like so
    Code:
            scanf(" %c", &a);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User enter text and save to text file (end enter text using -1)
    By DecoratorFawn82 in forum C Programming
    Replies: 7
    Last Post: 12-28-2017, 04:23 PM
  2. Replies: 9
    Last Post: 02-22-2009, 11:50 PM
  3. Enter?
    By alexnb185 in forum C Programming
    Replies: 9
    Last Post: 08-13-2007, 01:54 PM
  4. Enter KEY
    By HaCkEr TeStEr in forum C Programming
    Replies: 14
    Last Post: 12-05-2006, 07:05 AM
  5. Get Enter Key
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 05:21 AM

Tags for this Thread