2 variables always giving same result

This is a discussion on 2 variables always giving same result within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include "prix.h" double prixChandail(double prix); double prixLivraison(double livraison); double prixT(double prixLivraison, double prixChandail); int nombreChandail ...

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

    2 variables always giving same result

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "prix.h"
    
    double prixChandail(double prix);
    double prixLivraison(double livraison);
    double prixT(double prixLivraison, double prixChandail);
    
    int nombreChandail = 0;
    
    int main()
    {
        int recommencer = 0;
        double prixX = 0;
    
        do
        {
        printf("Nombre de chandails : ");
        scanf("%d", &nombreChandail);
        printf("\nPrix livrasion : %.2lf$", prixLivraison(nombreChandail));
        printf("\nPrix chandail : %.2lf$", prixChandail(nombreChandail));
        printf("\n\n");
        printf("Prix total : %.2lf$", prixT);
    
        // =============================
    
        printf("\n\nRecommencer ?");
        printf("\n1. Oui");
        printf("\n0. Non\t");
        scanf("\n%d", &recommencer);
        printf("\n====================\n\n");
        }while(recommencer != 0);
    }
    Headers :
    Code:
    double prixChandail(double prix)
    {
        return prix * 18;
    }
    double prixLivraison(double livraison)
    {
        return livraison * 11;
    }
    double prixT(double prixLivraison, double prixChandail)
    {
        return prixChandail + prixLivraison;
    }
    I'm making a console program to help me calculate some fees for a little buisness I'm looking into. I need to add prixChandail and prixLivraison together to get the total price but prixT which is the total price always gives me the same as prixChandail. Then I would add prixT minus nombreChandail * 55 to get the amount of money I would gain.

    btw, nombreChandail is the number of shirts, prixChandail is the price of shirts, prixLivraison is the price of shipping and prixT is the total. I did't added profit yet.

    So how can I fix this ? Thank you very much !

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Do you notice anything wrong in this line?

    Quote Originally Posted by zakiuz View Post
    Code:
        printf("Prix total : %.2lf$", prixT);
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,491
    I'm confused as to what prixChandail and what prixLivraison you are referring to, the parameters you pass in or the functions. If you mean the parameters, then I suggest making the parameter names different from the function names. If you mean to call the functions, then you need to actually call them. Try
    Code:
    double prixT(double prixL, double prixC)
    {
        return prixC + prixL;
        // or, if you want to add the parameters
        return prixChandail(prixC) + prixLivraison(prixL);
    }
    EDIT: I think GReaper nailed it.

    You need to return an int from main, preferably 0 for success. You should also never put code in a header file, or #include any file that has actual function definitions in it.
    Last edited by anduril462; 09-30-2011 at 03:00 PM.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    19
    @Greaper Well I did'nt finished that line and do not know what to write there actually

    @anduril462 I'll try, thanks

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    19
    Code:
    printf("Prix total : %d$", prixT(prixLivraison(nombreChandail), prixChandail(nombreChandail)));
    Yes ! I got it Thanks a lot people

  6. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Might I suggest this:

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include "prix.h"  
    double prixChandail(double prix); 
    double prixLivraison(double livraison); 
    double prixT(double prixLivraison, double prixChandail);  
    
    int nombreChandail = 0;
    double prixL = 0;
    double prixC = 0;
    double prixTotal = 0;
    
    int main() 
    {     
        int recommencer = 0;     
        double prixX = 0;
          
        do     
        {     
            printf("Nombre de chandails : ");     
            scanf("%d", &nombreChandail);
            prixL = prixLivraison(nombreChandail);     
            printf("\nPrix livrasion : %.2lf$", prixL);
            prixC = prixChandail(nombreChandail);     
            printf("\nPrix chandail : %.2lf$", prixC));     
            printf("\n\n");
            prixTotal = prixT(prixL, prixC);     
            printf("Prix total : %.2lf$", prixTotal);      
            // =============================      
            printf("\n\nRecommencer ?");     
            printf("\n1. Oui");     
            printf("\n0. Non\t");     
            scanf("\n%d", &recommencer);     
            printf("\n====================\n\n");     
        }while(recommencer != 0); 
    }

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Also it's a real bad idea to put active code in a .h file... these are intended to be headers introducing functions and variables from another .c file.
    Frankly with such a small project you shouldn't bother with multiple files, just move your functions into the main file where your compiler can complain about variables and functions with the same names.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,491
    Quote Originally Posted by rmatze View Post
    Might I suggest this:

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include "prix.h"  
    double prixChandail(double prix); 
    double prixLivraison(double livraison); 
    double prixT(double prixLivraison, double prixChandail);  
    
    int nombreChandail = 0;
    double prixL = 0;
    double prixC = 0;
    double prixTotal = 0;
    
    int main() 
    {     
        int recommencer = 0;     
        double prixX = 0;
          
        do     
        {     
            printf("Nombre de chandails : ");     
            scanf("%d", &nombreChandail);
            prixL = prixLivraison(nombreChandail);     
            printf("\nPrix livrasion : %.2lf$", prixL);
            prixC = prixChandail(nombreChandail);     
            printf("\nPrix chandail : %.2lf$", prixC));     
            printf("\n\n");
            prixTotal = prixT(prixL, prixC);     
            printf("Prix total : %.2lf$", prixTotal);      
            // =============================      
            printf("\n\nRecommencer ?");     
            printf("\n1. Oui");     
            printf("\n0. Non\t");     
            scanf("\n%d", &recommencer);     
            printf("\n====================\n\n");     
        }while(recommencer != 0); 
    }
    I wouldn't, at least not a full recommendation. The use of extra variables to save recalculating numbers is a good idea. You just quadrupled the number of global variables however, which is bad. Read Global Variables Are Bad. Move them into main and pass them around as needed.

  9. #9
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Agreed, I should have moved them into main.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    19

    use of headers

    I know it's a pretty small project and I could have put everything together in the main.c but I just learned about header files and wasn't sure how to use them, so I decided to make a program that involves multiple lines in the header file to fully understand it. Now I do :P.

    And the global variables were put so I can use them in the header file.
    I'm trying to add more options to the program itself just to explore what I've learned so far, like having the user to choose the price instead of 55$.

    But thanks a lot to all answers this forum has been great so far even toward a newbie like me

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zakiuz View Post
    I know it's a pretty small project and I could have put everything together in the main.c but I just learned about header files and wasn't sure how to use them, so I decided to make a program that involves multiple lines in the header file to fully understand it. Now I do :P.
    Trust me... you don't.

    Your project as multiple files should have been at least 3 separate files... main.c with your main program code, a second .c file (func.c?) with your function code in it and a third header file (func.h) listing function prototypes from the second .c file. You should never put active code in a .h file.
    But thanks a lot to all answers this forum has been great so far even toward a newbie like me
    Best bet... get yourself a decent C textbook sit down and do a deliberate study... page by page, do up all the examples and quizzes, move forward only when you understand what you've learned so far... You will be very glad you did.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    19
    What do you mean by ''active code'' ?

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,491
    By "active code", Tater basically means anything that ends up in the final executable. That means global variables and functions bodies (as opposed to function prototypes). The proper way to lay out your files (using rmatze's code as a base) is like so:
    main.c
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include "prix.h"  // this is just for the prototypes, not the definitions themselves.
    
    int main() 
    {     
        int nombreChandail = 0;
        double prixL = 0;
        double prixC = 0;
        double prixTotal = 0;
        int recommencer = 0;     
        double prixX = 0;
          
        do     
        {     
            printf("Nombre de chandails : ");     
            scanf("%d", &nombreChandail);
            prixL = prixLivraison(nombreChandail);     
            printf("\nPrix livrasion : %.2lf$", prixL);
            prixC = prixChandail(nombreChandail);     
            printf("\nPrix chandail : %.2lf$", prixC));     
            printf("\n\n");
            prixTotal = prixT(prixL, prixC);     
            printf("Prix total : %.2lf$", prixTotal);      
            // =============================      
            printf("\n\nRecommencer ?");     
            printf("\n1. Oui");     
            printf("\n0. Non\t");     
            scanf("\n%d", &recommencer);     
            printf("\n====================\n\n");     
        }while(recommencer != 0); 
        return 0;
    }
    prix.c
    Code:
    double prixChandail(double prix)
    {
        return prix * 18;
    }
    double prixLivraison(double livraison)
    {
        return livraison * 11;
    }
    double prixT(double prixLivraison, double prixChandail)
    {
        return prixChandail + prixLivraison;
    }
    prix.h
    Code:
    // all the lines with PRIX_H__ form an include guard
    // every header file you ever create should have an include guard
    // Read the Wikipedia article for details
    #ifndef PRIX_H__
    #define PRIX_H__
    
    // these are just function prototypes
    // they exist so the compiler can make sure you pass it the right
    // number and type of parameters and use the return value correctly
    double prixChandail(double prix);
    double prixLivraison(double livraison);
    double prixT(double prixLivraison, double prixChandail);
    
    #endif  // PRIX_H__
    Last edited by anduril462; 09-30-2011 at 10:30 PM.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zakiuz View Post
    What do you mean by ''active code'' ?
    What Anduril said....

    Anything that gets executed or used directly at run time.

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    19
    Oh ok I get it. I did it with 3 files it works perfectly and it's much more simple this way ! I'll read more about global variables now (and why they are bad )

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. output screen isn't giving proper result
    By time4f5 in forum C Programming
    Replies: 11
    Last Post: 03-22-2011, 01:34 AM
  2. Replies: 6
    Last Post: 12-02-2009, 07:47 AM
  3. sizeof(Header) giving wrong result
    By kapil1089thekin in forum C Programming
    Replies: 4
    Last Post: 05-05-2008, 09:30 AM
  4. Replies: 3
    Last Post: 11-28-2006, 02:44 PM
  5. Giving 100%!!
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 03-13-2005, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21