PDA

View Full Version : C++ Program issue


Lykin
Jan28-08, 08:47 PM
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:

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

DrKn
Jan28-08, 10:20 PM
you have an extra

}
} 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:


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

Lykin
Jan28-08, 10:57 PM
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.

HallsofIvy
Jan29-08, 06:04 AM
You also appear to have "n== 1" where you surely intended "n= 1".

mishagam
Feb1-08, 12:19 AM
DrKn - why you didn't intend code? Did you have too much experience with fortran 60?