- #36
ineedhelpnow
- 651
- 0
can u give me an example on how to test it? i want to make sure that it also prints an error statement.
ineedhelpnow said:can u give me an example on how to test it? i want to make sure that it also prints an error statement.
a = 1.0
b = 1.0
c = 3.0
ineedhelpnow said:when i put those numbers in instead, the screen that displays the statement opens for like half a second then closes immediately :(
ineedhelpnow said:yes but the window should only close if their is something wrong with the program. now it won't even print error.
#include <iostream>
#include <cmath>
using namspace std;
int main () {
double a=0.0;
double b=0.0;
double c=0.0;
double s=0.0;
double r=0.0;
double areaofTriangle=0.0;
a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));
areaofTriangle=sqrt(r);
if (r>0) {
areaofTriangle = sqrt(r);
cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl;
cin >> areaofTriangle;
}
else {
cout << "Error." << endl;
}
return 0;
}
ineedhelpnow said:is it possible that the cin statement is causing a problem?
Code:#include <iostream> #include <cmath> using namspace std; int main () { double a=0.0; double b=0.0; double c=0.0; double s=0.0; double r=0.0; double areaofTriangle=0.0; a=1.0; b=1.0; c=3.0; s=(a+b+c) / 2; r=s*((s-a)*(s-b)*(s-c)); areaofTriangle=sqrt(r); if (r>0) { areaofTriangle = sqrt(r); cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl; cin >> areaofTriangle; } else { cout << "Error." << endl; } return 0; }
- - - Updated - - -
so i definitely don't need to use && commands for the if statement?
#include <iostream>
#include <cmath>
using namspace std;
int main () {
double a=0.0;
double b=0.0;
double c=0.0;
double s=0.0;
double r=0.0;
double areaofTriangle=0.0;
a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));
if (r>0) {
areaofTriangle = sqrt(r);
cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl;
cin >> areaofTriangle;
}
else {
cout << "Error." << endl;
}
return 0;
}
ineedhelpnow said:thats why i have if (r>0)
How do i test the value of r?
MarkFL said:The error is that you are computing the area of the triangle before testing the value of $r$. :D
Try:
Code:#include <iostream> #include <cmath> using namspace std; int main () { double a=0.0; double b=0.0; double c=0.0; double s=0.0; double r=0.0; double areaofTriangle=0.0; a=1.0; b=1.0; c=3.0; s=(a+b+c) / 2; r=s*((s-a)*(s-b)*(s-c)); if (r>0) { areaofTriangle = sqrt(r); cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl; cin >> areaofTriangle; } else { cout << "Error." << endl; } return 0; }
ineedhelpnow said:what did you do different? (mark you HAVE to get rid of the time gap between posts)
ineedhelpnow said:i removed that statement and its STILL not working (ps i appreciate all your help)
ineedhelpnow said:i didnt see a document but I am pretty sure I am doing the cout statements correctly. buuuuuuuut anyways I GOT IT I GOT IT I GOT IT I GOT IT! i needed a cin statement for the else command. thank u!
sure it wasMarkFL said:That was actually going to be my next suggestion...:D
#include <iostream>
#include <cmath>
using namespace std;
int main () {
double a=0.0;
double b=0.0;
double c=0.0;
double s=0.0;
double r=0.0;
double areaofTriangle=0.0;
a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));
if (r>0) {
areaofTriangle = sqrt(r);
cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl;
cin >> areaofTriangle;
}
else {
cout << "Error." << endl;
cin >> areaofTriangle;
}
return 0;
}
ineedhelpnow said:i actually didnt meet one of the conditions. that two of the sides has to be greater than one of them. or something like that...
ineedhelpnow said:(Shake) he failed me on the assignment because i missed that second condition. he's giving a second chance though. also in my program, i stored the variables a b and c which was wrong.
ineedhelpnow said:yeah i could present him with a present lol i don't think he would care. he told me that i had to satisfy both conditions. i don't understand why because the program would still run properly once i get rid of what i stored for a,b, and c but he said i have to put that second condition.
ineedhelpnow said:he wants us to fulfill the second condition because he wants us to be able to code it properly. to have us use all the different types of commands and functions.