1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Calculating distance in c++

  1. Nov 23, 2012 #1
    1. The problem statement, all variables and given/known data
    can some one tell me what I am doing wrong in this program. I am getting a
    error C2296: '^' : illegal, left operand has type 'double'
    error at line 10 where I do my distance calculations. I am using Microsoft Visual Studios 2008.

    2. Relevant equations
    The program is supposed to input the time in seconds and then use the formula
    distance = (g * time2)/2 where g = 9.81

    3. The attempt at a solution

    Code (Text):

    #include <iostream>
    using namespace std;
    int main()
       double time, distance;

       cout << "Enter the time in seconds.\n";
       cin >> time;
       distance = (9.81*(time)^2)/2;
       cout << "Distance travelled under free fall is "<< distance <<" meters per second.";
    Last edited by a moderator: Nov 23, 2012
  2. jcsd
  3. Nov 23, 2012 #2


    Staff: Mentor

    You are using ^ as an exponentiation operator - there is no such exponentiation operator in any C-based language (including C++ and C#). The ^ operator is the bitwise "exclusive or" operator.

    Rewrite your assignment statement like so:
    Code (Text):

       distance = (9.81*time*time)/2;
  4. Nov 23, 2012 #3
    ^ is bitwise exclusive or
  5. Nov 23, 2012 #4
    Really? well thanks for the help
  6. Nov 23, 2012 #5
    So you know, if you need to do more complicated exponents, e.g. x^(2.3), you could include <cmath> and use the "pow()" function. Then instead of writing x^y, you would write pow(x,y). However, when you're just doing something like x^2, it's easier to just use x*x.
  7. Nov 23, 2012 #6
    I know about cmath I didn't add it to my program because I was under the impression I could just use ^ to represent exponent. Thanks for the help
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook