Thread: C program problem

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    23

    C program problem

    Hello,

    What the code below is not doing is returning a value for item to my variables (item1, item2, etc..) I'm not sure how I can make a function prototype that will ask for input and basically recycle after going through the program to a new value for each item. I also need to input individual tax for each item.
    I'm new to the forums so I will also post it in this message:
    double tax, item,salesTax;

    double cost(double);
    double CalcSalesTax(double);

    main(){
    double total,item1, item2, item3, item4, item5, tax1, tax2, tax3,tax4,tax5;


    item1 = cost(tax2);
    tax1 = CalcSalesTax(tax);
    printf("%f", item1);
    item2 = cost(item);
    printf("%f", item2);
    tax2 = CalcSalesTax(tax);
    item3 = cost(item);
    tax3 = CalcSalesTax(tax);
    item4 = cost(item);
    tax4 = CalcSalesTax(tax);
    item5 = cost(item);
    tax5 = CalcSalesTax(tax);

    total = item1 + item2+ item3 + item4 +item5;
    printf("%4.2f", total);

    }

    double cost(double item){
    printf("Please Enter the item amount: ");
    scanf("%f", &item);

    return item;
    }

    double CalcSalesTax(double tax){
    printf("Please Enter the Amount for tax: ");
    scanf("%f", &tax);
    salesTax = (item * tax);
    return salesTax;
    }
    Attached Files Attached Files

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's a difference between "my function isn't returning a value" and "I don't know how to read a value from the user". This appears to be the latter, as scanf requires %lf to read a double.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    23
    Well thanks a lot

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    23
    Wow thank you so much.. you practically saved my life

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In the future, be sure to post your code in code tags.

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    23
    Quote Originally Posted by Matticus View Post
    In the future, be sure to post your code in code tags.
    I am, as stated above, new to this website. Thanks for the tip though.

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    23

    Finished code

    Code:
    #include <stdio.h>
    
    double tax, item,salesTax;
    
    double cost(double);
    double CalcSalesTax(double);
    
    main(){
    double total,item1, item2, item3, item4, item5, tax1, tax2, tax3,tax4,tax5;
    
    
        item1 = cost(item);
        tax1 = CalcSalesTax(item1);
    
        item2 = cost(item);
        tax2 = CalcSalesTax(item2);
    
        item3 = cost(item);
        tax3 = CalcSalesTax(item3);
    
        item4 = cost(item);
        tax4 = CalcSalesTax(item4);
    
        item5 = cost(item);
        tax5 = CalcSalesTax(item5);
    
        system("cls");
    
        total = tax1 + tax2+ tax3 + tax4 +tax5;
        printf("Item1:%4.2f\nItem2:%4.2f\nItem3:%4.2f\nItem4:%4.2f\nItem5:%4.2f\nTOTAL:%4.2f\n", tax1, tax2, tax3,tax4,tax5,total);
    
    }
    
        double cost(double item){
            printf("Please Enter the item amount: ");
            scanf("%lf", &item);
                return item;
     }
    
        double CalcSalesTax(double item){
            printf("Please Enter the Amount for tax(In Decimal form, please): ");
            scanf("%lf", &tax);
            salesTax = ((item * tax)+item);
                return salesTax;
    }

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Good work! If this meets your requirements, then by all means, submit it as is. Just for your information, though, I'll offer a few suggestions...

    You should avoid using global variables. In your program, the globals "tax" and "salesTax" are used exclusively in the "CalcSalesTax()" function, so these variables can be declared locally in that function.

    Along the same idea, "item" does not need to be global - nor do you have to pass this variable to your "cost()" function. You could declare this variable in your "cost()" function, then just read into this variable and return it from your function (as you're doing now).

    When you see numbered variables (e.g. "item1", "item2", etc), this usually means an array would be more appropriate (in conjuction with a loop). If you used a named constant in your program to declare the arrays to be the right size, as well as to control the loop, then you can change how many items are read by your program simply by changing one number in your source code.

  9. #9
    Registered User
    Join Date
    Mar 2014
    Posts
    23
    Could my cost variable have void parameters then? I wasn't sure because at first.. I used void parameters, but the "lf" error lead me to believe that the void was the problem so I changed it to take a double instead.

    Thanks for all of the tips, I appreciate them and have also learned from them. I know about arrays, but I'm currently taking a C class and the teach hasn't introduced arrays yet, so I didn't want to make it seem like I was copying another program. I used the globals because the teach is trying to iterate the use of them.

    Could you provide me with an example of the array that I could include in this program?

    I've been wondering how I may ask for input in a loop and have it cycle through different variable I want to define. If you could show me that, I'd appreciate that too!

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Could my cost variable have void parameters then?
    Yes (be sure you use the right terminology - "cost" is a function in your code). In fact, when I was playing around with your code (implementing myself the suggestions I offered), I did just that.

    Could you provide me with an example of the array that I could include in this program?
    Well, I could - but if you reason it out yourself, you'll get more experience and a better understanding. Read up on arrays in your book (or online tutorials - we have a section here, and I also found this one). Since arrays are particularly suited for use in loops, most material will likely cover these topics near each other.

    I've been wondering how I may ask for input in a loop and have it cycle through different variable I want to define.
    The tutorials show how to print out elements of an array. You already read user input in the program you wrote. All you have to do is figure out how to combine these concepts. Again, I'll leave that as an exercise for you.

    Remember, you can always create a small project to test ideas out, before implementing them in your "real" code.

    If you have any specific difficulties with arrays, don't hesitate to ask.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Khalil Collins View Post
    Could my cost variable have void parameters then? I wasn't sure because at first.. I used void parameters, but the "lf" error lead me to believe that the void was the problem so I changed it to take a double instead.
    Yes, it can. And if there is no need to pass in a parameter, your function should have void params:
    Code:
    double cost(void) {
        double item;
        ...
    }
    Quote Originally Posted by Khalil Collins View Post
    I used the globals because the teach is trying to iterate the use of them.
    Either you misunderstood the teacher, or you have a very bad teacher. Globals are almost always the wrong solution to the problem. They make code difficult to scale, reason about, debug, fix and maintain. Read this for some more discussion.

    Quote Originally Posted by Khalil Collins View Post
    Could you provide me with an example of the array that I could include in this program?

    I've been wondering how I may ask for input in a loop and have it cycle through different variable I want to define. If you could show me that, I'd appreciate that too!
    It sounds like you're not quite there yet in class, so no need to push it. But if you want to get ahead, I suggest giving your textbook a good read, along with some online tutorials. The very basics might look like this
    Code:
    #define NUM_ITEMS 5
    ...
    int main(void) {
        double items[NUM_ITEMS];
        int i;
    
        for (i = 0; i < NUM_ITEMS; i++) {
            // do something with items array
    If you really want to know what goes in the loop body, read up on arrays. You'll get a much better understanding of them that way, than if I give you a complete, working code snippet you can use without really learning the subject.

    Note that you should always define a constant with a meaningful name, and use that in the array declaration and any loops that process the array. Never use magic numbers.

  12. #12
    Registered User
    Join Date
    Mar 2014
    Posts
    23
    I've already submitted the assignment, and I learn better from explanation of the code. Could you should me the body? I worked with arrays in java. I don't know how they work with C.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Khalil Collins View Post
    I've already submitted the assignment, and I learn better from explanation of the code. Could you should me the body? I worked with arrays in java. I don't know how they work with C.
    You access them very much the same way you do in Java, with the [ ] operator. Java's syntax is based heavily on C.
    Code:
    foo[1] = 42;

  14. #14
    Registered User
    Join Date
    Mar 2014
    Posts
    23

    Code!

    This is my corrected code, resubmitted to my instructor. Hopefully he hasn't graded and will accept.

    Code:
     
    #include <stdio.h>
    
    double cost(void);
    double CalcSalesTax(double);
    
    main(){
        double total;
        double items[4],tax[4];
        int i;
    
    
        for(i=0; i<=4;i++){
            items[i] = cost();
            tax[i]= CalcSalesTax(items[i]);
            }
    
            system("cls");
    
            for(i=0;i<=4;i++)
                printf("Item:%4.2f\n",tax[i]);
    
                total = tax[0]+tax[1]+tax[2]+tax[3]+tax[4];
                printf("Total:%4.2f",total);
    
                getch();
    }
    
        double cost(){
            double item;
            printf("Please Enter the item amount: ");
            scanf("%lf", &item);
                return item;
     }
    
        double CalcSalesTax(double item){
            double tax,salesTax;
            printf("Please Enter the Amount for tax(In Decimal form, please): ");
            scanf("%lf", &tax);
            salesTax = ((item * tax)+item);
                return salesTax;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi...me again same program different problem.
    By arti in forum C Programming
    Replies: 1
    Last Post: 05-03-2010, 10:04 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. A problem with my program.
    By eXeCuTeR in forum C Programming
    Replies: 14
    Last Post: 11-22-2007, 02:56 PM
  4. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  5. program problem
    By Korn1699 in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2001, 05:59 PM