Home > front end >  How could I check If a variable is a certain number multiple times while changing what to do if It m
How could I check If a variable is a certain number multiple times while changing what to do if It m

Time:09-21

I want to check what the variable "num" is from 0 - 15 How can I check if it is one of those numbers and what number it is so far I've got.

#include <iostream>
using namespace std;
int num = 0;
int main()
{
 
cout << "Number to check:";
cin >> num;


{
    if num = 0;
    cout << "0000";
    {
        if num = 2;
        cout << "0010";
        {
            if num = 1;
            cout << "0001";
            {
                if num = 3;
                cout << "0011";
                {
                    if num = 4;
                    cout << "0100";
                    {
                        if num = 7;
                        cout << "0111";
                        {
                            if num = 5;
                            cout << "0101";
                            {
                                if num = 6;
                                cout << "0110";
                                {
                                    if num = 8;
                                    cout << "1000";
                                    {
                                        if num = 9;
                                        cout << "1001";
                                        {
                                            if num = 10;
                                            cout << "1010";
                                            {
                                                if num = 11;
                                                cout << "1011";
                                                {
                                                    if num = 12;
                                                    cout << "1100";
                                                    {
                                                        if num = 13;
                                                        cout << "1101";
                                                        {
                                                            if num = 14;
                                                            cout << "1110";
                                                            {
                                                                if num = 15;
                                                                cout << "1111";
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
}

return 0;

How may I be able to do this If I can't, how can I convert a number to a 4-bit Binary byte? And if possible how can I make sure the number inputted is in the range of 0 - 15 and that there isn't letters in the inputted string

CodePudding user response:

The program below does what you want.

    #include<iostream>
    #include <bitset>
    int main() {
    
    
        int num = 0;
        std::cout << "Number to check: ";
        std::cin >> num;
        while (std::cin.fail() || num > 15 || num < 0) {
            std::cout << "Error! Invalid Input \n";
            std::cin.clear();
            std::cin.ignore(256, '\n');
            std::cout << "Number to check: ";
            std::cin >> num;
        }
        std::bitset<4> value;
            value = { static_cast<unsigned long>(num) };
            std::cout << value.to_string() << "\n";
    
        return 0;
    }

CodePudding user response:

Use <bitset>

#include <bitset>
#include <iostream>
#include <string>

int main()
{
    std::string input{ "12" };
    // std::cin >> input;

    unsigned long number = std::stoul(input); // does number checking

    if (number < 16) // do value checking
    {
        std::bitset<4> value{ number };
        std::cout << value.to_string() << std::endl;
    }
}

CodePudding user response:

Since you declared num as an integer, your program won't work if you input nondigit characters during cin. I believe the shortest variant of your code without dealing with std::bitset would be

const int MAX_BITS = 4;
cout << "Number to check:";
cin >> num;
if (num < 0 || num > (1 << MAX_BITS) - 1) {
    cout << "Invalid number, next time enter between 0 and " << (1 << MAX_BITS) - 1;
    return 0;
}

for(int bit = MAX_BITS - 1; bit >= 0; --bit){
    cout << ((num & (1 << bit)) > 0);
}
cout << endl;
  • Related