3 function program returning incorrect answer

I'll be upfront: this is a homework assignment. I'm not looking for a fix, but I am confused as to what I've done wrong. My assignment is to write a program that solves quadratic equations. Easy enough, I thought. I suspect my issue might be with how i coded the actual formula itself, since the end result isnt producing the right numbers. I've reviewed my notes and I can't see what i did wrong. Can someone give me a hint? any general tips on syntax would also be much appreciated.

Additionally, I thought about using an overloaded function to produce two values, but I wasnt sure exactly how to execute that. Still working on what exactly my input validation should be, but I'm confident I can get through that well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  // Solving quadratic formulas
#include <iostream>
#include <cmath>

using namespace std ;

int quadfunc(int a, int b, int c) // x= [-b + sqrt(b^2 - 4 ac)]/2a
{
    int z ;
    z = (-b + sqrt(pow( b, 2) - (4*a*c)))/(2*a) ;
    return z ;
}
int QuadFunc(int a, int b, int c) // x= [-b - sqrt(b^2 - 4 ac)]/2a
{
    int z ;
    z = (-b - sqrt(pow( b, 2) - (4*a*c)))/(2*a) ;
    return z ;
}
int main()
{
    float A, B, C, X, x ;
    X= QuadFunc( A, B, C );
    x=quadfunc( A, B, C ) ;
  cout << "Hello, I will solve any quadratic formula of the form : A(x^2) + Bx + C = 0. \n" ;
  cout << "Please enter the coefficient for x^2 : " ;
  cin >> A ;
  cout << "Please enter the coefficient for x : " ;
  cin >> B ;
  cout << "Please enter the constant, C : " ;
  cin >> C ;
  cout << "Thank you. I will now use the quadratic formula to calculate two X values. " ; 
  cout << "They are " << X << " and " << x ; 
  return 0 ;
  }
Your functions should be returning a float, since you deal with decimal numbers. You pass floats into the parameter as ints, but should be float...

You should call the functions after the user inputs the values too. If you pass them first, the user hasn't entered any values for them, so when the program calculates the roots, it's actually calculating who knows what.
Last edited on
Registered users can post here. Sign in or register to post.