> Programming Languages > C++
Various Topics Home

C++



C++ Programming Languages multiplying two numbers and giving the square root


Default Re: multiplying two numbers and giving the square root

On Wed, 14 Jun 2006 12:56:55 -0700 in comp.lang.c++, cplusplus
<> was alleged to have written:
> int number1,number2, product;


Here you declare some variables. 'root' is not among them.

> root = sqrt( product );


Here you attempt to assign a value to 'root', having never declared it.

>combine.cc: In function `int main()':
>combine.cc:16: error: `root' undeclared (first use this function)


Here the compiler es at you regarding the missing declaration.
Is there anything not perfectly clear about this message?

Default Re: multiplying two numbers and giving the square root


First set your system to a correct date/time !!!


Default multiplying two numbers and giving the square root

Hello, I have newbie question.
I'm stuck on this current assignment.

Write a program that prompts the user for two integer values,
passes the values to a function where they are multiplied
together and the square root of the product is returned and
displayed for the user. The function should return a double.
Hint: If you multiply an integer by 1.0 the result will be a
double. For example:

Enter an integer: 7
Enter another integer: 6
The square root of 7 times 6 = 6.48074

--
ok, so what I did was created a program that would get the
sqaure root of a number, and then created another one that multipled
2 numbers. Here is the source for the square root:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double intOne; // use double for real numbers

cout << "Enter an integer:\n";
cin >> intOne;
double side; // create another variable
side = sqrt( intOne ); // call function, assign return value
cout << "The square root is " << side;
cout << " \n";

return 0;
}

here is the source for multiplying the two integers:

#include <iostream>
using namespace std;


int main()
{
int number1,number2, product;
cout << "Enter a number: ";
cin >> number1;
cout << "Enter another number: ";
cin >> number2;
product = number1 * number2;
cout << "The product of " << number1 <<" and "<< number2 << " is " << product <<"\n";
return 0;
}

I tried to combine the two together using this code:

#include <iostream>
using namespace std;


int main()
{
int number1,number2, product;
cout << "Enter a number: ";
cin >> number1;
cout << "Enter another number: ";
cin >> number2;
product = number1 * number2;
cout << "The product of " << number1 <<" and "<< number2 << " is " << product <<"\n";
root = sqrt( product );
cout << "The square root is " << root <<"\n";

return 0;
}

here is the error I'm getting:

combine.cc: In function `int main()':
combine.cc:16: error: `root' undeclared (first use this function)
combine.cc:16: error: (Each undeclared identifier is reported only once for
each function it appears in.)
combine.cc:16: error: call of overloaded `sqrt(int&)' is ambiguous
/usr/include/bits/mathcalls.h:157: error: candidates are: double sqrt(double)
/usr/include/g++/cmath:550: error: long double std::sqrt(long
double)
/usr/include/g++/cmath:546: error: float std::sqrt(float)

I'm new to this language and any help would be greatly
appreciated. It would be great if someone could point
me in the right direction.
Thanks a bunch,

--Cameron

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0