Something goes wrong with my program in xcode

so im having a bit of trouble with getting my program to run properly in xcode. the build succeeds (thats what xcode says) but as soon as a i enter my two resistances (youll see what i mean below), something goes wrong. so here is the code:


//"Resistance is Everything" program -- CS 121

#include <iostream>
using namespace std;
int main()

{
short res1; //declared first variable
short res2; //declared second variable
short series_total;
float parallel_total;

cout << "\t\t\tWelcome to the Resistor Calculation Program!!!";
cout << "\n\nPlease enter your two resistances: ";

cin >> res1 >> res2;

series_total = res1 + res2;
parallel_total = (1)/((1/res1) + (1/res2));

cout << "\nThank you!! You have entered " << res1 << " ohms and " << res2 << " ohms." << endl << endl;

cout << "If your resistors are placed in series, they will total to " << series_total << " ohms." <<endl;

cout << "If your resistors are placed in parallel, they will total to " << parallel_total << " ohms." <<endl <<endl;

cout << "Thank you for using RCP. Have a wonderful day!";


cout << "\n\n"; //Included for cleaner look

return 0;
}

when i enter "3 3" meaning my two resistors i have both have a value of 3 ohms, the program doesnt actually stop running, but in the output screen it says (11db). and also, it highlights the "parallel_total" equation in my program in green and next to it, it says "thread 1 exc_arithmetic (code=exc_i386_div subcode=0x0)"

can you guys help me out with this? i have no idea what it means.


ALSO!...

when i change the data types for res1 res2 and series_total, to float the program runs perfectly fine. why isnt it running correctly for short? it should be shore if im correct
parallel_total = (1)/((1/res1) + (1/res2));

1/res1, 1/res2 ← Integer division, will equals zero if divizor > 1;
So your total can equals 0, 1, or 1/0 which causes an exception.

fix:
1.0/(1.0/res1 + 1.0/res2)

More on integer division: http://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division-in-c
Last edited on
CHECK THIS OUT.. IT WORKS BY ZARK SALMAN...
#include <iostream>
using namespace std;
int main()

{
short res1; //declared first variable
short res2; //declared second variable
short series_total;
float parallel_total;

cout << "\t\t\tWelcome to the Resistor Calculation Program!!!";
cout << "\n\nPlease enter your two resistances: ";

cin >> res1 >> res2;

series_total = res1 + res2;

parallel_total = (1)/((res1+res2)/res1*res2);

cout << "\nThank you!! You have entered " << res1 << " ohms and " << res2 << " ohms." << endl << endl;

cout << "If your resistors are placed in series, they will total to " << series_total << " ohms." <<endl;

cout << "If your resistors are placed in parallel, they will total to " << parallel_total << " ohms." <<endl <<endl;

cout << "Thank you for using RCP. Have a wonderful day!";


cout << "\n\n"; //Included for cleaner look

return 0;
}
(res1+res2)/res1*res2 is equal to ((res1+res2)/res1)*res2 and is equals to ((res1+res2)*res2/res1).
Your program uses wrong logic.
And if you use (res1+res2)/(res1*res2) The problem with integer division still persist.
Topic archived. No new replies allowed.