Thread: 2 variables always giving same result

1. 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);
}```
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. Do you notice anything wrong in this line?

Originally Posted by zakiuz
Code:
`    printf("Prix total : %.2lf\$", prixT);`

3. 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.

4. @Greaper Well I did'nt finished that line and do not know what to write there actually

@anduril462 I'll try, thanks

5. Code:
`printf("Prix total : %d\$", prixT(prixLivraison(nombreChandail), prixChandail(nombreChandail)));`
Yes ! I got it Thanks a lot people

6. 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. 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. Originally Posted by rmatze
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. Agreed, I should have moved them into main.

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. Originally Posted by zakiuz
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. What do you mean by ''active code'' ?

13. 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__```

14. Originally Posted by zakiuz
What do you mean by ''active code'' ?
What Anduril said....

Anything that gets executed or used directly at run time.

15. 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 )