3
\$\begingroup\$

I would like to hear some feedback about style on my program, and which one is better "do while" or "while loop" to run my program.

#include <stdio.h>
#include <stdlib.h>


#define Tequila 20
#define Whiskey 25




int main()
{


    int choice = 0;
    int bottle1= 0;
    int bottle2= 0;
    int final_total= 0;
    int total1 =0;
    int total2 =0;

    do{
        printf("\tMain Menu MK liquor Store\n");
        printf(" \tEnter your choice: \n");
        printf("\t 1-  Buy Tequila Jose Cuervo\n");
        printf("\t 2-  Buy Whiskey Jack Daniels\n");
        printf("\t 3-  Quit\n");

        scanf("%d", &choice);



        switch(choice)
        {
            case 1:
                printf("how many bottles of Tequila would you like\n");
                scanf("%d", &bottle1);
                printf("so far you got %d bottles in your basket \n", bottle1);

                break;
            case 2:
                printf("how many bottles of Whiskey would you like\n");
                scanf("%d", &bottle2);
                printf("so far you got %d bottles in your basket\n", bottle2);


                break;
            case 3:
                total1=bottle1 * Tequila;
                total2= bottle2 * Whiskey;
                final_total = total1+total2;
                printf("You obtained %d bottles of Tequila and %d of Whisky\n", bottle1,bottle2);
                printf("Your total  is %d dollars\n",final_total);

                break;
            default:

                printf("ERROR INVALID OPTION.\n\n");

                break;

        }

    }while (choice != 3);
        return 0;

    }
\$\endgroup\$
  • \$\begingroup\$ As far as "do-while" vs "while" loop, I would suggest reading this SO question. \$\endgroup\$ – esote Sep 18 '17 at 4:14
  • 1
    \$\begingroup\$ @Jamal given that the title is now "Menu for purchasing tequila and whiskey", I wondered whether I was on Seasoned Advice. And I really hope that the title does not apply to too many questions :D. \$\endgroup\$ – Zeta Sep 18 '17 at 5:09
1
\$\begingroup\$

I would like to hear some feedback about style on my program,

The vertical spacing is excessive.

Odd separation example: blank lines between case code printf("so far you got %d bottles in your basket\n", bottle2); and break; misleads code blocks. Consider instead;

        case 2:
            printf("how many bottles of Whiskey would you like\n");
            scanf("%d", &bottle2);
            printf("so far you got %d bottles in your basket\n", bottle2);
            break;

        case 3:

{ at line end or next line: Stay consistent

do{
...
    switch(choice)
    {                  // why here vs above line

I'd expect following to be less indented.

       return 0;

It is apparent code is not written using an auto formatter and that is the biggest style problem. Any style worth using will match an auto formatter. Manually formatting is not productive and too inconsistent.


and which one is better "do while" or "while loop" to run my program.

Use of do() is OK here as the loop should always execute at least once. Some style guides eschew do(). As will such styles issues, follow your group's coding rules.


Style aside, code not checking the result of an input function like scanf("%d", &choice); is weak.

// better
if (scanf("%d", &choice) != 1)  {
  break;
}
\$\endgroup\$
  • \$\begingroup\$ Thank you Chux Do you know , how can I activate the auto formatter in Codeblocks \$\endgroup\$ – Mike Sep 18 '17 at 18:45
  • 1
    \$\begingroup\$ @Mike I am not familiar with Codeblocks. You may need a 3rd party tool. Tip: It is worth the time to learn how to let automatic tools take care of the irksome task of formatting code. \$\endgroup\$ – chux - Reinstate Monica Sep 18 '17 at 18:46
  • \$\begingroup\$ One more thing. The program has an issue when the user type something that is not an interger, it keeps rolling the program without stop. Any recommendations Thanks \$\endgroup\$ – Mike Sep 18 '17 at 20:25
  • 1
    \$\begingroup\$ The robust solution is to read all user input with fgets() and then parse it with sscanf(), strtol(), etc. A cheap solution is to getchar() a character when scanf("%d"... fails. \$\endgroup\$ – chux - Reinstate Monica Sep 18 '17 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.