0

This code is for a resolvent calculator (the variables are in Spanish, but I think you will have no problem reading it). I don't know why this code doesn't work.

#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;

int main() {
    int a, b, c, raiz, r, resultado1, resultado2;

    cout<<"Determine el valor de A: "; cin>> a; cout<<"\n";
    cout<<"Determine el valor de B: "; cin>> b; cout<<"\n";
    cout<<"Determine el valor de C: "; cin>> c; cout<<"\n";

    raiz = pow(b,2) - (4 * a * c);
    r = sqrt(raiz);

    if (raiz > 0) {
        resultado1 = ((b * -1) + r) / (2 * a);
        resultado2 = ((b * -1) - r) / (2 * a);

        cout<<"El valor de X1 es: "<<resultado1<<endl<<endl;
        cout<<"El valor de X2 es: "<<resultado2<<endl<<endl;

    }

    if (raiz < 0) {
        cout<<"La raiz posee un valor negativo, por lo que no podra resolverse. El valor negativo dentro 
               de la raiz es "<<raiz;
    }

    getch();
    return 0;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    You need to describe *precisely* how your program doesn't work. Show an example of input, output, and expected output. – cigien Nov 02 '20 at 23:18
  • In what way exactly does it "not work"? You need to be more specific. – Remy Lebeau Nov 02 '20 at 23:18
  • If you're just learning C++, that's great, but avoid `using namespace std`. The `std::` prefix might seem annoying, but it serves an important purpose, namely separating Standard Library code from your own. – tadman Nov 02 '20 at 23:23
  • 1
    Tip: Instead of declaring a heap of variables all at once, declare them individually *as necessary*, in the context in which they're used. As in: `double r = sqrt(raiz)`. – tadman Nov 02 '20 at 23:24
  • 1
    Tactical note: Compilers are getting smarter and smarter and some know this trick, but `pow(b,2)` is usually less effective than `b*b`. a simple multiplication may be faster and because `pow` operates on floating point numbers and floating point numbers are imprecise, there may be some damage done in the computation that becomes really noticeable when converting back to integers. – user4581301 Nov 02 '20 at 23:31
  • 1
    You could replace `getch()` with `std::cin.ignore(1000000, '\n');` and remove the #include ` – Thomas Matthews Nov 02 '20 at 23:52

1 Answers1

2

Even if a, b and c are ints, the results for r, resultado1 and resultado2 don't have to be. They should be defined as doubles:

double r, resultado1, resultado2;
Mureinik
  • 297,002
  • 52
  • 306
  • 350