I'm having an error in my C++ class. I can't seem to get the division to work properly. I keep getting strange errors.
1 2 3 4 5 6 7 8 9 10 11 12
elseif (oper == '/' || oper == '%')
{
if (bb==0)
cout << "\n undefined";
else
aa=a;
bb=b;
c = a /b;
cout << "\n division" << c << setprecision (3);
cout << "\n remainder" << a % b;
}
I already declared the int and double. I also have a set precision section to prevent infinite decimals. I keep getting crazy inaccurate answers for division like "-99972"". I also want to remove the scientific notions
You haven't shown the declarations of any of your variables.
If a and b are ints, then you're doing integer division and you;re not going to get the right answer.
here you go, I wanted to avoid posting the whole thing so my professor doesn't think I'm plagiarizing but whatever. Either she won't see this or she will and I'll explain it.
#include <iostream> // input and output
#include <iomanip> // manipulator that allows the user to use setprecision
usingnamespace std;
int main ()
{
int a; //declaration of 2 integers
int b;
double aa; //declaration of two double values. They can hold both integers and floats
double bb; //used two aa's as an easy way to remember double=two letters
double c;
char oper;
cout << "\nEnter first number" <<endl ; /* prompt to enter first and second numbers */
cin >> aa ;
cout << "\nEnter second number" <<endl ;
cin >> bb;
cout << "\nChoose a mathematical operator"; //prompt for operator
cin >> oper;
if (oper == '*' || oper == 'x') // multiple options for operators
{
cout << "\nThe product of the two numbers is\t\n" ;
cout << aa*bb;
}
elseif (oper == '-')
{
cout << "\nThe difference of the two numbers is" ;
cout << aa-bb;
}
elseif (oper == '+')
{
cout << "\nThe sum of the two numbers is" ;
cout << aa+bb ;
}
elseif (oper == '/' || oper == '%')
{
if (bb==0)
cout << "\n undefined";
else
aa=a;
bb=b;
c = a /b;
cout << setprecision (5) << "\n division" << c;
cout << "\n remainder" << a % b;
}
else
cout << "error exiting program";
return 0;
}
#include <iostream> // input and output
#include <iomanip> // manipulator that allows the user to use setprecision
usingnamespace std;
int main ()
{
int a; //declaration of 2 integers
int b;
double aa; //declaration of two double values. They can hold both integers and floats
double bb; //used two aa's as an easy way to remember double=two letters
double c;
char oper;
cout << "\nEnter first number" <<endl ; /* prompt to enter first and second numbers */
cin >> aa ;
cout << "\nEnter second number" <<endl ;
cin >> bb;
cout << "\nChoose a mathematical operator"; //prompt for operator
cin >> oper;
if (oper == '*' || oper == 'x') // multiple options for operators
{
cout << "\nThe product of the two numbers is\t\n" ;
cout << aa*bb;
}
elseif (oper == '-') //continuing pattern for several lines
{
cout << "\nThe difference of the two numbers is" ;
cout << aa-bb;
}
elseif (oper == '+')
{
cout << "\nThe sum of the two numbers is" ;
cout << aa+bb ;
}
elseif (oper == '/') //division by zero is not possible. If else inside statement to separate the two outcomes
{
if (bb==0) //checks value
cout << "\nUndefined" ;
else
cout << "\n("<< aa << "/" << bb << ")"<< "=" << setprecision(5)<< aa/bb; // decimal place cannot exceed 5. Also it looks fancy
}
elseif (oper == '%') //have to use variables for this
{
a=aa; //declaration of variables
b=bb;
if (b==0) // once again zero is not an acceptable value
cout << "Error" ;
else
cout << a % a ; answer
}
else
cout << "error exiting program"; //in case user tries to type nonsense
return 0;
}
#include <iostream> // input and output
#include <iomanip> // manipulator that allows the user to use setprecision
usingnamespace std;
int main ()
{
int a; //declaration of 2 integers
int b;
double aa; //declaration of two double values. They can hold both integers and floats
double bb; //used two aa's as an easy way to remember double=two letters
char oper;
cout << "\nEnter first number" <<endl ; /* prompt to enter first and second numbers */
cin >> aa ;
cout << "\nEnter second number" <<endl ;
cin >> bb;
cout << "\nChoose a mathematical operator"; //prompt for operator
cin >> oper;
if (oper == '*' || oper == 'x') // multiple options for operators
{
cout << "\nThe product is\n" ;
cout << aa*bb;
}
elseif (oper == '-') //continuing pattern for several lines
{
cout << "\nThe difference is" ;
cout << aa-bb;
}
elseif (oper == '+')
{
cout << "\nThe sum is" ;
cout << aa+bb ;
}
elseif (oper == '/' || oper == '%') // I combined the division and modus operators.
//this makes it easier to use
{
if (bb==0) //If zero is enter the answer is undefined
{
cout << "\n undefined";
}
else
{
a=aa;
b=bb;
c = aa /bb;
cout << setprecision (5) << "\n division" << c; //stop decimals after 5 spaces
cout << "\n remainder" << a % b;
}
}
else
cout << "error exiting program"; //in case user tries to type nonsense
return 0;
}