# C++ Problem

Discussion in 'Software Development' started by ejspin, Feb 16, 2005.

Not open for further replies.

Joined:
Jan 5, 2005
Messages:
89
I need to type up this program for an assignment, but I keep getting the same output for different inputs for this one thing(in bold):
-------------------------------------------------------------------
#include <stdio.h>
#include <math.h>

/* Defining symbolic constants*/
#define ROW 3.57
#define K 4.0/3.0

int main(void)
{
/* Establishing variables*/
double xPharaoh, yPharaoh, xServant, yServant, distance;
double xDifference, yDifference;
double h1, h2, d;
double side1, side2, side3;

/* Reading in the coordinants, height and length*/
printf("What is the pharaoh's x coordinate?\n");
scanf("%i", &xPharaoh);
printf("What is the pharaoh's y coordinate?\n");
scanf("%i", &yPharaoh);
printf("What is the servant's x coordinate?\n");
scanf("%i", &xServant);
printf("What is the servant's y coordinate?\n");
scanf("%i", &yServant);
printf("What is the height of the Pharaoh's radio antenna?\n");
scanf("%i", &h1);
printf("What is the height of the servant's radio antenna?\n");
scanf("%i", &h2);
printf("What is the length of the first side of the triangle?\n");
scanf("%i", &side1);
printf("What is the length of the second side of the triangle?\n");
scanf("%i", &side2);
printf("What is the length of the third side of the triangle?\n");
scanf("%i", &side3);
printf("------------------------------------------------------\n\n");

/* calcuate difference for x and y */
xDifference = xPharaoh - xServant;
yDifference = yPharaoh - yServant;

/* Distance between points */
distance = sqrt(xDifference * xDifference + yDifference * yDifference);

d = ROW * (sqrt(K*h1) + sqrt(K*h2));

/* Calculate distance range*/
if (distance<=d) {
printf("Close enough to communicate\n");
}
else {
printf("Distance is too far apart, unable to communicate\n");
}

/* Determinig triangle sides*/
if (side1 == side2 && side1 == side3) {
if (side3 = sqrt(pow(side1, 2.0) + pow(side2, 2.0)) ) {
printf("The servant must paint an equilateral triangle\n");
}
else if (side1==side2 && side2!=side3 || side1==side3 && side3!=side2 || side2==side3 && side3!=side1) {
if (side3 = sqrt(pow(side1, 2.0) + pow(side2, 2.0)) ) {
printf("The servant must paint an isosceles triangle\n");
}
}
else if (side1!=side2 && side2!=side3) {
if (side3 = sqrt(pow(side1, 2.0) + pow(side2, 2.0)) ) {
printf("The servant must paint a scalene triangle\n");
}
}
}
else {
printf("Invalid triangle\n"); }

printf("\nEnd of program\n");

return 0;
}
-----------------------------------------------------------------------
My output keeps coming out "Close enough to communicate", even though some are not.

Any suggestions?

Joined:
Apr 30, 2001
Messages:
2,636
That's not c++. That's c.

In c++, you can use c methods of course, but you include the standard c wrappers for the c headers. Also, the c wrappers undefine the original c functions and redifine them in the std namespace. Although some of them are also still defined outside the std namespace, you should use the std namespace ones. You can do that globally by the 'using namespace std;" line or by doing std::scanf(), std:rintf() etc.

return is not needed at the end of a c++ program.

void is not needed if you are not using arguments. just do int main().

Also, instead of using printf(), you can include <iostream> and use cout and endl. For example: cout << "\n" << "Hello, wordl!" << endl;
(endl prints out "\n" and then flushes the buffer. cout and endl are also part of the std namespace)

Instead of scanf(), you can use cin. For example:

double x;
cin >> x;

Also, you can do it any way you want, but it's a good idea to declare a variable near where it is first used if you can help it.

All that aside your program won't even compile (on a good compiler at least) mainly because the scanf() %i parameter is for integers, but you're using doubles. You can use a modifier though to properly accept a double.

Also, if you are initializing a variable when you declare it and you don't instead of changing it again, make it a constant. e.g. const string x = "bla".

Now the version below displays different results depending on what you input.

Code:
```#include <cstdio>
#include <cmath>

#define ROW 3.57
#define K 4.0 / 3.0

using namespace std;

int main() {

/* Reading in the coordinants, height and length*/
printf("What is the pharaoh's x coordinate?\n");
double xPharaoh;
scanf("%lf", &xPharaoh);

printf("What is the pharaoh's y coordinate?\n");
double yPharaoh;
scanf("%lf", &yPharaoh);

printf("What is the servant's x coordinate?\n");
double xServant;
scanf("%lf", &xServant);

printf("What is the servant's y coordinate?\n");
double yServant;
scanf("%lf", &yServant);

printf("What is the height of the Pharaoh's radio antenna?\n");
double h1;
scanf("%lf", &h1);

printf("What is the height of the servant's radio antenna?\n");
double h2;
scanf("%lf", &h2);

printf("What is the length of the first side of the triangle?\n");
double side1;
scanf("%lf", &side1);

printf("What is the length of the second side of the triangle?\n");
double side2;
scanf("%lf", &side2);

printf("What is the length of the third side of the triangle?\n");
double side3;
scanf("%lf", &side3);

printf("------------------------------------------------------\n\n");

/* calcuate difference for x and y */
const double xDifference = xPharaoh - xServant;
const double yDifference = yPharaoh - yServant;

/* Distance between points */
const double distance = sqrt( xDifference * xDifference + yDifference * yDifference);
const double d = ROW * (sqrt(K*h1) + sqrt(K*h2));

/* Calculate distance range*/
if ( distance <= d) {
printf("Close enough to communicate\n");
} else {
printf("Distance is too far apart, unable to communicate\n");
}
[color=red]
/* Determinig triangle sides*/
if (side1 == side2 && side1 == side3) {
if (side3 == sqrt(pow(side1, 2.0) + pow(side2, 2.0)) ) {
printf("The servant must paint an equilateral triangle\n");
} else if (side1 == side2 && side2 != side3 || side1 == side3 && side3 != side2 || side2 == side3 && side3 != side1) {
if (side3 == sqrt(pow(side1, 2.0) + pow(side2, 2.0)) ) {
printf("The servant must paint an isosceles triangle\n");
}
} else if (side1 != side2 && side2 != side3) {
if (side3 == sqrt(pow(side1, 2.0) + pow(side2, 2.0)) ) {
printf("The servant must paint a scalene triangle\n");
}
}
} else {
printf("Invalid triangle\n");
}
[/color]
printf("\nEnd of program\n");
}
```

However, you also had a few if ( side3 = sqrt..) instead of if (side3 == sqrt...).

You need to redo the triangle check part anyway. The way you have it, only an equalateral triangle could be valid. It also looks like you are using a form of the Pythagorean theorem to determine if a triangle is valid. That won't work.

Since the sum of every two sides of a triangle must be greater than the third side, you can use that as a rule.

The check should go something like this:

If each set of 2 sides is greater than the other side, go ahead and check for the type of triangle. If the rule is not satisfied, then it's an invalid triangle.

If you show effort on the the triangle check, we can show you how to do it.

Joined:
Jan 5, 2005
Messages:
89
Ok I did what you told me to do and this is what i came up with:

/* Determinig triangle sides*/
if (side1+side2 > side3 && side2+side3 > side1 && side1+side3 > side2) {
if (side1 == side2 && side1 == side3) {
printf("The servant must paint an equilateral triangle\n");
}
else if (side1 == side2 && side2 != side3 || side1 == side3 && side3 != side2 || side2 == side3 && side3 != side1) {
printf("The servant must paint an isosceles triangle\n");
}
else if (side1 != side2 && side2 != side3) {
printf("The servant must paint a scalene triangle\n");
}
}
else {
printf("Invalid triangle\n");
}

Joined:
Apr 30, 2001
Messages:
2,636
Yep, that's what I meant. ( I used parenthesis just in case)

Code:
```if ( (side1 + side2) > side3 && (side2 + side3) > side1 && (side1 + side3) > side2)  {
if (side1 == side2 && side1 == side3) {
printf("The servant must paint an equilateral triangle\n");
} else if (side1 == side2 && side2 != side3 || side1 == side3 && side3 != side2 || side2 == side3 && side3 != side1) {
printf("The servant must paint an isosceles triangle\n");
} else if (side1 != side2 && side2 != side3) {
printf("The servant must paint a scalene triangle\n");
}
} else {
printf("Invalid triangle\n");
}
```
When posting code, put it between the code tags so it's easier to read and copy.

&#91;code&#93;
code
&#91;/code&#93;

Also, for posting on forums, if your code is indented with 4 spaces and not tabs, it will look a lot better when posted. (a tab will show up as 8 spaces and will look over-indented)

Joined:
Jan 5, 2005
Messages:
89
Thanks for the tips, and all your help

As Seen On