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.

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++)
    else if (num>=0&&num<=1000){
    for(int dec=0; dec<5; dec++)
    else if (num>=0&&num<=7){
    for(int oct=0; oct<5; oct++)
    else if (num!='A'||num!='B'||num!='C'||num!='D'||num!='E'||num!='F'){
    for(int hex=0; hex<5; hex++)

    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;
6 Years
Discussion Span
Last Post by Duoas

Is your program to assume that input values take the most restricted radix possible?
(That is, given an input of "24" is it to assume octal?)

Keep in mind that any string containing only the characters '0' and '1' is binary; any string containing only the characters '0'..'7' is octal; etc.

This is a string-parsing homework, not a number-parsing homework, so your inputs should be using getline().

int nbin = 0;
int noct = 0;
int ndec = 0;
int nhex = 0;
string s;

while (true)
  getline( cin s );

  // identify the type of number in 's'
  if (s is hexadecimal) nhex++;
  else if (s is decimal) ndec++;
  else if (s is octal) noct++;
  else if (s is binary) nbin++;
  // if it is not a valid number we are done
  else break;

You might want to make yourself a useful function to check whether or not a number is hexadecimal, decimal, octal, etc.

Also, remember that order matters... unless there is some known way to distinguish radices (such as the way C++ asks for "0x92" to mean hex instead of decimal).

Good luck!


Oh yes, I forgot about that, input values should take the most restricted radix possible. Well thank you very much, it really helped me a lot, now i just have to come up with effective functions for number validation :) Thanks! regards.

Edited by lupacarjie: n/a


Remember, you are dealing with strings, not numbers.

The textual representation of a number is what you are interested in -- that is, what characters are valid in the string version of a number?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.