Program that'll give a squared value of z

  • Thread starter Thread starter JamesU
  • Start date Start date
  • Tags Tags
    Program Value
Click For Summary

Discussion Overview

The discussion revolves around writing a simple C++ program to calculate the squared value of a number, specifically focusing on syntax issues and the use of functions like pow and sqrt. Participants also explore how to correctly handle input and output in the program.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant shares initial code for squaring a number but encounters syntax errors, particularly with the use of cin and endl.
  • Another participant suggests using pow and sqrt functions from the cmath library, providing examples of their usage.
  • There is a question about whether variable names can be used in the parentheses of pow.
  • Participants discuss the necessity of declaring variables before use in C/C++, emphasizing that C/C++ is a typed language.
  • Multiple participants point out missing semicolons and the incorrect use of endl in the input statement.
  • One participant suggests that adding cout is necessary to display the result of the calculation.

Areas of Agreement / Disagreement

Participants generally agree on the need for correct syntax and variable declaration in C++, but there are differing opinions on the necessity of including the cmath library for simple squaring operations. The discussion remains unresolved regarding the best practices for input and output formatting.

Contextual Notes

Some participants express uncertainty about formatting and the use of whitespace in the code. There are also unresolved questions about the necessity of certain libraries for basic operations.

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;
count << 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:
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K