I need help.

Write a program called powersOf2.cpp and save it in your programs directory.

This program should ask the user to input an integer value. It will then raise 2 to all the powers from 0 up to and including the user entered value.

For example, if the user entered 5 the output would be:

2 ^ 0 = 1
2 ^ 1 = 2
2 ^ 2 = 4
2 ^ 3 = 8
2 ^ 4 = 16
2 ^ 5 = 32

For this assignment, you must use a for loop to accomplish this.

Remember to once again:
•Explain the program and its use to the user.
•Check for input that will cause mathematical problems (in this case, to satisfy the conditions of the problem, the entered value must be greater than 0).
•Allow the user to run the program again.

This is the code I have so far:

#include <iostream> 

using namespace std;

int main()
   int x;
   int y;
   cout << "This program asks for a number then raises 2 to every" << endl;
   cout << "number up until the number you put in. The number must" << endl;
   cout << "be a whole number from 1 to 20." << endl;
   cout << "\n" << endl;
   cout << "Please enter your number here: " << endl;
   cin >> y;   
   for (x = 0; x <= 20 ; x^y)
      cout << x;
      cout << " ";
   cout << endl;
   return 0;

When I try to run the program it tells me that the third statment in line 22 has no effect (that would be the "x^y" in the for statement). How can I get it to do what the directions say?

Edited by Nick Evan: added code-tags

6 Years
Discussion Span
Last Post by Fbody

Hint: x ^ y takes the exclusive or (XOR) between the bits of the two integers it is not the proper operation. The pow() function of <cmath> is used for that but is not necessary here.

You are correct in needing a for loop. Try and figure out the value of 2 you need at each step (e.g., at 0 you need 2 raised to 0 which is 1, at 1 you need 2 raised to 1 which is 2, etc.) and find the pattern. Trace it out by hand if it helps you. Take another crack at it and post back.

Please use code tags when posting code [code] //code here [/code]

Edited by jonsca: n/a

Votes + Comments
well handled :)

Well I don't know what you imagine "x^y" to be supposed to do, but I can tell you what it does and why the compiler warns you about it. The ^ operator in C++ is a bitwise XOR operator, so x^y produces the results of x XOR y (XOR is btw the exclusive OR operator, remember those lame binary number exercises from computer science 101..). So the compiler complains because you evaluate an expression and don't store it anywhere, and thus, it "has no effect".

Google for the "bit shift operators in C++", it's the key to solving this problem.

EDIT: beat me at it jonsca!

Edited by mike_2000_17: n/a


That would work, but I suspect the OP is not familiar with those yet. If you're not exactly familiar with how a binary number system works, addressing the 2^0 condition would be rather confusing.

Of course, this is completely dependent on what the OP has already learned, and the lesson that this assignment is linked to. Considering the operator SNAFU, I suspect that they are extremely new and aren't familiar with bitwise operations at this point.

Edited by Fbody: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.