I just started c++ and programming and need some major help. I am trying to convert binary to decimal. I have searched through everything and its all to hard for me to understand,and uses terms ive never seen.

I need to convert the binary to dec, using % and / and only unsigned numbers.

Does anybody have suggestions. I need help to type in "1000101" seperate it into digits and then assign that digit its decimal number. It needs to be simple again I amvery new and the stuff on here seems to advanced for me.

OK, so which step of
- read in "1000101"
- seperate it into digits
- and then assign that digit its decimal number
is confusing you?

I read in the binary form "011010"
I having trouble getting it into digits, and assigning that digit the value in decimal form.

I can only use unsign, % and / ...

I am getting down how to divide into digits using %10, but i cant assign it the decimal form.

this is as far as i have as to seperating it into digits, I dont know where to begin to take that digit, then assign it the decimal value

#include <iostream>

using namespace std;
int main()
{
    int num;
    cout<<"Enter a Number : ";
    cin>>num;

    for(; num ; num/=10)
        cout<<num%10<<endl;
    


system("pause");
}

I read in the binary form "011010"

As a string or a number?

I having trouble getting it into digits,

This depends on the answer above.

and assigning that digit the value in decimal form.

Explain what decimal form means -- with an example.

It would really help if you posted your code so we know what you're doing. Most of the terms you use have multiple meanings and/or are ambiguous and/or used incorrectly so your meaning is vague. Read this.

No, not as a string number,

I need to enter a binary "111" and have the output be equal 7...

What I mean by decimal form is i need to convert the binary number to the decimal number that us ut equivilant too. The only code I really have is what I posted above. Where I seperated the number into digits. I dont know how to take those binary digits and give them the decimal.

I dont know if this will help or not. It is homework and ive spent 2 days on it getting absolutely no where. the teacher never even explained how to use modulus, and I am really really stuck trying to figure it out.

Write a program that reads in an unsigned integer containing only 0s and 1s (i.e. a "binary" integer) and outputs its decimal equivalent. Use the modulus and division operators to pick off the "binary" number's digits one at a time from rigiht to left.Your program must accept only (unsigned) "binary"integers containing only 0s and 1s. Do not read in the integer as a string object. Use a loop to validate the input: the user should get stuck entering a "binary" integer if the value entered is not of the right form.

You can't read 01001001 as an integer using say cin >> myInt; It will seem to work, but it doesn't give you an answer you can use.

Read it as a string.

#include <iostream>
#include <string>
using namespace std;

int main ( ) {
    string line;
    getline(cin,line);
    cout << line << endl;
    for ( int i = 0 ; i < line.length(); i++ ) {
        cout << line[i] << endl;
    }
    return 0;
}

$ ./a.exe
[B]100111001
[/B]100111001
1
0
0
1
1
1
0
0
1
#include <iostream>
using namespace std ;

// is every decimal digit of number a 0 or 1
bool is_every_digit_zero_or_one( unsigned int number )
{
  while( number > 0U )
  {
    if( number%10U > 1U )return false ;
    number /= 10U ;
  }
  return true ;
}

// read in an unsigned int containing only decimal digits 0 and 1
// user should get stuck entering a "binary" integer if the value
// entered is not of the right form
unsigned int read_in_number()
{
  unsigned int number ;
  // cin >> skipws >> dec ; // being safe, ignore this for now
  do
  {
    cout << "number? " ;
    // also ignore cin.clear/cin.ignore; by spec. user should get 'stuck'!
    cin >> number ;
  }while( !is_every_digit_zero_or_one( number ) ) ;
  
  return number ;
}

int main()
{
  unsigned int n = read_in_number() ;
  cout << "number (binary digits; leading zeroes omitted): " << n << '\n' ;
  // 'convert' number to decimal, and print out the value
}

some questions concerning the code you provided::

// cin >> skipws >> dec ; // being safe, ignore this for now
//***you add this, just for the case that the default input is other than decimal? is this possible?

// also ignore cin.clear/cin.ignore; by spec. user should get 'stuck'!
//***if i add cin.clear() i don't get any different behaviour...what is the difference?

> ..default input is other than decimal? is this possible?
yes, look up ios_base::fmtflags

> if i add cin.clear() i don't get any different behaviour...what is the difference?
clear() merely sets sets the state to a good state. if the stream is already in a good state, there would be no difference. if the stream is not in a good state, it would make a difference

#include <iostream>
#include <string>
#include <sstream>
#include <cassert>
using namespace std ;

int main()
{
  string input = "0101 \n 0101 \n 0101 \n *&^%$ \n 0101 \n" ;
  istringstream stm(input) ;
  streambuf* buff = cin.rdbuf( stm.rdbuf() ) ;
  
  int i ;
  cin >> i ;
  cout << "default input (decimal): " << i << '\n' ;
  cin >> oct >> i ; cout << "octal input: " << i << '\n' ;
  cin >> hex >> i ; cout << "hex input: " << i << '\n' ;
  cin >> dec >> i ; // fails, cin is now not in a good state
  assert( !cin ) ; // next char in buffer is '*'
  cin.clear() ; // reset the state of cin to good
  cout << "next char in input is: '" << char( cin.peek() ) << "'\n" ;
  cin.ignore(100,'\n') ; // remove chars (max 100) upto next newline
  cout << "next char in input is: '" << char( cin.peek() ) << "'\n" ;
  assert( cin >> dec >> i ) ; // ok
  cout << "decimal input: " << i << '\n' ;
  cin.rdbuf( buff ) ; // restore cin's original streambuf
}
/**
>g++ -Wall -std=c++98 input.cc && ./a.out
default input (decimal): 101
octal input: 65
hex input: 257
next char in input is: '*'
next char in input is: ' '
decimal input: 101
*/

OK, based on what you told us, you define an integer (say binary) and you enter only 1's and 0's into the variable. Your binary value is really read into a decimal variable, but contains only 1's and 0's.

Zero out another variable (say decimal) to hold the decimal value.

Next, using the % operator, get the each digit (a 1 or 0) and multiply each by the proper power of 2 and add it to the decimal variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.