Return 2

It has been solved
Last edited on
use
cout << a << endl;

and read here http://www.cplusplus.com/forum/beginner/1988/
return is a very specific word that I don't think you meant to use here. If it is... you didn't use the return keyword so it won't return 2.

Otherwise, you aren't decrementing a by 0.5 every iteration of the while loop, you are just calculating a/2 and not doing anything with it.

I think what you meant to do is:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std ;

int main(){
  float a=8.0;

  while(a>2) {
    a/=2; // a = a/2;
  }
  cout<<a;
}


Also keep in mind that when declaring a variable as double or float, you introduce floating point errors. Using int a, will keep this minimized and you'll certainly output 2 each time.
Topic archived. No new replies allowed.