Program dividing incorrectly - please help
My linear equation calculator is calculating 71/6 = 13.5!
Please help.
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
// ax + b = c linear equation calculator.
#include <iostream>
#include <string>
#include <sstream>
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<double>(someinteger) / static_cast<double>(anotherinteger);
|
I found the issue, the commands in the last else statement was not in braces.
Topic archived. No new replies allowed.