i am using visual studio and writing a program to come up with a square root of a number that is prompted from the user. the error i receive is [error C2296: '^' : illegal, left operand has type 'double']. ive read my begining c++ book over and over and do not understand what is causing this error? here is my code. visual studio tells me i have an error in my while loop.

#include <iostream>

using namespace std;

int main()

{

const double e = 0.01;

double y, incro, guess;

cout << "Please enter a positive real number: " << endl;

cin >> y;

if(y<=0)

{

cout << "only positive numbers can be used" << endl;

}

else

{

guess = y/2.0; //start search for sqrt(y) at n/2

incro = guess/2.0; //start incro at 1/2 of guess

while (((guess*guess-y)^2)>e^2) //close enough??

{

if(((guess)^2) >y)

{

guess = guess-incro;//makes guess smaller as it is too big

}

else

{

guess = guess+incro;//makes guess bigger as it is too small

}

incro = incro/2.0;

}

cout << guess << endl;

}

return 0;

}

#include <iostream>

using namespace std;

int main()

{

const double e = 0.01;

double y, incro, guess;

cout << "Please enter a positive real number: " << endl;

cin >> y;

if(y<=0)

{

cout << "only positive numbers can be used" << endl;

}

else

{

guess = y/2.0; //start search for sqrt(y) at n/2

incro = guess/2.0; //start incro at 1/2 of guess

while (((guess*guess-y)^2)>e^2) //close enough??

{

if(((guess)^2) >y)

{

guess = guess-incro;//makes guess smaller as it is too big

}

else

{

guess = guess+incro;//makes guess bigger as it is too small

}

incro = incro/2.0;

}

cout << guess << endl;

}

return 0;

}

^ is not how you raise numbers to an exponent in C++, it is used for bitwise operations.

You will either want to use * in the correct way or use the pow function.

You will either want to use * in the correct way or use the pow function.

Last edited on

Topic archived. No new replies allowed.