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.
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.
@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.
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.
@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.
@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();?
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.
#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()
{
constdouble a = get_side( "one" ) ;
constdouble b = get_side( "two" ) ;
constdouble 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" ) returntrue ;
elseif( answer == "exit" ) returnfalse ;
return again() ; // invaid input, try again
}
int main()
{
show_instructions() ;
do run_calculator() ; while( again() ) ;
std::cout << "\nexit program.\n" ;
}