Please check my code

Pages: 12
Hey guys I'm a beginner in C++ (learning overloads and templates in tutorials). I made a small project and I would like you guys to have a look at it. Here is a snippet. Please tell me how to make it better and some constructive criticism. Thanks!!!
EDIT:
Here it is sorry. I didn't know how to show all code. This is a calculator. A calculator where you enter 2 sides of the right triangle and it figures out the missing side (Pythagorean Theorem).
I made the program restart by copying the code in int main () and pasting it in void restart (). I didn't use a loop because I kept running into problems. I cleared the screen with string (50, '\n'). But I'm pretty sure there are other better ways.
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
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

void instructions()
{
	cout << "Instructions:\n";
	cout << "1. Enter length of 1st side of right\ntriangle.\n";
	cout << "2. Enter length of 2nd side of right\ntriangle.\n";
	cout << "3. The process of the calculation is shown and the final answer is the missing side\nlength.\n\n";
	cout << "Tips:\n";
	cout << "Press the \"enter\" key after you type\nsomething\n";
	cout << "Press \"more\" and press \"restart\" to do\nanother calculation\n";
	cout << "Press \"more\" and press \"toggle soft\nkeyboard\" to use keyboard\n";
	cout << "Press \"more\" and press \"preferences\" to\nchange settings like font size, text color,\netc\n\n";
	cout << "IMPORTANT:\n";
	cout << "Only numbers will be accepted!\nAnything else will cause a\ncalculation error!\n\n";
}
void restart()
{
	cout << string (50, '\n');
    int a, b;
    string exit;
    
    cout << "[ Pythagorean Theorem Calculator ]\n(Right Triangle)\n\n";
	instructions();
    cout << "Length of 1st side of triangle: ";
    cin >> a;
    cout << "Length of 2nd side of triangle: ";
    cin >> b;
    
    a *= a;
    b *= b;
    double pyth;
    pyth = sqrt(a+b);
    
    cout << endl;
    cout << "a^2 + b^2 = c^2" << endl;
    cout << sqrt(a);
    cout << "^2";
    cout << " + ";
    cout << sqrt(b);
    cout << "^2";
    cout << " = ";
    cout << "?" << endl;
    cout << a;
    cout << " + ";
    cout << b;
    cout << " = ";
    cout << a + b << endl;
    cout << "SQRT of ";
    cout << a + b;
    cout << " is ";
    cout << sqrt(a+b);
    cout << "!" << endl;
    cout << endl;
    cout << "Missing side length: ";
    cout << pyth << '\n' << '\n';
    cout << "Type \"exit\" to exit and \"again\" to\ndo another calculation: ";
    cin >> exit;
    
    if(exit=="again")
    restart ();
    
    else
    cout << "Exiting...";
}
int main()
{
    int a, b;
    string exit;
    
    cout << "[ Pythagorean Theorem Calculator ]\n(Right Triangle)\n\n";
	instructions();
    cout << "Length of 1st side of triangle: ";
    cin >> a;
    cout << "Length of 2nd side of triangle: ";
    cin >> b;
    
    a *= a;
    b *= b;
    double pyth;
    pyth = sqrt(a+b);
    
    cout << endl;
    cout << "a^2 + b^2 = c^2" << endl;
    cout << sqrt(a);
    cout << "^2";
    cout << " + ";
    cout << sqrt(b);
    cout << "^2";
    cout << " = ";
    cout << "?" << endl;
    cout << a;
    cout << " + ";
    cout << b;
    cout << " = ";
    cout << a + b << endl;
    cout << "SQRT of ";
    cout << a + b;
    cout << " is ";
    cout << sqrt(a+b);
    cout << "!" << endl;
    cout << endl;
    cout << "Missing side length: ";
    cout << pyth << '\n' << '\n';
    cout << "Type \"exit\" to exit and \"again\" to\ndo another calculation: ";
    cin >> exit;
    
    if(exit=="again")
    restart();
    
    else
    cout << "Exiting...";
    
    return 0;
}
Last edited on
Please tell me how to make it better


Please tell us what it's supposed to do. You're structure could be called 'odd-ball' if this were surrealist art. Otherwise, it's a bit confusing there bud.
closed account (E0p9LyTq)
What are the values for a and b?

a and b are not assigned any values, so use whatever garbage value is in the memory location.
Last edited on
@FurryGuy: You're asking someone who is grasping at straws to explain why they are doing 'X'... Identify their purpose, then where they went wrong and THEN determine where they went askew.
Last edited on
Have you tried compiling your code yet?

That's always a good way to start
Sorry guys look at the code now. The code is a calculator. Enter 2 sides of a right triangle and it figures out the missing side. Pythagorean Theorem.
Last edited on
Better to use a loop than to call the function recursively.
Last edited on
And what do you mean using a loop THEN calling the function?

Sorry, that was my fault. Classic grammar mistake: I meant to write "than", not "then". Meaning, its superior to write all the code in the main function in a loop (if the user chooses to go again), rather than recursively calling the restart() function.
Last edited on
http://www.cplusplus.com/doc/tutorial/

1
2
3
4
5
int main()
{
cout << "Hello";
main();
}


That's a troll tactic.
closed_account_5a8Ym39o6/
Difference between program_entry(); and return program_entry()???
So is this better?
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
int main()
{
    int a, b;
    string exit;
    
    do {
    cout << "[ Pythagorean Theorem Calculator ]\n(Right Triangle)\n\n";
	instructions();
    cout << "Length of 1st side of triangle: ";
    cin >> a;
    cout << "Length of 2nd side of triangle: ";
    cin >> b;
    
    a *= a;
    b *= b;
    double pyth;
    pyth = sqrt(a+b);
    
    cout << endl;
    cout << "a^2 + b^2 = c^2" << endl;
    cout << sqrt(a);
    cout << "^2";
    cout << " + ";
    cout << sqrt(b);
    cout << "^2";
    cout << " = ";
    cout << "?" << endl;
    cout << a;
    cout << " + ";
    cout << b;
    cout << " = ";
    cout << a + b << endl;
    cout << "SQRT of ";
    cout << a + b;
    cout << " is ";
    cout << sqrt(a+b);
    cout << "!" << endl;
    cout << endl;
    cout << "Missing side length: ";
    cout << pyth << '\n' << '\n';
    cout << "Type \"exit\" to exit and \"again\" to\ndo another calculation: ";
    cin >> exit;
    
    if(exit=="again")
    cout << string (50, '\n';
    
    else
    cout << "Exiting...";
    } while (exit=="again");

    return 0;
}
Last edited on
closed account 5a8Ym39o6
WOW 6EQUJ5

Both of you are trolling.
@TheIdeasMan
How am I trolling?
@TheIdeasMan
You know that trolls are craving for attention, don't you?
So permanently jumping on the troll bandwagon just feeds the trolls...
@TheIdeasMan
@coder777
Alright!! What makes you guys think I'm trolling? I was just asking if I could call a function recursively using main();. If you look at my code it's not a troll at all. It's a pythagorean theorem calculator.
What makes you guys think I'm trolling?
I dont' think so. I want to remind TheIdeasMan not to feed trolls.
@coder777
Look. I'm the one who posted this question. I wanted to make my code better. And now you're calling my a troll... because TheIdeasMan said main(); was a troll tactic? Or maybe the fact I posted main();?
@WOW 6EQUJ5:

The code you posted with the do-while loop seems correct. Only thing I would say is to call instructions() outside of the loop. No point in giving instructions to the user multiple times.
Last edited on
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
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

void show_instructions()
{
	std::cout << "Instructions:\n"
	             "    1. Enter length of 1st adjacent side of right-angled triangle.\n"
	             "    2. Enter length of 2nd adjacent side of right-angled triangle.\n"
	             "    3. The process of the calculation is shown\n"
	             "       and the final answer is the length of the hypotenuse.\n\n";
}

double get_side( std::string side_name )
{
    std::cout << "\nenter a positive value for length of side '" << side_name << "': " ;

    double side ;
    if( std::cin >> side && side > 0 ) return side ; // valid input, return it

    // invalid input
    std::cout << "please enter a positive number\n" ; // inform the user
    std::cin.clear() ; // clear possible error because of non-numeric input
    std::cin.ignore( 1000000, '\n' ) ; // empty the input buffer
    return get_side(side_name) ; // and try again
}

void run_calculator()
{
    const double a = get_side( "one" ) ;
    const double b = get_side( "two" ) ;

    const double c = std::sqrt( a*a + b*b ) ;

    std::cout << std::fixed << std::setprecision(2) << "\n\n"
              << a << '*' << a << " + " << b << '*' << b << " == " << c << '*' << c
              << "\n\nlength of the hypotenuse: " << c << '\n' ;
}

bool again()
{
    std::cout << "\ntype 'again' to perform another calculation, 'exit' to exit: " ;

    std::string answer ;
    std::getline( std::cin >> std::ws, answer ) ; // std::cin >> std::ws skip leading white-space

    if( answer == "again" ) return true ;
    else if( answer == "exit" ) return false ;

    return again() ; // invaid input, try again
}

int main()
{
    show_instructions() ;

    do run_calculator() ; while( again() ) ;

    std::cout << "\nexit program.\n" ;
}
WOW 6EQUJ5 wrote:
And now you're calling my a troll...
I did not call you a troll and this is the last post regarding this.
Pages: 12