LightbulbSqrt program

Posts 1–7 of 7 · Page 1 of 1
Sqrt program
Hi, Plump here. I have made a pretty simple program. I didn't post the .exe due to the simplicity of the file. But, it's my first program so, gimme some suggestions on it.

// Calculator App.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"

using namespace std;

int main()
{
cout <<"Plumps Calculator!" << endl;
return 0;
}

using namespace std;

int main ()
{
int x = 0;
while(x < 10) {
double y = sqrt((double)x);
cout << "The Sqaure root of" << x << "equals" << y <<endl;
x++;
}
return 0;
}
Use [code] next time it make it easy to read.
why do you have 2 int main() ?


change
cout << "The Sqaure root of" << x << "equals" << y <<endl;
to
cout << "The Sqaure root of " << x << " equals " << y <<endl; //note the extra spaces.


8 lines. looks good.

edit: The command-prompt will close as soon as the return statement is hit - This means the user won't actually have enough time to read what's on the screen.

You can pause the screen by waiting for user input

Code:
char zz;
cin >> zz; <-- will wait for the user to type something and press {enter}. Technically the variable is never used and the return statement would be executed a moment later.
return 0;
}
2. on Windows(R), use
Code:
system("PAUSE");
return 0;
}
to get the familiar "Press any key to continue..." message. Once any key is pressed the return will get executed and program closes.

^^Your development environment might keep the window open for you / have it's own output window -- but when you share the actual .exe with someone, it will close as soon as the return 0; executes.

And yes plz use [ code ] [ / code] tags to make webpage/code more readable. I think [ b ] (bold) [ / b ] doesn't work inside a [ code ], so sometimes I use [ quote ] [ / quote] because it does allow bold (if you're drawing attention to a specific line(s) of code).

[ c o d e ] (without spaces)
Code:
int x;
int y;
int z;
[ / c o d e ] (without spaces)

[ q u o t e ] (without spaces)
int x;
int y;
float z;
[ / q u o t e] (without spaces)
Don't use using namespace std. If you really must import a name into any scope do it at function scope using a using declaration instead of a directive. If the namespace is long then simply use a namespace alias. It's not hard to type the namespace prefix. Never use system() unless absolutely necessary.
Sounds good, I will keep that in mind
Console applications are 'meant' to be run through command line(so output stays visible after execution)
Posts 1–7 of 7 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us