Pythagorean Theorem Program Help?

closed account (DEyAqMoL)
Hi guys, before I post the code, I want to give you some background. I'm a programmer/ gamer guy and I have been for a long time now. It's just that I am now just getting back to using C++ after a long vacation in Python- land, and so I decided to write something, procedurally, elementary, like a program that uses the Pythagorean Theorem. However, my 1337 coder skills prove rusty and there seems to be a couple bugs in here I can't find. Apart from some missing semi- colons- which I know there are are some- can you here in the C++ forums help me try to find bugs? Is there something big and obvious that I'm missing here just because I'm a little rusty? Do I look stupid? Well, I always look stupid, but..

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <math.h>
#include <string>

//pyth.cpp
//a^2+b^2=c^2 -- The Pythagorean Theorem
//This C++ program takes information inputted about a triangle, and using the Pythagorean Theorem:
//Either solves for a, b, or c, and displays properties about a simulated triangle with those variables
//For example, we would find and output the area, perimeter, etc. of that triangle
//I would say to credit this if you used it in your schoolwork, but you probably won't anyway
//I do accept payment, donations,  and gifts of appreciation in the form of money and women
//(c)2012 Ty K.

using namespace std;

string AskIfGoAgain(){
	int loop = 1;
	string answer = "y";
	while (loop == 1){
		answer = "y";
		cout << "\nWould you like to calculate another triangle? [y]/[n]";
		cin >> answer;
		if (answer == "y"){return answer;}
		else if (answer == "n"){return answer;}
		else {
			cout << "\n That wasn't even an option. Seriously?";
		}
	}
}

int SolveForA(){
	float b, c, bSquared, cSquared, a;
	//Initializes the variables
	float area, perimeter;
	//Initializes the other properties we are going to find when we simulate the triangle
	cout << "What is b?";
	cin >> b;
	//Prompts user to input b
	cout << "\nWhat is c?";
	cin >> c;
	//Promts user to input c
	bSquared = b*b;
	cSquared = c*c;
	//Finds each variable's square.. if that makes grammatical/syntactical sense
	a = cSquared - bSquared;
	//Finds a
	cout << "\na = ";
	cout << a;
	//Tells you what A is
	area = (a + b) / 2;
	perimeter = a + b + c;
	cout << "\n Perimeter: ";
	cout << perimeter;
	cout << "\n Area: ";
	cout << area;
	//Simulates the triangle and outputs the area and the perimeter
	cout << "\n\nThank you for using one of my programs! And remember, thanks can be expressed in money and women!";
	return 0;
}

int  SolveForB(){
        float b, c, bSquared, cSquared, a;
        //Initializes the variables
        float area, perimeter;
        //Initializes the other properties we are going to find when we simulate the triangle
        cout << "What is a?";
        cin >> a;
        //Prompts user to input a
        cout << "\nWhat is c?";
        cin >> c;
        //Promts user to input c
        aSquared = a*a;
        cSquared = c*c;
        //Finds each variable's square.. if that makes grammatical/syntactical sense
        b = cSquared - aSquared;
        //Finds a
        cout << "\nb = ";
        cout << b;
        //Tells you what A is
        area = (a + b) / 2;
        perimeter = a + b + c;
        cout << "\n Perimeter: ";
        cout << perimeter;
        cout << "\n Area: ";
        cout << area;
        //Simulates the triangle and outputs the area and the perimeter
        cout << "\n\nThank you for using one of my programs! And remember, thanks can be expressed in money and women!";
        return 0;
}
int SolveForC(){
	float a, b, c, aSquared, bSquared, cSquared;
	//Initialize the variables we will be using in this function
	float area, perimeter;
	//Initialize the variables of the properties of the triangle we will simulate
	cout >> "\nWhat is a?: ";
	cin << a;
	cout >> "\nWhat is b?: ";
	cin << b;
	//Prompts for and gets input for a and b
	aSquared = a*a;
	bSquared = b*b;
	//Finds the squares of a and b
	cSquared = aSquared + bSquared;
	c = sqrt(cSquared);
	//Finds c
	cout << "\nc: ";
	cout c;
	//outputs c
	area = (a*b)/2;
	perimeter = a + b + c;
	cout << "\nPerimeter: ";
	cout perimeter;
	cout << "\nArea: ";
	cout area;
	//Simulates the triangle and finds the value of those properties
}	
int main(answer){
	answer = "y"
	while (answer == "y"){
		float choice;
		cout << "Would you like to solve for [a], [b], or [c]?: ";
		cin >> choice;
		if (choice == "a"){
			SolveForA();
			AskIfGoAgain();
		}
		else if (choice == "b"){
			SolveForB();
			AskIfGoAgain();
		}
		else if (choice == "c"){
			SolveForC();
			AskIfGoAgain();
		else {
			cout << "That wasn't even an option. Seriously?";
		}
	}
	return 0;
}			
I think you are confusing terms. A "bug" is something different from a compiler error. Programs with bugs compile okay, but do not work as expected. Since you seem to be asking about compiler errors and not about bugs, I just felt I should clarify.

Also, when posting about compiler errors, it helps if you actually tell us what the error is and what line it's on, so we're not searching for a needle in a haystack.


That said, I see this:

1
2
int main(answer){
	answer = "y"


main can take zero parameters, or it can take the "argc/argv" parameters (which hold commandline options). That's it. You can't make it take 'answer'. And even if you could, you'd have to specify a type for answer (is it an int? a string?)

Make main parameterless, and instead make answer a variable inside the function:

1
2
int main(){
    string answer = "y";



As for logic errors / bugs:

1) 'answer' in main is always "y" (you never change it), so your program will never exit. I'm assuming you meant to set answer to whatever AskIfGoAgain returns:

 
answer = AskIfGoAgain();


2) You are calculating a and b incorrectly. You need to sqrt to calculate any one of the missing values. Strangely, you remembered to do this when calculating c, but forgot when calculating a and b.
Topic archived. No new replies allowed.