C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 12th, 2012, 01:19 AM
aieenaein aieenaein is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 aieenaein User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 36 sec
Reputation 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.

Reply With Quote
  #2  
Old September 12th, 2012, 01:53 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 24 m 11 sec
Reputation Power: 1974
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.

Reply With Quote
  #3  
Old September 12th, 2012, 05:49 AM
aieenaein aieenaein is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 aieenaein User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 36 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Help in making my do..while loop work,

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap