Reputation: 700
The user enters the base from 2- 10. The user then enters a number they would like to convert to in base ten. But first the program checks if the number is in the base they entered.
CODE:
//ERROR CHECKING FOR DIGITS/ GETTING PROPER DIGITS INPUT
int digit = 0;
bool checker;
cout << "enter number for conversion: "; //Prompt user for base data
cin >> num;
if (num < base){
checker = true;
}
else{
checker = false;
}
//insert each number of digitN inside the array & check if each value is part of the base
while (checker == false){
int len = (std::to_string(num)).size(); //attain array size
for (int i = 0; i <= (len - 1); i++)
{
digit = num % 10;
if (digit >= base)
{
checker = false;
i = len;
}
else
{
checker = true;
}
num = (num / 10);
}
cout << "enter PROPER number for conversion: "; //Prompt user for base data
cin >> num;
}
I appear to be getting an error in the for loop. Can someone help me with the logic. Once the program accepts a number, it checks every digit of that number to see if it is in the base. If so, checker will be true otherwise false.
Upvotes: 1
Views: 53
Reputation: 311088
It is simple to make the checker.
bool is_valid;
unsigned int digit;
unsigned int x = ( num >= 0 ? num : -num );
do
{
digit = x % 10;
} while ( ( is_valid = digit < base ) && ( x /= 10 ) );
Instead of the ternary operator you could use standard function abs
Instead of variable name checker I use name is_valid. It is not importnat what name you will use.
Upvotes: 1