1 Attachment(s)
Functions with one parameter problem
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;
}
Re: Functions with one parameter problem
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....... :wave:
Re: Functions with one parameter problem
I have no clue, thats why I posted it on here
Re: Functions with one parameter problem
Does this problem use global variables?
Re: Functions with one parameter problem
please point me in the right direction
Re: Functions with one parameter problem
Quote:
Originally Posted by
MMac1218
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:
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:
You make the same mistake later on here:
This should be changed to:
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).
This should be:
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.
Regards,
Paul McKenzie
Re: Functions with one parameter problem
Quote:
Originally Posted by
MMac1218
please point me in the right direction
The answer to your question is this:
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?
Regards,
Paul McKenzie
Re: Functions with one parameter problem
Re: Functions with one parameter problem
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'
Re: Functions with one parameter problem
Code:
if (num == 0.0)
return;
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.
Re: Functions with one parameter problem
Sorry, still dont get it. Im not to good with this language, its way over my head. So you are saying I have 2 errors?
Re: Functions with one parameter problem
Still getting
error: '::main' must return 'int'
Re: Functions with one parameter problem
change it to return 0;
since by default main() declaration is like below
int main();
Re: Functions with one parameter problem
Still doesnt exit when 0 is entered.
It should look like this:
Enter a number (a double): 0
Press any key to continue . . .
--------------------------------------
Instead it just keeps asking the question:
Enter a number (a double): 0
The square root is 0
Enter a number (a double)
Everything else woks but when I enter '0', it doesnt.
Look at attachment in first post if needed.
Re: Functions with one parameter problem
Quote:
double mySquareRoot (double num=0)
{
return sqrt(num);
}
Your sending a variable, for it just to be declared as 0 again.
try
Code:
double mySquareRoot(double num)return sqrt(num);