Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ Program issue

  1. Jan 28, 2008 #1
    I'm attempting to teach myself C++, and I came across this old C++ problem in my closet. It's a ripped page and I'm not sure what book it's from so it has no solution. It's easy, but I seem to have issues with while loops. If someone would explain the logic or show me an example as to how they would do it that would be much appreciated.

    The problem:
    -Construct a program that computes the square root of a number. Make compare the difference from your first input to your second input, and so on and so forth (for example, if the first number you input is 4, compare the square root of that to the square root of the next number you input). If the difference of the two numbers is greater than 50, terminate the program.

    I have something like this:

    Code (Text):
    #include<iostream.h>
    #include<iomanip.h>
    #include<math.h>

    double positivePrint (double num, double square);

    int main ()
    {
        double num, square;
       
        positivePrint(double num, double square);
    }

    double positivePrint (double num, double square)
    {
           num==1;
           while(num>0, num++);
           {
                        square=sqrt(num);
                        cout << square << endl;
           }
    }
           }
    }
    But that gives me an "expected primary expression before" error and I'm not really sure where to proceed from here anyhow. Any help is appreciated and thanks in advance.
     
  2. jcsd
  3. Jan 28, 2008 #2
    you have an extra

    Code (Text):
     }
    }
    at the end. == is a comparison, not an assignment. having a ; after the num++) ends the while loop there. you arent returning a value from positivePrint or main. Also look up passing by reference , cause i think your code is intending to do that.

    for Construct a program that computes the square root of a number. Make compare the difference from your first input to your second input, and so on and so forth (for example, if the first number you input is 4, compare the square root of that to the square root of the next number you input). If the difference of the two numbers is greater than 50, terminate the program.

    One implementation is:

    Code (Text):

    #include <iostream>
    #include <math.h>

    int main(void)
    {
    double num1=0;
    double num2=0;
    std::cout << "Enter num1: ";
    std::cin >> num1;
    std::cout << "Enter num2: ";
    std::cin >> num2;
    double sqrtnum1=sqrt(num1);
    double sqrtnum2=sqrt(num2);
    std::cout << "The difference of sqrt of both nums is " << fabs(sqrtnum1-sqrtnum2);
    if (fabs(sqrtnum1-sqrtnum2) >50)
    {
    return 1;
    }
    return 0;
    }
     
    or something like that. Terminates with 1 if the difference is greater than 50, terminates with 0 if it isnt.
     
    Last edited: Jan 28, 2008
  4. Jan 28, 2008 #3
    Thanks for the quick response.

    I didn't notice the extra braces and using == instead of = was just dumb, but that fixed my syntax error. I read through your code and understand what you're doing - I think I was trying to do to much by trying to call another function in main. However, I still don't really understand how to implement and use while loops.

    Not on my home PC now, but I plan on trying a variation of your code when I get home. Once again I thank you for your quick help.
     
  5. Jan 29, 2008 #4

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    You also appear to have "n== 1" where you surely intended "n= 1".
     
  6. Feb 1, 2008 #5
    DrKn - why you didn't intend code? Did you have too much experience with fortran 60?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C++ Program issue
  1. C program (Replies: 4)

  2. C program (Replies: 4)

  3. C Program (Replies: 3)

  4. C program (Replies: 0)

  5. C programming (Replies: 9)

Loading...