operations and operators

Hi cplusplus,

I am a complete beginner with no coding experience and using bjane (the creators) book to try and learn this langue. So please forgive me if this is a stupid question.
The program that i'm working on is (pg 67):

int main()
{
cout <<"please enter a floating-point value:";
int n;
cin >> n;
cout <<"n ==" <<n
<<"/nn+1 ==" << n+1
<<"\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';
}

The problem is that i'm inputting an integer and wanting it to be treated as a double (real world number) to get the correct square root. I have tried different possibilities (that i thought wouldn't work and they didn't...) using the [if(first == second]type commands and am now completely unsure what to do!! My thoughts were that i could somehow say that it could be treated as an int for all of the equations apart from the square root problem but am unsure how to do this.

any help would be much appreciated!
p.s. after thought: he gives this equation (if it is of any use): a/b*b+a%b==a.
an explanation of why that is useful or relevant would allow be a huge amount of help and even more gratitude will be sent your way!!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cmath>
using namespace std ;

int main()
{
    cout <<"please enter a floating-point value: ";
    // int n;
    double n ; // floating point value
    cin >> n;

    cout << "n == " << n
         << "\nn+1 == " <<  n+1
         << "\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';
}
change your "int n" to "double n"

int = integer
double = decimal

the way I understand it in class.
Thank you! Problem solved.
I'm not exactly sure what you are asking but I hope this helps. In your code, you ask the user for a floating point number yet the input is stored in n which is an integer. The simple solution would be to instead of use int n; use float n; or double n;. If you do want the user's entry to be stored as an int and you just want sqrt to return a floating point number then you need to cast n as a float (look below). Though using n\ can work when learning its easier to just use endl;

If you just want to use a double instead of an int:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream> 
using namespace std;

int main()
{
	cout << "please enter a floating-point value:";
	double n;
	cin >> n;
	cout << "n = " << n << endl;
	cout << "n+1 = " << n + 1 << endl;
	cout << "three times n = " << 3 * n << endl;
	cout << "twice n = " << n + n << endl;
	cout << "n squared = " << n*n << endl;
	cout << "half of n = " << n / 2 << endl;
	cout << "square root of n = " << sqrt(n) << endl;
}


Cast n as a float for square root:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream> 
using namespace std;

int main()
{
	cout << "please enter a floating-point value:";
	int n;
	cin >> n;
	cout << "n = " << n << endl;
	cout << "n+1 = " << n + 1 << endl;
	cout << "three times n = " << 3 * n << endl;
	cout << "twice n = " << n + n << endl;
	cout << "n squared = " << n*n << endl;
	cout << "half of n = " << n / 2 << endl;
	cout << "square root of n = " << sqrt(float(n)) << endl;
}


Last edited on
Topic archived. No new replies allowed.