Code Doesn't work

Problem was basically to output 2^n for any inputted integer n. I cannot see why my code doesn't work. It keeps outputting 2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include <iostream>
using namespace std;
int main () {
int n;
cin>>n;
int counter = 0;
int x = 2;
do {
	int x = x*2;  
	counter ++;
}
while (counter<n);
cout<<x<<endl;
}
Hello fat person, you are doing what's known as shadowing a variable.
You are redefining your x variable within the do-while loop, instead of modifying the existing x variable.

Your logic is a bit off as well,
If you type in 1, in order to calculate 21, you start at 2 but then always multiply by 2 at least once, so your lowest answer possible is 4, or 22.

initialize x to start at 1 instead of 2, and change line 9 to just x = x*2;
Topic archived. No new replies allowed.