Opinions?

Apr 23, 2011 at 4:57pm
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 () 
{ 
using namespace 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; 
}
Last edited on Apr 23, 2011 at 5:16pm
Apr 23, 2011 at 5:06pm
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 using namespace 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!

Have you worked with function creation yet?
Apr 23, 2011 at 5:08pm
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.
Apr 23, 2011 at 5:18pm
Thanks guys,

@Ulti, I did getch(); since it's better then system("pause") I heard system("pause") isn't good so i used getch();, but i did forget std::cin
Apr 23, 2011 at 5:20pm
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?
Apr 23, 2011 at 5:22pm
Apr 23, 2011 at 5:26pm
Thanks
Apr 24, 2011 at 10:09pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cmath>
using namespace 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 . . .
Last edited on Apr 24, 2011 at 10:13pm
Apr 24, 2011 at 10:17pm
^Your main needs a return code...and system() is not really needed either.
Apr 24, 2011 at 11:18pm
Why would it need a return code? System("pause"); is needed to see the result.
Apr 25, 2011 at 1:05am
He means int main() rather than main(). Also, you should return 0 at the end of main. It means that the program exited normally.
Apr 25, 2011 at 2:14am
If the program exits normally, it will say "Press any key to continue . . ." Why would you have the need to make an unnecessary action of
int main()
and
return 0;
sorry, I'm having a difficult time understanding the purpose.
Apr 25, 2011 at 2:50am
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.
Apr 25, 2011 at 2:55am
Actually in C++, it is required to have main return an int.
Topic archived. No new replies allowed.