sqrt() problem

Hello,
I'm reading programming principles and practice using c++ and I don't understand this exercise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "std_lib_facilities.h"
int main()
{
	cout << "Please enter a floating-point value: ";
	double n;
	cin >> n;
	cout << "n== " << n
		<< "\nn+1== " << n + 1
		<< "\nn-1== " << n - 1
		<< "\nn%2== " << n % 2
		<< "\nthree times n== " << 3 * n
		<< "\ntwice n== " << n + n
		<< "\nn squared== " << n*n
		<< "\nhalf of n==" << n / 2
		<< "\nsquare root of n== " << sqrt(n)
		<< "\n";
    keep_window_open();
	return 0;
}


Task is: Get this little program to run. Then, modify it to read an int rather than a double. Note that sqrt() is not defined for an int so assign n to a double and take sqrt() of that.
For me, it's working with int too(I can take sqrt() of that). Question is, how can I assign n to double in the middle of program?
Any help would be greatly appreciated.
how can I assign n to double in the middle of program?

There must be lots of possibilities.
one would be,
1
2
3
4
5
	
    cout << "Please enter an integer value: ";
    int n;
    cin >> n;
    double d = n;

That is, use a different name (I chose d) for the other variable, and use that in the sqrt().

Note depending on which compiler you use, and which standard it conforms to, there are several variations of the sqrt() function. The integer versions seem to have been added with C++11.
Last edited on
Two methods:
1
2
3
4
5
6
7
8
9
int foo;

// 1. create another variable:
double bar = foo;
sqrt( bar );


// 2. explicit conversion:
sqrt( static_cast<double>(foo) );
std::sqrt has been able take integers as argument since C++11.
Last edited on
cout << "Please enter an integer value: ";
int n;
cin >> n;
double d = n;
This is working, I tried it at first but it wasn't working because I was doing that in this sequence:
cout << "Please enter an integer value: ";
int n;
double d = n;
cin >> n;
When I type for example 7.3 it's not working even if I do it with a first method but it make a sense.
Last edited on
keskiverto you first method is not working. I have to type a value and only after that I can assign it.
When I type for example 7.3
1
2
int n;
cin >> n;

The input operation takes characters from the stream and tries to construct an integer.
It can take the '7'. It cannot take '.', because no integer has a dot in it.

Since it already got at least one character before the '.' it can make an integer from the "7".

However, there are still ".3" in the input stream, waiting for next input operation.

If you would start by typing .42, then the first character in the stream would be the '.'
One cannot make a number from that. The cin >> n; would set the stream cin into state fail and the .42 would still lurk there. Then you would have to first cin.clear(); to reset the state before any read operation could succeed (and get rid of offending characters, if it is an int that you need to read).
Topic archived. No new replies allowed.