My linear equation calculator is calculating 71/6 = 13.5!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````// ax + b = c linear equation calculator. #include #include #include int main() { std::string a_var_s; long double a_var = 0; std::string b_var_s; long double b_var = 0; std::string c_var_s; long double c_var = 0; std::string correct_equation; std::string correct_equation_validation; long double right_side = 0; long double res = 0; std::string use_again; do { std::cout << "\n"; std::cout << "This program solves linear equations in the ax + b = c form.\n"; std::cout << "Example: 5x + 4 = 9. This program will calculate the value of x.\n"; while ( correct_equation != "y" ) { std::cout << "Input the value of a:\n"; std::getline ( std::cin, a_var_s ); std::stringstream( a_var_s ) >> a_var; std::cout << "Input the value of b:\n"; std::getline ( std::cin, b_var_s ); std::stringstream( b_var_s ) >> b_var; std::cout << "Input the value of c:\n"; std::getline ( std::cin, c_var_s ); std::stringstream( c_var_s ) >> c_var; std::cout << "Did you mean to input:\n"; std::cout << a_var << "x"; if ( b_var >= 0 ) std::cout << "+" << b_var; else std::cout << b_var; std::cout << "=" << c_var << ".\n"; std::cout << "\n"; do { std::cout << "Input 'y' if correct or 'n' if incorrect:\n"; std::cin >> correct_equation; std::cin.ignore(); if ( correct_equation == "y" || correct_equation == "Y" ) { correct_equation_validation = "y"; correct_equation = "y"; } else if ( correct_equation == "n" || correct_equation == "n") correct_equation_validation = "y"; else correct_equation_validation = "n"; } while ( correct_equation_validation != "y" ); } if ( b_var >= 0) { right_side = c_var - b_var; std::cout << right_side << "\n"; std:: cout << a_var; } else b_var = b_var * -1; right_side = c_var + b_var; res = right_side/a_var; std::cout << "\n"; std::cout << res << "\n"; std::cout << "Input 'y' to use this program again:\n"; std::cin >> use_again; } while ( use_again == "y" || use_again == "Y" ); return 0; }``````
You are probably dividing two integers, that gives you one integer (rounded).

Put a . behind your constants to make them doubles:

`double num = 71./6.;`

if you want the decimal value of two divided integers:

 `` `` ``double num = static_cast(someinteger) / static_cast(anotherinteger);``
I found the issue, the commands in the last else statement was not in braces.
Topic archived. No new replies allowed.