Program that'll give a squared value of z

  • Thread starter Thread starter JamesU
  • Start date Start date
  • Tags Tags
    Program Value
AI Thread Summary
The discussion revolves around creating a simple C++ program to calculate the square of a number and later to find its square root. Initial code attempts contained syntax errors, such as incorrect use of `cin` and missing semicolons. Key points include the necessity of declaring variables before use, as C++ is a typed language. The correct input statement should not include `endl`, which is meant for output formatting. It was also emphasized that every command must end with a semicolon. To display the squared value, a `cout` statement was added to output the result of `pow(x, 2.0)`. Additionally, the `cmath` library is not required for simple squaring, but it is useful for functions like `sqrt`. Overall, the final code successfully captures user input, computes the square, and displays the result.
JamesU
Gold Member
Messages
821
Reaction score
3
I'm writing a simple program that'll give a squared value of z, a number

Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    double z;
    cout<<"enter an integer"<<
    cin>>z;>>
    cout<<"The squared value of your integer is:"<<z*z<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

my editor is telling me that it's wrong :|

and how would I make a program that can find a square root? :smile:
 
Computer science news on Phys.org
#include<cmath>

pow(2.0,2.0) => 4.0
sqrt(4.0) => 2.0
 
and in the parnthesis i would put variable names?
 
yes:

pow(x,y) will yield the vaule of x to the power of y. You can use variables.
 
I put in this: cin>>x>>endl;

and it says: "x does not name a type"
 
dduardo said:
#include<cmath>

pow(2.0,2.0) => 4.0
sqrt(4.0) => 2.0

that is superfluous. you do not need the cmath library to do a simple square.

cin >> z;
z = z*z;
cout << z;
 
yomamma said:
I put in this: cin>>x>>endl;

and it says: "x does not name a type"


are you declaring your variables before you use them?

C/C++ is a typed language (not as strong as it could be, but it is)

you need a statement declaring the variable and the type before you use it.

int x;
 
okay, here's my code:

#include <cstdlib>
#include <iostream>
#include<cmath>

using namespace std;

int main(int argc, char *argv[])
{
int x;
cin>> x >>endl;
pow(x,2.0)
system("PAUSE")
return EXIT_SUCCESS;
}
 
1) Why do you have >> endl; at the end of the cin statement? endl is equivalent to "\n".
2) What happened to the semicolons after pow(x,2.0) and system("PAUSE")?
3) Why aren't you outputting anything?
 
  • #10
yomamma said:
okay, here's my code:

#include <cstdlib>
#include <iostream>
#include<cmath>

using namespace std;

int main(int argc, char *argv[])
{
int x;
cin>> x >>endl;
pow(x,2.0)
system("PAUSE")
return EXIT_SUCCESS;
}

You need to not use a endl in your input statement. That is an element of an output statement.
 
  • #11
Code:
#include <cstdlib>
#include <iostream>
#include <cmath>   //maybe you should also add whitespace here, I'm not sure

using namespace std;

int main(int argc, char *argv[])
{
int x;
cin>> x;            // you don't have to add endl here because when you will enter some number and hit enter you will automatically jump into a new line... so no need for formatting here
pow(x,2.0);    //you have to add ";" behind every command/line
system("PAUSE");
return EXIT_SUCCESS;
};
 
  • #12
Code:
#include <cstdlib>
#include <iostream>
#include <cmath>   //maybe you should also add whitespace here, I'm not sure

using namespace std;

int main(int argc, char *argv[])
{
int x;
cin >> x;            // you don't have to add endl here because when you will enter some number and hit enter you will automatically jump into a new line... so no need for formatting here
cout << pow(x,2.0);    //you have to add ";" behind every command/line
system("PAUSE");
return EXIT_SUCCESS;
};

Added a cout. No sense in calculating the power and not display or use it in some way, shape, or form.
 
  • #13
I got it :smile:
 
Back
Top