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 4 8602
cplusplus <cp*******@linux.local> wrote in
news:pa****************************@linux.local: int number1,number2, product;
should be
int number1,number2, product, root = 0;
On Wed, 14 Jun 2006 12:57:24 -0700, cplusplus wrote: Hello, I have newbie question.
[snip]
here is the error I'm getting:
combine.cc: In function `int main()': combine.cc:16: error: `root' undeclared (first use this function)
It's quite simple, really. Compiler error messages are not always clear,
but this one is. If you become a programmer, you will be spending a lot of
time deciphering what the error message means in terms of your code, so
it's best to start right away! The problem here is that you have not
declared your root variable. In your first example program, you have
double intOne; // use double for real numbers
You have no such declaration for root in the combined program.
John's answer was close, but would give you an integral root, which would
not produce the desired answer (it would give 6 as the root of 6 * 7).
--
Greg Schmidt gr***@trawna.com
Trawna Publications http://www.trawna.com/
you don't include cmath in the third program and you don't declare the
variable root:
#include <cmath> //should go near the beginning of the prog
int root = sqrt( product ); //inserted int before root
cplusplus wrote: 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
In message <C7********************@adelphia.com>, Squid Seven
<te*********@squidseven.com> top-posted
[please don't do that] you don't include cmath in the third program and you don't declare the variable root:
#include <cmath> //should go near the beginning of the prog
int root = sqrt( product ); //inserted int before root
Since when did std::sqrt return an int?
cplusplus wrote: 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.
That's a really bad hint. There are better ways to convert integer to
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 );
double root = sqrt(double(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
--
Richard Herring This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: cplusplus |
last post by:
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...
|
by: Stavros Christoforou |
last post by:
Hello everyone,
I was wondering if someone could help me with an issue I have in C++. I
want to select random points within the volume of a sphere. I know how
to get random numbers using srand()...
|
by: brian.digipimp |
last post by:
Write a program that prompts the user to input a positive integer. It
should then output a message indicating whether the number is a prime
number. (Note: An even number is prime if it is 2. An odd...
|
by: k |
last post by:
I have aproblem when multiplying 2 float
638.9 * 382.8 should = 244570.92
results giving 244570.922
both numbers are float variables , tried using double to...
|
by: Mike Meyer |
last post by:
In the discussion of equality, the issue that decimal('3.0') == 3.0 is
False came up as a reason for changing the behavior of ==. The problem
with this is that the proposed change doesn't really...
|
by: priyam.trivedi |
last post by:
Hi!
Could anyone tell me how to find the square root of a number without
using the sqrt function. I did it by using Newton's Formula. How can it
be done by using the Binomial Theorem/Taylor...
|
by: vj |
last post by:
Hi!
I have a piece of code (shown below) involving complex numbers. The
code is not running and giving error ("Invalid floating point
operation" and "SQRT:Domain error"). I would be very...
|
by: johnmsimon |
last post by:
i need to develop a code that finds a prime right number between 2 and
100000. and print one line of text that indicates if the int. is right
prime. i am in beginning programing so complex is...
|
by: shihaoran |
last post by:
I really need help with one my program; it is about arraylist; I do not get it. Can someone please help me with it?
Here's the instruction:
1. Your instructor will provide you with a text...
|
by: krishnai888 |
last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship.
I am currently writing a code...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
| |