I want opinions of this script I made in C++ like a month ago when I first started. Although I edited it and it's more efficient as it used to be. =P
So what do you think? Hopfully we can have a really good C++ discussion here, and maybe if we do I may learn more and further my skills in C++ a little more. :)
Script here:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <cmath>
int main ()
{
double num1;
std::cout << "Pick your number.." << std::endl;
cin >> num1;
double num2;
num2 = sqrt(num1);
std::cout << "The sqaure root of " << num1 << " is " << num2 << std::endl;
getch();
return 0;
}
This brings up an output in any compiler, asks you to "Pick your number", and when you enter that specific number it give you the Sqrt of that specific number you entered. :} <-- May be easy for others, but for a beginner at the time it's pretty good.
What do you guys think of this for a beginner at C++ at that time? Well now I am basic, but I just want opinions on that.
Thanks!
I did have this when I started
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <cmath>
int main ()
{
usingnamespace std;
double num1;
cout << "Pick your number.." << endl;
cin >> num1;
double num2;
num2 = sqrt(num1);
cout << "The sqaure root of " << num1 << " is " << num2 << endl;
system("pause");
return 0;
}
Well it's interesting that you std::cout but you don't std::cin =] I believe getch(); is in the conio.h header file. (could be wrong). Oh and the second program is missing a # in front of include <iostream>
So what I gather from the differences between the two, you now dislike usingnamespace std;, and you've learned not to use system("pause");?
I remember when I started and did stuff of a similar difficulty level, well done, keep it up, I hope you keep learning and improving!
The newer one is a little better, but it doesn't look like you need num2. You should also indent your code. Also, "Pick your number.." could be "Input a number to evaluate the sqaure root." or something similar to tell the user why he's picking a number.
So what do you think? Hopfully we can have a really good C++ discussion here, and maybe if we do I may learn more and further my skills in C++ a little more. :)
Unfortunately I don't see much to discuss with such a short code.
Also at Ulti. I have a hard time with function creating since i forget how it works.I know it's void, but i don't know how to call the function, nor define it. Wanna help on that?
#include <iostream>
#include <cmath>
usingnamespace std;
main()
{
double Number = 0.0;
cout << "Enter a number: ";
cin >> Number;
cout << "The square root of " << Number << " is ";
Number = sqrt(Number);
cout << Number << endl;
system("pause");
}
That is the most efficient way to go about that code as far as I can tell. Is that what you were looking for? Declaring two variables takes more processing power than one. So, this is more efficient than both those codes above. Initializing your variables that contain numbers is a good practice to follow as well.
The output of this code is:
Enter a number: 25
The square root of 25 is 5
Press any key to continue . . .
What they are trying to say that it is better practice to have your main return a value, in this case 0. The reason for this is that you know that your main return no errors and exited correctly i.e. by returning a value (0).
There has been a long thread about the use of system("pause") because the fact that it is platform specific and is a security flaw. I would advise not using it in an example to a beginner programmer.