I need help with my 'while' statement assignment.

closed account (oi35oG1T)
Hi,

I'm taking an intro programming course, but I'm having difficulty with my current assignment.

Here's the assignment:


Using while statement, write C++ code to display all the bit values up to the number entered by a user through pow(base, n) function.

Output Example:
Enter a bit number : 8
1 bit: 2
2 bit: 4
3 bit: 8
4 bit: 16
5 bit: 32
6 bit: 64
7 bit: 128
8 bit: 256


Here's what I have so far. Not much, I know:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int bitNum, base, n;
    base = ;
    n = ;
    while (condition)
    {
        statements
    }
    cout << "Enter a bit number: " << bitNum << endl;
    return 0;
}


I sort of understand loops, but I'm not sure what while condition I should use and how to implement the pow() function. Could someone help point me in the right direction. I'm not looking for my homework to be done for me, but just to get an idea of what to do.

Thanks.
Last edited on
For the input/output see:

http://www.cplusplus.com/doc/tutorial/basic_io/

For loops see:

http://www.cplusplus.com/doc/tutorial/control/


The loop has to be after the input of the user since the input of the user is actually the condition for the loop.
closed account (oi35oG1T)
Thank you, I'll have to take a look at those later when I have time. :)
Consider: "while the number of bits that I am printing the value for is smaller than or equal to the entered number of bits"
Topic archived. No new replies allowed.