Hey guys, I think I am very close on this problem but I dont know how to end my program after the zero is entered because it just continues to promt the question.
Also we are not allowed to use global variables.. Is this a global variable?
Code:
/* Write a program with a function called mySquareRoot.
In main, prompt the user for a number (a double), then
by calling the mySquareRoot function calculate and
display the square root of the number. Use the standard
cmath function to calculate the square root. Use the
follow screen shot as a guide.
*/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
double mySquareRoot (double num=0)
{
return sqrt(num);
}
void main ()
{
double NewNum;
double num=0;
while(num != '0')
{cout << "Enter a number (a double): "; cin >> num;
NewNum = mySquareRoot(num);
cout << "The square root is " << NewNum << endl;
}
if (num =='0')
return;
}
Step through your program with the debugger. Pay special attention to the order of operations. THINK about WHERE you really need to make the decision to exit....
You are very very close.......
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010 In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Hey guys, I think I am very close on this problem but I dont know how to end my program after the zero is entered because it just continues to promt the question.
You have a big mistake in your program:
Code:
while(num != '0')
This compares num to 48. Why 48? The '0' is the character 0, not the number 0. A character '0' is equal to the ASCII value of 48.
Since num is declared as a double, you must compare doubles to doubles. This should be changed:
Code:
while (num != 0.0)
You make the same mistake later on here:
Code:
if (num =='0')
This should be changed to:
Code:
if (num == 0.0 )
However you should never really compare doubles for equality, since doubles are inexact representations of floating point numbers. However, I believe you can get away with it here (but don't make it a habit, as it really isn't correct to compare doubles for equality).
Code:
void main()
This should be:
Code:
int main()
If your teacher is giving you "void main()", that doesn't bode too well as to the teacher's knowledge of C++. Any code that has "void main()" will disqualify you for most jobs in the C++ programming industry. The main() function must always return an int.
Reformat your while loop. You will see your mistake more clearly:
Code:
while(num != 0.0)
{
cout << "Enter a number (a double): "; cin >> num;
NewNum = mySquareRoot(num);
cout << "The square root is " << NewNum << endl;
} // end of the while loop
if (num == 0.0)
return;
Look at the opening and closing braces of the while loop. What code is between those braces? What code isn't in there?
I am still stuck on this. I thought I had it but I was wrong.
Code:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
double mySquareRoot (double num = 0)
{
return sqrt(num);
}
int main ()
{
double NewNum;
double num = 0;
while(num != 0.0)
{cout << "Enter a number (a double): "; cin >> num;
NewNum = mySquareRoot(num);
cout << "The square root is " << NewNum << endl;
}
if (num == 0.0)
return;
}
I keep getting the error:
c:\users\owner\documents\visual studio 2008\projects\assignment25\assignment25\assignment25.cpp(40) : error C2561: 'main' : function must return a value
1> c:\users\owner\documents\visual studio 2008\projects\assignment25\assignment25\assignment25.cpp(25) : see declaration of 'main'
This is the *last* statement in your main() function. The very last one. Think about it. If num == 0.0, return....otherwise.....return anyway, since it's the only thing left to do. Doesn't make much sense, does it? And when things don't make much sense, it's usually an indication there's a logic problem in your code.
The precise error is occurring because main returns an int and you aren't specifying one in the return statement (typically 0 for success, something else for failure). However, that's secondary to the above issue.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.