I want to create a program that solve quadratic equation (Ax²+Bx+C=0) using 2 void functions: one to insert the values of A,B,C, and the second for solving the equation. This is what I did:
#include <iostream>
#include <math.h>
using namespace std;
void add_nmbr(int a, int b, int c){
int *pa,*pb,*pc;
cout << "Entrer le nombre A " <<endl;
cin >> a;
cout << "Entrer le nombre B " <<endl;
cin >> b;
cout << "Entrer le nombre C " <<endl;
cin >> c;
pa = &a;
pb = &b;
pc = &c;
cout << a <<"x2 + "<<b<<"x + "<<"c = 0"<<endl;
}
void resoudre(int a,int b, int c){
double delta;
double x1,x2;
delta= b*b-4*a*c ;
if (delta<0){
cout << "Pas de solution !"<<endl;
}else{
x1=(-b-(sqrt(delta)))/(2*a);
x2=(-b+(sqrt(delta)))/(2*a);
}
cout << a <<"x2 + "<<b<<"x + "<<"c = 0"<<endl;
cout << "la solution est : " << x1 << endl;
cout << "la solution est : " << x2 << endl;
}
int main()
{
int a,b,c;
add_nmbr(a,b,c);
resoudre(a,b,c);
return 0;
}
When you declare a function like
void add_nmbr(int a, int b, int c)
you are passing parameters by value which means you pass a copy of value into the function. You can change the value insideadd_nmbr
fora
but that value stays inside the function. In you case, the variablea
in functionmain
stays un-initialized.The same thing for
resoudre
. To fix it, you can usereference
, like this