Hello, I'm a huge c++ beginner and i was given a task to write a program that will accept continuous inputting of binary, decimal, octal and hexadecimal numbers. The program will only terminate if the user inputs an invalid value and it would display the total number of binary, decimal, octal and hexadecimal entered.
Example:
Enter value: A
Enter value: 1
Enter value: 5
Enter value: 9
Enter value: H
That is an invalid input!
The total number of binary is 1
The total number of octal is 2
The total number of decimal is 3
The total number of hexadecimal is 4
I have tried many ways to figure out how to solve this problem and searched everywhere for additional information but nothing worked, And this is what i came up so far:
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main(){
int bin, dec, oct, ctr=0;
int num=0;
char hex;
while (ctr<5)
{cout << "Please enter value:\n " ;
cin >> num;
if (num==0||num==1){
for(int bin=0; bin<5; bin++)
num=bin;
}
else if (num>=0&&num<=1000){
for(int dec=0; dec<5; dec++)
num=dec;
}
else if (num>=0&&num<=7){
for(int oct=0; oct<5; oct++)
num=oct;
}
else if (num!='A'||num!='B'||num!='C'||num!='D'||num!='E'||num!='F'){
for(int hex=0; hex<5; hex++)
num=hex;
ctr++;
}
else{
cout << "That value is invalid\n " ;
cout << "\nThe number of binary is "<<bin<<"\n " ;
cout << "\nThe number of decimal is "<<dec<<"\n " ;
cout << "\nThe number of octal is "<<oct<<"\n " ;
cout << "\nThe number of hexadecimal is "<<hex<<"\n " ;
}}
system ("PAUSE");
return 0;
}