help with c++ programing

The program is supposed to take in two numbers from the user and display them separated by a comma, with the smallest one displayed first. This program does run, so there are no syntax errors, but you need to correct it based on the following description.

The program is supposed to check and make sure that both numbers are positive. Only if they are positive, should it display the numbers separated by commas. If both numbers are not positive, the program should only display “Error: Negative Numbers!”.

Please correct the logic errors and submit updating comments at the top of the program.

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
#include <iostream>

using namespace std;

int main()
{	
	//declare variables
	int num1 = 0;
	int num2 = 0;
	int temp = 0;

	//enter input
	cout << "Enter the first number: ";
	cin >> num1;
	cout << "Enter the second number: ";
	cin >> num2;

	if (0 > num1 || num2 > 0)	{ //check positive numbers
		//if necessary, swap numbers
		if (num2 >= num1)	{
			temp = num1;
			num1 = num2;
			num2 = num1;
		}	//end if
	}
	else	{	//not positive
		cout << "Error: Negative Numbers!" << endl;
	}
	
	//display output
	cout << num1 << ", " << num2 << endl;
	
	return 0;
}	//end of main function 
Last edited on
Please explain:
the dimentions of a triangle


For example, you do read in a "side". A triangle has three sides, does it not?
Does one "side" apply to all three?
If not, what other "dimentions" will you read?

You surely know the math about triangles?
Hello anthony2011,

To start with "double' is the preferred floating point type these days. A "double" has greater precision than a "float" and will store a number better. It also does a better job in calculations.

using namespace std; // <--- Best not to use.

In your for loops They are generally written as for (int y = 1; y <= sideVariable; y++). Unless you need to define the loop iterator outside of the for loop for later use it is best to specify the type of the iterator inside the for loop. (y++ ) is the form generally used and is the same as (y += 1).

It looks like line 34 is where you want to add to the program. Although "sideVariable" may not be the best name here it looks like you could use this variable for the height. To figure the perimeter, and area of a triangle you will need at least one more variable. I am thinking "base", but I never was very good at geometry.

You can put these calculations right in "main", but if you know about functions I would suggest using functions.

Your last bit of code would be to print out what you have done.

Come up with something and post it. Even if it is wrong or a problem at least it is something to work with.

Hope that helps,

Andy
you can find these 3 values with any of the standard S(ide) / A(ngle) combos if these are right triangles. eg SAS so the first thing to think about is how you want to input the data that defines your triangle, one way or all ways or via xy points or whatever. If they are not right triangles, you may want to just get x/y coordinates or the 3 side lengths.

once you have the triangle defined, the rest is pretty basic, but note that trig functions in c++ (and most languages) use radians for angles (easy convert, 180 degrees = pi radians). If its not a right triangle, you probably need trig to get the height.
Last edited on
Perimeter (easy): P = a + b + c

Area from Heron's formula A= sqrt( s(s-a)(s-b)(s-c) ), where s = P/2
https://en.wikipedia.org/wiki/Heron%27s_formula

One angle by using the cosine rule, then use this angle and a relevant side to get the "height". (Do you really need it? ... it's ambiguous - because there would be 3 possibilities - and you can get the area from Heron's formula anyway).
Last edited on
if you have the x/y coordinates of the 3 corners the height is pretty easy, its the biggest y value if its sitting on the x axis, and if not, a bit more logic to find it via coordinates but still not too bad. Distance formula...
Last edited on
Topic archived. No new replies allowed.