#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Help in making my do..while loop work,


    i'm trying to make a simple cashier program, and my logic here was very plain.

    my problem was, my loop was not working. after doing the stuff inside "do" , my program will terminate, but i believe my condition inside "while" was correct... so i'm getting frustrated...

    but it's kind of long so i'm very hesitant to put it here, im afraid people will get a headache looking at my ugly codes T^T but, i still wanted to give it try, cause i really can't find any error in my program, so here it is:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string>
    #include <stdlib.h>
    
    using namespace std;
    
          struct record
          {
                 string pname;
                 float price, total;
                 int qty;
          } order[10];
          
          
          struct choices
          {
                 string pname;
                 float price;
          };
    
          choices product[3] = {
                  
                               {"PORK", 100},
                               {"SHRIMP", 200},
                               {"BEEF", 300},
                               
                               };
                               
          float total_bill, totali, change;
          int ctr=1, choice, qty, a, amount;
          char ans; 
         
          
          
    main()
    {
        do
        {  
            printf("MENU: \n\n");
            printf("(1) Pork - P100\n");
            printf("(2) Shrimp - P200\n");
            printf("(3) Beef - P300\n");
    
            printf("\nPlace Order[%d] : ", ctr);
            scanf("%d", &choice);
            printf("\nQuantity : ");
            scanf("%d", &qty);
    
            totali = (product[choice-1].price) * qty;
                        
                     order[ctr].pname = product[choice-1].pname;
                     order[ctr].price = product[choice-1].price;
                     order[ctr].total = totali;
                     order[ctr].qty = qty;
                     
                     ctr++; 
                     
            printf("Finished Ordering? Y/N:\n");
            scanf("%c", &ans);
    
            system ("pause");  //i even tried putting this everywhere because i thought maybe it's looping too fast or something T^T
          
        } while ( (ans=='N') || (ans=='n') );
        
      
        if ( (ans=='Y') || (ans=='y') )
        {
            for (a=1; a<=ctr; a++)
            {   system ("pause"); 
                printf("Order No. %d", a);
                printf("Product name: %c", order[a].pname.c_str());
                printf("Quantity: %d", order[a].qty);
                printf("Price: %.2f", order[a].price);
                printf("Total: %.2f", order[a].total);
    
                total_bill = (order[a].total) + total_bill;
                system ("pause");
            }
                printf("Total Bill: %d", total_bill);
                printf("\nAmount Tendered:");
                scanf("%d", amount);
                change = (amount - total_bill);
                printf("\nChange: %.2f", change);
                system ("pause");
                
            //for receipt  in notepad form   
            FILE*fp;
                fp=fopen("RECEIPT.txt","a");
                fprintf(fp, "\nTOKYO TOKYO\n");
                fprintf(fp, "\n--------------------\n");
    
            for (a=1; a<=ctr; a++)
            {    
                fprintf(fp, "Order No. %d", a);
                fprintf(fp, "Product name: %c", order[a].pname.c_str());
                fprintf(fp, "Quantity: %d", order[a].qty);
                fprintf(fp, "Price: %.2f", order[a].price);
                fprintf(fp, "Total: %.2f", order[a].total);
            }
                fprintf(fp, "Total Bill: %d", total_bill);
                fprintf(fp, "\nChange: %.2f", change);
                fprintf(fp, "\nThank you!!\n");            
        }
      return 0;
        system("pause");
    }

    Comments on this post

    • b49P23TIvg agrees : I disagree---your post is beautiful.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    If I'm not mistaken, here is your problem:
    scanf("%c", &ans);

    You need to understand, so the explanation first:
    Previous to this scanf, you performed this scanf:
    scanf("%d", &qty);

    The way that scanf works is that it scans through the keyboard input, one character at a time, until it finds a match for the specification ("%d"). And then it stops. Anything else in the input buffer is still in the input buffer.

    Now, for historical reasons (four decades ago, when C was created, most all terminals were line-oriented, so that they didn't send anything until the user had hit the Enter key), Standard C Library functions (such as scanf) do not even start to process input until the Enter key has been hit. So, when you did that scanf("%d", &qty);, when it finished the input buffer still contained the new-line (AKA '\n', AKA '\x0a\x0d', AKA whatever your particular operating system uses as a new-line). So when you told scanf to read in the next character, no matter what it is (as per scanf("%c", &ans);), it read in that first new-line character, which satisfied the scanf call. Since '\x0a' is neither 'N' nor 'n', you got unexpected results.

    Because you wrote that last scanf thus:
    scanf("%c", &ans);
    you are ignoring any user input. Here's a very important concept for you to learn: white space. Those are meaningless (except as delimiters) characters like spaces, tabs, new-lines. Normally, you would want to ignore white space, but if you write a scanf like you did, then you are not ignoring white space.

    Clearly, what you want to do is to ignore white space and only want to input a printable character. Here is how you do that:
    scanf(" %c", &ans);
    Please notice the space before the "%c". That tells scanf to expect white space before the character that you want to input. That doesn't tell it how many white space characters to expect, more than one, or even zero. But it will ignore any and all new-lines, tabs, spaces, whatever, that may come before that meaningful character that the user has input.

    Think about it. Remember it. Use it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Red face


    omg. thank you so much for your reply, it means a lot! after reading it i applied it right away, and it worked. now i finally got to deal with all the mistakes in my program, thank you so much!!! my knowledge in C++ was very limited that even the simplest/ most basic rules i was sooooo clueless. thank you so much for your time! T^T

IMN logo majestic logo threadwatch logo seochat tools logo