Thread: Please help fix my code!

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

    Please help fix my code!

    I have a C project due this week, and I have written the following code to the best of my ability, but I don't know where to go from here:

    Code:
    #include <stdio.h>
    
    
    int array[10], choice, table;
    float temp, payment;
    char choice2;
    
    
    void tabs();
    void addOrder();
    void addPayment();
    void printTabs();
    
    
    int main(){
        tabs();
        printf("1) Add Order To Table's Tab.\n");
        printf("2) Add Payment To Table's Tab.\n");
        printf("3) Print Out All Tab Values.\n");
        printf("4) Close Out All Tables and Shutdown.\n");
        printf("What is your choice? ");
        scanf("%i", &choice);
            if (choice == 1)
                addOrder()
            if (choice == 2)
                addPayment()
            if (choice == 3)
                printTabs()
            if (choice == 4){
                tabs();
                printf("Shutting down...Goodbye!\n");
                return 0;
            else
                printf("Invalid option.");
                /* Redo main() */
            }
    }
    
    
    void tabs(){
        printf("\n");
        printf("Table Tabs:\n");
        printf("-------------------------\n");
        printf("Table #1          $%.2f\n", array[1]);
        printf("Table #2          $%.2f\n", array[2]);
        printf("Table #3          $%.2f\n", array[3]);
        printf("Table #4          $%.2f\n", array[4]);
        printf("Table #5          $%.2f\n", array[5]);
        printf("Table #6          $%.2f\n", array[6]);
        printf("Table #7          $%.2f\n", array[7]);
        printf("Table #8          $%.2f\n", array[8]);
        printf("Table #9          $%.2f\n", array[9]);
        printf("Table #10         $%.2f\n", array[10]);
    }
    
    
    void addOrder(){
        printf("C) Chili            $  2.50\n");
        printf("N) Nachos            $  6.50\n");
        printf("P) Pizza            $ 12.00\n");
        printf("S) Sliders            $  5.00\n");
        printf("W) Buffalo Wings    $ 10.50\n");
        printf("What did the customer order?: ");
        scanf("%c", &choice2);
            if (choice2 == "C" || "c")
                temp = 2.5;
            else if (choice2 == "N" || "n")
                temp = 6.5;
            else if (choice2 == "P" || "p")
                temp = 12;
            else if (choice2 == "S" || "s")
                temp = 5;
            else if (choice2 == "W" || "w")
                temp = 10.5;
            else
                printf("Orders have be to be one of the following:\n");
                /* loop function */
        printf("What table's tab are you dealing with? (Tables are numbered 1-10) ");
        scanf("%i", &table);
        if (table < 1 || table > 10){
            printf("Tables are numbered 1-10.");
            printf("Enter a valid number.");
            /* Repeat printf above "What table's tab..." */
        }
        else
            /* Back to main() */
    }
    
    
    void addPayment{
        printf("What table's tab are you dealing with? (Tables are numbered 1-10) ");
        scanf("%i", &table);
        printf("Enter their payment amount: ");
        scanf("%f", &payment);
        array[table] = array[table] - payment
        /* Back to main() */
    }
    
    
    void printTabs{
        printf("What table's tab are you dealing with? (Tables are numbered 1-10) ");
        scanf("%i", &table);
        printf("Enter their payment amount: ");
        scanf("%f", &payment);
            if (payment > ){
                printf("You entered %f but their tab is only $%.2f!\n", payment, array[table]);
                printf("Enter their payment amount: ");
                scanf("%f", &payment);
            }
            else
                /* Back to main() */
    }
    Any help will be appreciated.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Do you have a specific question or problem?

    Also stop using those global variables. Make the variables local to your functions and pass them to and from other functions as required.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    How about telling us the output you want to see, and the output you are getting?

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    6
    How do I loop back to main or to the start of the current function as directed by the green comments?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    These are your first six mistakes:
    Code:
    int array[10], choice, table;
    float temp, payment;
    char choice2;
    What mistake did you make? Simple: you declared them at file scope, making them global variables. Global variables tend to make it more difficult for you to reason about your program: at every point of your code, you have to consider the global state, and then whenever there is a function call, you have to consider if the function call might modify the global state, particularly the state of a global variable that you are concerned with at that point.

    Your next mistake is more harmless, but still:
    Code:
    void tabs();
    The mistake is twofold: the name does not really express what the function does, and then when declaring a function that takes no arguments, you should explicitly specify void as the parameter list. So, we might write:
    Code:
    void printAllTabs(void);
    Your next three mistakes are related to the first six:
    Code:
    void addOrder();
    void addPayment();
    void printTabs();
    What does addOrder do? It appears to request for the user's order, but then it must also update something. So, decide on what parameters addOrder should take. Likewise for the other functions.

    A few other comments:
    • To "Redo main()", use a loop. This is related to your "Back to main()" comments: they are unnecessary. The functions should just return to the caller, and if the caller happens to be main, great.
    • You declared array as being an array of 10 elements. Therefore, the valid indices of array are from 0 to 9, inclusive. array[10] is out of bounds. Actually, array is a bad name for a domain-specific array: what is this array for?
    • choice2 == "C" || "c" is wrong. You probably meant: choice2 == 'C' || choice2 == 'c'. Actually, with local variables, you won't need to call the variable choice2: you could also call it choice.
    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

  6. #6
    Registered User
    Join Date
    Nov 2015
    Posts
    6
    Thank you for your quick reply. I will return if I have further issues.

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    What are the Buffalo Wings like? Can I try a bit of the sauce they're cooked in before I order them? Thanks!

    Edit: Also, how many wings do I get? They seem a bit on the expensive side.
    Last edited by Hodor; 11-23-2015 at 10:02 PM.

  8. #8
    Registered User
    Join Date
    Nov 2015
    Posts
    6
    Okay, so here's my latest code:
    Code:
    #include <stdio.h>
    
    
    int choice, table;
    float array[10], temp, payment;
    char choice2;
    
    
    void tabs();
    void addOrder();
    void addPayment();
    void printTabs();
    
    
    int main(){
        tabs();
        printf("1) Add Order To Table's Tab.\n");
        printf("2) Add Payment To Table's Tab.\n");
        printf("3) Print Out All Tab Values.\n");
        printf("4) Close Out All Tables and Shutdown.\n");
        printf("What is your choice? ");
        scanf("%i", &choice);
            while (choice == 1)
                addOrder();
            while (choice == 2)
                addPayment();
            while (choice == 3)
                printTabs();
            if (choice == 4){
                tabs();
                printf("Shutting down...Goodbye!\n");
                return 0;
            }
            while (choice < 1 || choice > 4){
                printf("Invalid option.");
                scanf("%i", &choice);
            }
    }
    
    
    void tabs(){
        printf("\n");
        printf("Table Tabs:\n");
        printf("-------------------------\n");
        printf("Table #1          $%.2f\n", array[1]);
        printf("Table #2          $%.2f\n", array[2]);
        printf("Table #3          $%.2f\n", array[3]);
        printf("Table #4          $%.2f\n", array[4]);
        printf("Table #5          $%.2f\n", array[5]);
        printf("Table #6          $%.2f\n", array[6]);
        printf("Table #7          $%.2f\n", array[7]);
        printf("Table #8          $%.2f\n", array[8]);
        printf("Table #9          $%.2f\n", array[9]);
        printf("Table #10         $%.2f\n", array[10]);
    }
    
    
    void addOrder(){
        printf("C) Chili            $  2.50\n");
        printf("N) Nachos            $  6.50\n");
        printf("P) Pizza            $ 12.00\n");
        printf("S) Sliders            $  5.00\n");
        printf("W) Buffalo Wings    $ 10.50\n");
        printf("What did the customer order?: ");
        scanf("%c", &choice2);
            if (choice2 == 'C' || choice2 == 'c')
                temp = 2.5;
            else if (choice2 == 'N' || choice2 == 'n')
                temp = 6.5;
            else if (choice2 == 'P' || choice2 == 'p')
                temp = 12;
            else if (choice2 == 'S' || choice2 == 's')
                temp = 5;
            else if (choice2 == 'W' || choice2 == 'w')
                temp = 10.5;
            while (choice2 != 'C' && choice2 != 'c' && choice2 != 'N' && choice2 != 'n' && choice2 != 'P' && choice2 != 'p' && choice2 != 'S' && choice2 != 's' && choice2 != 'W' && choice2 != 'w'){
                printf("Orders have be to be one of the following:\n");
                printf("C) Chili            $  2.50\n");
                printf("N) Nachos            $  6.50\n");
                printf("P) Pizza            $ 12.00\n");
                printf("S) Sliders            $  5.00\n");
                printf("W) Buffalo Wings    $ 10.50\n");
                printf("What did the customer order?: ");
                scanf("%c", &choice2);
            }
        printf("What table's tab are you dealing with? (Tables are numbered 1-10) ");
        scanf("%i", &table);
        while (table < 1 || table > 10){
            printf("Tables are numbered 1-10.");
            printf("Enter a valid number.");
            printf("What table's tab are you dealing with? (Tables are numbered 1-10) ");
            scanf("%i", &table);
        }
            /* Back to main() */
    }
    
    
    void addPayment(){
        printf("What table's tab are you dealing with? (Tables are numbered 1-10) ");
        scanf("%i", &table);
        printf("Enter their payment amount: ");
        scanf("%f", &payment);
        array[table] = array[table] - payment;
        /* Back to main() */
    }
    
    
    void printTabs(){
        //int {Add local variables here!}
        printf("What table's tab are you dealing with? (Tables are numbered 1-10) ");
        scanf("%i", &table);
        printf("Enter their payment amount: ");
        scanf("%f", &payment);
            while (payment > array[table]){
                printf("You entered %f but their tab is only $%.2f!\n", payment, array[table]);
                printf("Enter their payment amount: ");
                scanf("%f", &payment);
            }
                /* Back to main() */
    }
    Here's the input:
    Code:
    gcc -Wall tabs.c -o tabs.out
    And here's the output:
    Code:
    tabs.c: In function ‘main’:
    tabs.c:35: warning: control reaches end of non-void function
    Can anyone think of a solution?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The solution for that one is simple: add a return 0; at the end of the main function.

    However, your code still has the mistakes that I outlined earlier, so this trivial fix means little.
    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

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > 4 Days Ago #6
    > Andrew K
    ...
    > 5 Hours Ago #8
    > Andrew K
    Well here's your problem.
    You missed out on making some progress 3 days ago, 2 days ago and yesterday.

    So now you're in headless chicken mode

    Your failure to plan ahead doesn't constitute an emergency on our part.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM