Calculating Distance in C++: Troubleshooting Exponent Error

  • Context: Comp Sci 
  • Thread starter Thread starter wahaj
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a C++ program that calculates distance using a formula involving time and gravitational acceleration. Participants focus on identifying and correcting an error related to the use of the exponentiation operator.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant reports an error message related to the use of the '^' operator for exponentiation in their C++ code.
  • Another participant clarifies that the '^' operator is actually a bitwise exclusive or operator in C++, not an exponentiation operator.
  • A suggestion is made to rewrite the distance calculation using multiplication instead of exponentiation: distance = (9.81*time*time)/2.
  • Further, a participant mentions that for more complex exponentiation, the library and the "pow()" function can be used.
  • One participant acknowledges their misunderstanding about the use of the '^' operator and thanks others for their help.

Areas of Agreement / Disagreement

Participants generally agree on the misuse of the '^' operator and the correct approach to calculating distance, but there is no consensus on the necessity of using the library for simple exponentiation.

Contextual Notes

Participants discuss the limitations of using certain operators in C++ and the implications of not including necessary libraries for mathematical functions.

wahaj
Messages
154
Reaction score
2

Homework Statement


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.




Homework Equations


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

The Attempt at a Solution



Code:
#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 traveled under free fall is "<< distance <<" meters per second.";
}
 
Last edited by a moderator:
Physics news on Phys.org
wahaj said:

Homework Statement


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.




Homework Equations


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

The Attempt at a Solution



Code:
#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 traveled under free fall is "<< distance <<" meters per second.";
}

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:
   distance = (9.81*time*time)/2;
 
^ is bitwise exclusive or
 
Really? well thanks for the help
 
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.
 
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
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K