Thread: What is your opinion of this?

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    6

    What is your opinion of this?

    This is a simple Point of Sales program for my assignment.
    Do note my knowledge is limited with only 8 hours of formal training.
    This program works but I want to know if there's anything I can change to make it better like reducing additional steps or correcting bad programming practices.

    Thank you.

    Code:
    #ifdef _MSC_VER
    #define _CRT_SECURE_NO_WARNINGS
    #endif
    #include<stdio.h>
    #include<time.h>
    #include<string.h>
    
    
    int booklist();
    
    int main()
    {
        char passcode[8];
        const char id[][8] = { "1304914", "1304961", "1304772", "1306306", "1303166","1303447" };
        char title[][40] = { "Programming Vol.1 - Mr.Lim", "Programming Vol.2 - Mrs.Chooi", "Modern Physic and Wave - Mr.Lee",
            "Mathematics - Mr.Kang", "Inorganic Chemistry - Mr.Alex" };
        float price[6] = { 20.00f, 15.00f, 30.00f, 25.00f, 20.00f };
        float ttl_1 = 0, ttl_2 = 0, ttl_3 = 0, ttl_4 = 0, ttl_5 = 0, sub_ttl = 0, grand_ttl = 0, cash_given = 0, change = 0, discount_amt = 0;
        int  card_type, cash_or_credit=0, book_code, menu_code, qty_1 = 0, qty_2 = 0, qty_3 = 0, qty_4 = 0, qty_5 = 0, back_main = 0, check_code, switchstart = 0, member, do_exit, back_login;
        do
        {
            printf("\t\t\t============================= ");
            printf("\n\t\t\t|| Hatsune Miku Bookstores ||");
            printf("\n\t\t\t=============================\n\n");
        
            while 
                ((strcmp(id[0], passcode) != 0)&&
                (strcmp(id[1], passcode) != 0)&&
                (strcmp(id[2], passcode) != 0)&&
                (strcmp(id[3], passcode) != 0)&&
                (strcmp(id[4], passcode) != 0)&&
                (strcmp(id[5], passcode) != 0))
            {
                printf("Please Enter Your 7-digit Passcode: ");
                scanf(" %s[^/n]", &passcode);
                if ((strcmp(id[0], passcode) !=0)&&
                    (strcmp(id[1], passcode) != 0)&&
                    (strcmp(id[2], passcode) != 0)&&
                    (strcmp(id[3], passcode) != 0)&&
                    (strcmp(id[4], passcode) != 0)&&
                    (strcmp(id[5], passcode) != 0)
                   &nbsp;)
    
    
                {
                    printf("\nWrong ID Entered\n\n");
                    fflush(stdin);
            }
            } 
            system("cls");
            do
            {
                back_main = 0;
    
    
                if ((strcmp(id[0], passcode) == 0))
                {
                    printf("Welcome Jojo Lim");
                }
                else
                if ((strcmp(id[1], passcode) == 0))
                {
                    printf("Wecome Teoh Lee Seng");
                }
                else
                if ((strcmp(id[2], passcode) == 0))
                {
                    printf("Welcome Ho Jia Liang");
                }
                else
                if ((strcmp(id[3], passcode) == 0))
                {
                    printf("Welcome Kuhan");
                }
                else
                if ((strcmp(id[4], passcode) == 0))
                {
                    printf("Welcome Mahen");
                }
                else
                if ((strcmp(id[5], passcode) == 0))
                {
                    printf("Welcome Khugganesh");
                }
                printf("\n\n1. Book List");
                printf("\n2. Checkout");
                printf("\n3. Payment");
                printf("\n4. View Receipt");
                printf("\n\n5. Logout");
                printf("\n6. Exit");
                printf("\n\n7. About This Software");
                printf("\n\nPlease Enter A Code: ");
                scanf("%d", &menu_code);
                system("cls");
                do
                {
                    switch (menu_code)
                    {
                    case 1:
                        booklist();
                        
                        do
                        {
                            printf("\n\nPlease Enter A Code: ");
                            scanf("%d", &book_code);
    
    
                            if (book_code == 101)
                            {
                                printf("%-32s", title[0]);
                                do
                                {
                                    printf("\nPlease Enter the Quantity: ");
                                    scanf("%d", &qty_1);
                                    if (qty_1 > 0)
                                    {
                                        ttl_1 = (float)qty_1 * price[0];
                                        printf("Total: RM%.2f", ttl_1);
                                    }
                                    else
                                    {
                                        printf("\nError. Invalid Code Entered. Please Enter a Number >0\n");
                                        fflush(stdin);
                                    }
                                } while (qty_1 <= 0);
                                
                            }
                            else
    
    
                            if (book_code == 102)
                            {
                                printf("%-32s", title[1]);
                                do
                                {
                                    printf("\nPlease Enter the Quantity: ");
                                    scanf("%d", &qty_2);
                                    if (qty_2 > 0)
                                    {
                                        ttl_1 = (float)qty_2 * price[1];
                                        printf("Total: RM%.2f", ttl_2);
                                    }
                                    else
                                    {
                                        printf("\nError. Invalid Code Entered. Please Enter a Number >0\n");
                                        fflush(stdin);
                                    }
                                } while (qty_2 <= 0);
                            }
                            else
    
    
                            if (book_code == 103)
                            {
                                printf("%-32s", title[2]);
                                do
                                {
                                    printf("\nPlease Enter the Quantity: ");
                                    scanf("%d", &qty_3);
                                    if (qty_3 > 0)
                                    {
                                        ttl_3 = (float)qty_3 * price[2];
                                        printf("Total: RM%.2f", ttl_3);
                                    }
                                    else
                                    {
                                        printf("\nError. Invalid Code Entered. Please Enter a Number >0\n");
                                        fflush(stdin);
                                    }
                                } while (qty_3 < 0);
                            }
                            else
    
    
                            if (book_code == 104)
                            {
                                printf("%-32s", title[3]);
                                do
                                {
                                    printf("\nPlease Enter the Quantity: ");
                                    scanf("%d", &qty_4);
                                    if (qty_4 > 0)
                                    {
                                        ttl_1 = (float)qty_4 * price[3];
                                        printf("Total: RM%.2f", ttl_4);
                                    }
                                    else
                                    {
                                        printf("\nError. Invalid Code Entered. Please Enter a Number >0\n");
                                        fflush(stdin);
                                    }
                                } while (qty_4 <= 0);
                            }
                            else
    
    
                            if (book_code == 105)
                            {
                                printf("%-32s", title[4]);
                                do
                                {
                                    printf("\nPlease Enter the Quantity: ");
                                    scanf("%d", &qty_5);
                                    if (qty_5 > 0)
                                    {
                                        ttl_1 = (float)qty_5 * price[4];
                                        printf("Total: RM%.2f", ttl_5);
                                    }
                                    else
                                    {
                                        printf("\nError. Invalid Code Entered. Please Enter a Number >0\n");
                                        fflush(stdin);
                                    }
                                } while (qty_5 <= 0);
                            }
                            else
    
    
                            if (book_code == 1)
                            {
                                back_main = 1;
                                system("cls");
                            }
                            else
                            if (book_code == 2)
                            {
                                switchstart = 1;
                                menu_code = 2;
                                system("cls");
                            }
                            else
                            {
                                printf("Error. Invalid Code");
                                fflush(stdin);
                            }
                        } while (book_code != 1 && book_code != 2);
                        break;
                    case 2:
                        printf("Checkout");
                        printf("\n=========\n\n");
                        if (qty_1 > 0)
                        {
                            printf("101 - %-32s - @ RM %.2f", title[0], price[0]);
                            printf("\nQuantity= %d", qty_1);
                            printf("\nTotal= %.2f\n", ttl_1);
                        }
                        if (qty_2 > 0)
                        {
                            printf("102 - %-32s - @ RM %.2f", title[1], price[1]);
                            printf("\nQuantity= %d", qty_2);
                            printf("\nTotal= %.2f", ttl_2);
                        }
                        if (qty_3 > 0)
                        {
                            printf("103 - %-32s - @ RM %.2f", title[2], price[2]);
                            printf("\nQuantity= %d", qty_3);
                            printf("\nTotal= %.2f\n", ttl_3);
                        }
                        if (qty_4 > 0)
                        {
                            printf("104 - %-32s - @ RM %.2f", title[3], price[3]);
                            printf("\nQuantity= %d", qty_4);
                            printf("\nTotal= %.2f\n", ttl_4);
                        }
                        if (qty_5 > 0)
                        {
                            printf("105 - %-32s - @ RM %.2f", title[4], price[4]);
                            printf("\nQuantity= %d", qty_5);
                            printf("\nTotal= %.2f\n", ttl_5);
                        }
                        sub_ttl = ttl_1 + ttl_2 + ttl_3 + ttl_4 + ttl_5;
                        printf("\n\nSubtotal: RM %.2f", sub_ttl);
                        printf("\n\nEnter 1 to go to main menu, Enter 2 to proceed to payment.");
                        do
                        {
                            printf("\n\nAwaiting Input: ");
                            scanf("%d", &check_code);
                            if (check_code == 1)
                            {
                                back_main = 1;
                                switchstart = 0;
                                system("cls");
                            }
                            else
                            if (check_code == 2)
                            {
                                switchstart = 1;
                                menu_code = 3;
                                system("cls");
                            }
                            else
                            {
                                printf("\n\nError. You have entered an invalid input.\n");
                                fflush(stdin);
                            }
                        } while (check_code != 1 && check_code != 2);
                        break;
                    case 3:
    
    
                        printf("Payment");
                        printf("\n========");
                        if (sub_ttl == 0)
                        {
                            printf("\n\nError. No Item Selected for Purchase");
                            printf("\nPress Any Key to Return to Main Menu : ");
                            getch();
                            back_main = 1;
                            menu_code = 0;
                            switchstart = 0;
                            system("cls");
                        }
                        else
                        {
                            do
                            {
                                
                                printf("\n\nAre you a member? <1 - Yes 2- No> : ");
                                scanf("%d", &member);
                                switch (member)
                                {
                                case 1:
                                    grand_ttl = sub_ttl*(float)0.80;
                                    printf("\n\nThere is a 20 percent discount");
                                    
                                    break;
                                case 2:
                                    grand_ttl = sub_ttl;
                                    printf("\n\nThere is no discount");
                                    
                                    break;
                                default:
                                    printf("\nError. Invalid Code Entered");
                                    fflush(stdin);
                                }
                            } while (member != 1 && member != 2);
                            
                            printf("\n\nThe Grand Total = %.2f", grand_ttl);
                            while (cash_or_credit != 1 && cash_or_credit != 2)
                            {
    
    
                                printf("\n\nPay by Cash or Credit Card? <1 - Cash, 2 - Credit Card> : ");
                                scanf("%d", &cash_or_credit);
    
    
                                if (cash_or_credit == 1)
                                {
    
    
                                    do
                                    {
                                        printf("\n\nPlease Enter Cash Tendered: RM");
                                        scanf("%f", &cash_given);
                                        if (cash_given >= grand_ttl)
                                        {
                                            change = cash_given - grand_ttl;
                                            printf("Change: RM %.2f", change);
                                            printf("\n\nPress any key to continue");
                                            getch();
                                            menu_code = 4;
                                            switchstart = 1;
                                            system("cls");
                                        }
                                        else
                                        {
                                            printf("Error. Invalid Input. ");
                                            fflush(stdin);
                                        }
                                    } while (cash_given < grand_ttl && cash_given != grand_ttl);
                                }
                                else
                                if (cash_or_credit == 2)
                                {
                                    printf("\n\nPlease Select Payment Card");
                                    printf("\n1. Visa");
                                    printf("\n2. Mastercard");
                                    printf("\n3. American Express");
    
    
                                    do
                                    {
    
    
                                        printf("\n\nSelection: ");
                                        scanf("%d", &card_type);
    
    
                                        if (card_type == 1 || card_type== 2 || card_type == 3)
                                        {                                
                                            menu_code = 4;
                                            switchstart = 1;
                                            system("cls");
                                        }
                                        else
                                        {
                                            printf("Selection Invalid. Please Try Again");
                                            fflush(stdin);
                                        }
                                    } while (card_type != 1 && card_type != 2 && card_type != 3);
                                }
                                else
                                {
                                    printf("Error. Invalid Input");
                                    fflush(stdin);
                                }
                            }
                        }
                        break;
                    case 4:
                        switchstart = 0;
                        printf("Receipt");
                        printf("\n========");
                        printf("\n\t\t\t============================= ");
                        printf("\n\t\t\t|| Hatsune Miku Bookstores ||");
                        printf("\n\t\t\t=============================\n\n");
                        time_t now;
                        fflush(stdin);
                        time(&now);
                        printf("%s", ctime(&now));
                        printf("\n");
                        if (qty_1 > 0)
                        {
                            printf("101 - %-32s - @ RM %.2f", title[0], price[0]);
                            printf("\nQuantity= %d", qty_1);
                            printf("\nTotal= %.2f", ttl_1);
                        }
                        if (qty_2 > 0)
                        {
                            printf("102 - %-32s - @ RM %.2f", title[1], price[1]);
                            printf("\nQuantity= %d", qty_2);
                            printf("\nTotal= %.2f", ttl_2);
                        }
                        if (qty_3 > 0)
                        {
                            printf("103 - %-32s - @ RM %.2f", title[2], price[2]);
                            printf("\nQuantity= %d", qty_3);
                            printf("\nTotal= %.2f", ttl_3);
                        }
                        if (qty_4 > 0)
                        {
                            printf("104 - %-32s - @ RM %.2f", title[3], price[3]);
                            printf("\nQuantity= %d", qty_4);
                            printf("\nTotal= %.2f", ttl_4);
                        }
                        if (qty_5 > 0)
                        {
                            printf("105 - %-32s - @ RM %.2f", title[4], price[4]);
                            printf("\nQuantity= %d", qty_5);
                            printf("\nTotal= %.2f", ttl_5);
                        }
                        discount_amt = grand_ttl - sub_ttl;
                        printf("\n\nSubtotal: RM %.2f", sub_ttl);
                        printf("\nDiscount: RM %.2f", discount_amt);
                        
                        if (card_type == 1)
                        {
                            printf("\nPaid by: VISA");
                        }
                        else
                        if (card_type == 2)
                        {
                            printf("\nPaid by: MASTER");
                        }
                        else
                        if ( card_type == 3)
                        {
                            printf("\nPaid by: AMEX");
                        }
                        else
                        {
                            printf("\nPaid by: CASH");
                        }
                        printf("\n====================");
                        printf("\nGrand Total: RM %.2f", grand_ttl);
                        printf("\n====================");
                        printf("\nCash Tendered: RM %.2f", cash_given);
                        printf("\nChange: RM %.2f \n\n", change);
                        printf("Press Any Key to Return to Main Menu :");
                        getch();
                        back_main = 1;
                        ttl_1 = 0;
                        ttl_2 = 0;
                        ttl_3 = 0;
                        ttl_4 = 0;
                        ttl_5 = 0; 
                        sub_ttl = 0; 
                        grand_ttl = 0; 
                        cash_given = 0; 
                        change = 0; 
                        discount_amt=0;
                        card_type = 0;
                        cash_or_credit = 0;
                        menu_code = 0;
                        qty_1 = 0;
                        qty_2 = 0; 
                        qty_3 = 0;
                        qty_4 = 0;
                        qty_5 = 0;
                        check_code = 0;
                        switchstart = 0; 
                        member = 0; 
                            
                        system("cls");
                        
                        break;
                    case 5:
                        back_login = 1;
                        passcode[0] = 0;
                        system("cls");
                        break;
                    case 6:
                        printf("Are You Sure You Want to Exit? <1 - Yes Any Key - No>\n");
                        scanf("%d", &do_exit);
                        switch (do_exit)
                        {
                        case 1: return 0;
                            break;
                        default:
                            back_main = 1;
                            system("cls");
                            break;
                        }
                    case 7:
                        printf("THIS IS A PAGE FOR ALL THE PEOPLE WHO CREATED THIS SOFTWARE WRITE WHATEVER HERE");
                        printf("\n\nPress Any Key to Return to Main Menu");
                        getch();
                        back_main = 1;
                        system("cls");
                        break;
                    default:
                        printf("Error. Invalid Code Entered\n\n");
                        fflush(stdin);
                        back_main = 1;
                        break;
                    }
                } while (switchstart == 1);
            } while (back_main == 1);
        } while (back_login == 1);
        return 0;
    }
    
    
    int booklist()
    
    
    {
        char title[][40] = { "Programming Vol.1 - Mr.Lim", "Programming Vol.2 - Mrs.Chooi", "Modern Physic and Wave - Mr.Lee",
            "Mathematics - Mr.Kang", "Inorganic Chemistry - Mr.Alex" };
        float price[6] = { 20.00f, 15.00f, 30.00f, 25.00f, 20.00f };
    
    
        printf("Book List");
        printf("\n==========\n\n");
        printf("Code - Name - Author - Price");
        printf("\n 101 - %-32s - @RM %.2f", title[0], price[0]);
        printf("\n 102 - %-32s - @RM %.2f", title[1], price[1]);
        printf("\n 103 - %-32s - @RM %.2f", title[2], price[2]);
        printf("\n 104 - %-32s - @RM %.2f", title[3], price[3]);
        printf("\n 105 - %-32s - @Rm %.2f", title[4], price[4]);
    
    
        printf("\n\nEnter 1 to return to main menu, Enter 2 to proceed to checkout.");
    
    
        return booklist;
    }

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Yeah. Make your program data-oriented, so that all the information you spam the user with is stored in one place, then write your program so that it operates on that data.
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    One thing that I noticed: fflush(stdin)
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    Also, getch is not a standard function. You can use getchar() and ask the user to press enter for it to be standard.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, for an assignment, the marker will expect some comments.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A development process

    Now create functions called checkout(), payment() etc.
    Ideally, each block of code for each case statement in main should be a call to a function (with a suitable list of parameters).
    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.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This code appears as a contradiction of skill level.

    On one hand you have nice lookup table arrays, and on the other hand, you have variables like qty_1 to qty_5, not using an array at all.
    You've shown that you can use loops, yet you have passed up every opportunity to use them to iterate over an array.
    You've shown that you know how to make functions, but you've put all the rest of the code into one big long main.

    Overall you're doing well at this stage. I get the feeling that deep down, you know what's wrong with much of it, but you need a nudge of someone else to say: "yes, you really do need to go and put some effort into trying to fix these things". You need to push yourself beyond the boundaries of what you comfortably know, and find that better way. From what I can see, you're smart enough to have a good go at improving the things I and others have mentioned. We will certainly help you if you get stuck.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    about calling to a function, can i return multiple values or just one?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just one, though the value could be a structure (but it takes more work to copy a structure that takes up more space).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    okay so my group members with visual studio 2005 has problems with:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
      time_t now;
      time(&now);
    
      printf("%s", ctime(&now));
    
      return 0;
    }


    while me with 2013 has no problems.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did they bother to tell you what problems they had with it?

  11. #11
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    it was an error,

    error C2063: 'time_t' : not a function
    error C2065: 'now' : undeclared identifier
    error C2065: 'now' : undeclared identifier

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by LOL500 View Post
    it was an error,

    error C2063: 'time_t' : not a function
    error C2065: 'now' : undeclared identifier
    error C2065: 'now' : undeclared identifier
    If the compiler believes you are calling time_t as a function, that means someone put parentheses after the word "time_t". Have that person take them out.

  13. #13
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    lol it still doesn't work, there were no parenthesis. T_T

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Then that compiler or the standard library implementation has a serious bug, which seems unlikely. Visually, I don't see a problem with the code, and my copy of gcc 4.7.3 finds no fault with it either. Are you absolutely sure that the code is the same as what you posted? What options were used to compile?
    Last edited by laserlight; 11-22-2013 at 12:12 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by LOL500 View Post
    lol it still doesn't work, there were no parenthesis. T_T
    If that is the error message they are receiving, then there are parentheses involved. Perhaps they typed
    Code:
    time_t(&now);
    instead of time -- I think even VS provides line numbers so you can see which line it is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opinion please.
    By Carp in forum Tech Board
    Replies: 4
    Last Post: 02-18-2003, 06:48 PM
  2. .NET; What is your opinion?
    By CompiledMonkey in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 05-07-2002, 12:49 AM
  3. Your opinion about .NET
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 04-05-2002, 02:55 PM
  4. your opinion please
    By iain in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-10-2001, 05:36 AM