I am supposed to ask the user for a low number, and a high number. Then, the numbers should print, and each number in the range should print its binary as well. (using a while loop) I am struggling to figure out how to structure the while loop to properly spit out the binary. I am new to CPP so any help would be appreciated!
If low and high are 1 and 10, I would want it to print:
1 1
2 10
3 11
4 100
... and so on.
// Unit 5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
int lowNum;
cout << "Please enter your low number (only use numbers 1 - 256): ";
cin >> lowNum;
while (lowNum < 1 || lowNum > 256)
{
cout << "Please only enter numbers between 1 and 256: ";
cin >> lowNum;
while (cin.fail())
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
int highNum;
cout << "Please enter your high number (only use numbers 1 - 256): ";
cin >> highNum;
while (highNum <= lowNum)
{
{
cout << "Please ensure that the high number you enter is bigger than your low number: ";
cin >> highNum;
}
while (highNum < 1 || highNum > 256)
{
cout << "Please only enter numbers between 1 and 256: ";
cin >> highNum;
if (cin.fail())
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
int saveLow = lowNum;
int current = 0;
while (current < highNum)
{
current = lowNum;
cout << current << endl;
lowNum += 1;
}
int bin;
int count = saveLow;
while (lowNum > 0)
{
bin = lowNum % 2;
cout << bin;
lowNum /= 2;
count += 1;
}
return 0;
}