michael_hartman_cz

PedF UK - OKB1319205 Ukol4

May 21st, 2014
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  /**
  4.   * Program reads zeros and ones from stdin and after terminating char 2 print decimal value of binary number.
  5.   *
  6.   * @return Returns 1 if wrong input is detected, 0 if everything ok.
  7.   * @author Michael Hartman [email protected]
  8.   */
  9.  
  10. int main ( int argc, char ** argv )
  11.  {
  12.     short i;
  13.     unsigned int num = 0;
  14.     while ( 1 )
  15.      {
  16.         if ( ! ( std::cin >> i ) || ( i != 0 && i != 1 && i != 2 ) )
  17.          {
  18.             std::cerr << "Wrong input!" << std::endl;
  19.             return 1;
  20.          }
  21.        
  22.         if ( i == 2 )
  23.             break;
  24.        
  25.         num <<= 1;
  26.         num |= (unsigned int) i;            
  27.      }
  28.     std::cout << "vysledna hodnota je " << num << std::endl;
  29.     return 0;
  30.  }
Advertisement
Add Comment
Please, Sign In to add comment