CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2008
    Posts
    52

    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;  
    }
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,117

    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.......
    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

  3. #3
    Join Date
    Oct 2008
    Posts
    52

    Re: Functions with one parameter problem

    I have no clue, thats why I posted it on here

  4. #4
    Join Date
    Oct 2008
    Posts
    52

    Re: Functions with one parameter problem

    Does this problem use global variables?

  5. #5
    Join Date
    Oct 2008
    Posts
    52

    Re: Functions with one parameter problem

    please point me in the right direction

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Functions with one parameter problem

    Quote Originally Posted by MMac1218 View Post
    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.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Functions with one parameter problem

    Quote Originally Posted by MMac1218 View Post
    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

  8. #8
    Join Date
    Oct 2008
    Posts
    52

    Re: Functions with one parameter problem

    thank you so much paul

  9. #9
    Join Date
    Oct 2008
    Posts
    52

    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'

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  11. #11
    Join Date
    Oct 2008
    Posts
    52

    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?

  12. #12
    Join Date
    Oct 2008
    Posts
    52

    Re: Functions with one parameter problem

    Still getting

    error: '::main' must return 'int'

  13. #13
    Join Date
    Aug 2008
    Posts
    18

    Re: Functions with one parameter problem

    change it to return 0;
    since by default main() declaration is like below
    int main();

  14. #14
    Join Date
    Oct 2008
    Posts
    52

    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.

  15. #15
    Join Date
    Nov 2008
    Location
    Maryland, USA
    Posts
    22

    Re: Functions with one parameter problem

    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);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured