Thread: Little help to complete my program..pls..

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    19

    Question Little help to complete my program..pls..

    Code:
    i ve completed my program..i can run the program and need minor changes to make it perfect..
    i. i need to show the quantity chosen next to menu item output is: 
    enter selection :2
    enter quantity 3:
    current output --> Plain egg $ 2.50
    it should look like this --> 3  Plain egg $2.50
    
    2. If the selection is more than 8..if 9 is entered
    output is : 
    Selections must be between 1 and 8
                                                   $0.00
    why this $0.00 is shown here?
    
    3. Finally, the output should look like this:
    
    THE OUTPUT WANTED LIKE THIS :
    Welcome to xxxxxx
    1  orange		$2.95
    1  apple		$3.45
    2  Tea	                $3.60
    sub total                 
    Tax             	
    total	                
    
    CURRENT OUTPUT IS :
    
    Welcome to xxxxxx
    Enter selections :
    Enter quantity :
    x            orange  2.95
    Enter selections :
    Enter quantity :
    x           apple   3.45
    
    sub total                 
    Tax             	
    total
    
    
    I NEED TO CHANGE IT TO WANTED OUTPUT...
    
    void showMenu(menuItemType menuList[MENU_SIZE], int menuCount)
    {
      int index;
    
      
      printf("Enter the number of your selections until you are finished, then enter -1.\n");
    
      
    
      
      for(index = 0; index < menuCount; ++index)
      {
          printf("%d - %20s %.2f\n", index+1, menuList[index].menuItem, menuList[index].menuPrice);
      }
    }
    
    void printCheck(menuItemType menuList[MENU_SIZE], int menuCount)
    {
       int selections = 0;
       int quantity = 0;
       double totalPrice = 0;
       double totalTax;
    
       printf("Welcome to xxxxxx\n");
    
       while(selections != -1)
       {
           printf("Enter selection: ");
    
    
           scanf("%d", &selections);
           if (selections == -1)
           {
               break;
           }
           printf("Enter Quantity : ");
           scanf("%d", &quantity);
    
           if (selections >= 1 && selections <= menuCount)
           {
    
    --selections;
               printf("2d %20s %.2f\n", menuList[selections].menuItem, menuList[selections].menuPrice);
               totalPrice += (menuList[selections].menuPrice * quantity);
    
    
            }
           else
           {
               printf("Selections must be between 1 and %d\n", menuCount);
           }
    
    
    
       }
       totalTax = (totalPrice * GST);
       printf("Sub total %.2f\n", totalPrice);
       printf("Tax %.2f\n", totalTax);
       printf("Total %.2f\n", totalPrice + totalTax);
    
    }
    Last edited by scorpio76; 02-24-2011 at 02:45 AM. Reason: submitting part of program

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, all you need is to add the closing bracket after the first [CODE ==> needs a ] after the E.

    I strongly suggest you edit your post, by highlighting all the code, and then clicking on the # icon in the advanced editor window.

    Code not inside code tags tends to be ignored.
    Last edited by Adak; 02-23-2011 at 06:10 AM.

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by scorpio76 View Post
    1. i need to show the quantity chosen next to menu item output is:
    enter selection :2
    enter quantity 3:
    current output --> Plain egg $ 2.50
    it should look like this --> 3 Plain egg $2.50
    Change your printout to something like this, then.
    Code:
    printf("%2d %20s %.2f\n", quantity, menuList[selections].menuItem, menuList[selections].menuPrice);
    Quote Originally Posted by scorpio76 View Post
    2. If the selection is more than 8..if 9 is entered
    output is :
    Selections must be between 1 and 8
    $0.00
    why this $0.00 is shown here?
    Because the following code is executed regardless of selections validity. You should rethink your control flow.
    Code:
    --selections;
    printf("%20s %.2f\n", menuList[selections].menuItem, menuList[selections].menuPrice);
    totalPrice += (menuList[selections].menuPrice * quantity);
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    tq msh..it works now...but my ques 3..couldn't get that output after i adjust the lines..

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    What does your output look like now? Post your updated code -- with proper code tags this time.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    Ok i did little changes to incorporate question 1 and 2..now my problem is with ques 3..the output should look like wanted output not the current one...tq

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You're printing out after user enters each selection. Instead, you should only print out at the end, when the user has finished their selection.

    Consider the following code. If you have questions, ask here.
    Code:
    /* maximum number of dynamic lines in receipt */
    #define MAXRECL 10
    
    /* maximum receipt line length in characters, null-inclusive */
    #define MAXSTRLEN 30
    
    /* allocate memory to hold pointers to receipt lines */
    char **rec_texts = malloc(MAXCQL * sizeof(*rec_texts));
    
    /* keep a counter of lines in receipt */
    int line_ctr = 0;
    
    /* function to compose a receipt line, returns NULL if there were issues */
    char* compose(int item_qty, char *item_descr, double item_value)
    {
      /* allocate memory for a new line */
      char *buffer = malloc(MAXSTRLEN * sizeof(*buffer));
      if (buffer == NULL)
        return buffer;
    
      /* compose receipt line from given arguments */
      sprintf(buffer, "%2d%-20s$%.2f", item_qty, item_descr, item_value);
    
      return buffer;
    }
    
    #############
    
    /* replace your current printing block with this */
    if (selections >= 1 && selections <= menuCount)
    {
      --selections;
    
      rec_texts[line_ctr] = compose(quantity, menuList[selections].menuItem, menuList[selections].menuPrice);
      ++line_ctr;
      totalPrice += (menuList[selections].menuPrice * quantity);
    }
    
    /* and print out all lines and totals at the end like so */
    totalTax = (totalPrice * GST);
    
    /* print all lines composed */
    int i;
    for (i = 0; i < line_ctr; ++i)
      printf(rec_texts[i]);
    
    printf("Sub total %.2f\n", totalPrice);
    printf("Tax %.2f\n", totalTax);
    printf("Total %.2f\n", totalPrice + totalTax);
    
    
    /* at the end of the program, before returning, free all allocated memory */
    for (i = 0; i < line_ctr; ++i)
      free(rec_texts[i]);
    
    free(rec_texts);
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The output is simply working with the printf() details. If you need some spaces between columns, put them right into the printf() line:
    Code:
    printf("\n  %2d     %20s     $%5.2f", num, item, price);
    Adjust the spaces between the control formats, and the 2, 20, and 5.2, until you get the exact alignment you need.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    i dont understand from #define MAXRECL to return buffer...is this line to replace.. void printCheck(menuItemType menuList[MENU_SIZE], int menuCount)??

    i already have a getData(menuItemType menuList[MENU_SIZE])..is this will efeect the whole program..

  10. #10
    Novice
    Join Date
    Jul 2009
    Posts
    568
    His issue is that user input is interspersed with output. He wants to print the receipt at the end.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  11. #11
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by scorpio76 View Post
    i dont understand from #define MAXRECL to return buffer...is this line to replace.. void printCheck(menuItemType menuList[MENU_SIZE], int menuCount)??

    i already have a getData(menuItemType menuList[MENU_SIZE])..is this will efeect the whole program..
    This is additional code you have to add to yours.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  12. #12
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Found a bug in my code. This is correct.
    Code:
    /* allocate memory to hold pointers to receipt lines */
    char **rec_texts = malloc(MAXRECL * sizeof(*rec_texts));
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    all the lines added to the program...but seems to have compiling errors..
    menucount is never used in function,
    menuList never used in fucntion,
    totalPrice assigned a value never used in fucntion printcheck..and some expression syntax in function printcheck

  14. #14
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Post the current version of your code and the errors you get.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  15. #15
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    menucount is never used in function,
    menuList never used in fucntion,
    totalPrice assigned a value never used in fucntion printcheck
    Those sound like warnings, not errors, are the warnings being generated due to an error that is listed prior to this? due to this non-descriptive line?
    and some expression syntax in function printcheck
    Put a sensible section of the actual build messages in your post.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. Replies: 3
    Last Post: 03-21-2004, 06:02 PM
  3. Pls explain how this program works...
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-05-2002, 09:53 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM