0

I wanted to use only 1 and 0 for the binary. But instead the answer keep giving me the 2nd option with whatever number I typed. I had tried where did I programmed wrongly but unfortunately I still can't find it. So I hoped that I could get some help here.

#include<iostream>
#include<cmath>

using namespace std;

int DualzahlZuDezimal(long long n)
{
    int dez = 0;
    int i = 0, rem;
    while (n != 0)
    {
        rem = n % 10;
        n /= 10;
        dez += rem * pow(2, i);
        ++i;
    }
    return dez;
}

string a;

int main()
{
    long long n;
    int dez;

    cout << "Test Ein- und Ausgabe : \n";
    cout << "----------------------- \n";

    cout << "Eingabe einer Dualzahl : ";
    cin >> n;

    if ((n == '1') && (n == '0'))
    {
        cout << "Dual : " << n << endl;
        cout << "Dezimal : " << DualzahlZuDezimal(n) << endl;
        cout << "cin ok ? : ja-ok" << endl;
        return 0;
    }

    else
    {
        cout << "Dual : 0" << endl;
        cout << "Dezimal : 0" << endl;
        cout << "cin ok ? : nein-nicht ok" << endl;
        return 0;
    }
}
WhatIsLife
  • 39
  • 5

2 Answers2

1

If I understand this right, you want the user to enter a binary number, like 10001101001, and you will show the decimal equivalent (1129 in this case).

There are 2 general ways to do that yourself:

  1. You can read the value as a number, as you do, and then apply your conversion process, except that you check that rem is either 0 (in which case you do nothing), or 1 (in which case you add the power of 2). If it's another value, you report the error, and return 0.
  2. You can read the value as a std::string instead. Then you can use std::find_first_not_of() to check for contents other than 0 or 1:

    if (n.find_first_not_of("01") != string::npos) { /* complain */ }

    but then you need to do the conversion based on characters.

But the best approach is not to reinvent the wheel and instead let the standard library handle it for you via stol():

#include <cstddef>
#include <iostream>
#include <string>

using namespace std;

int
main()
{
  string text;
  cout << "Enter a binary number: " << flush;
  cin >> text;
  size_t endpos = 0;
  long decimal_number = stol(text, &endpos, 2); // base 2 == binary
  if (endpos != text.size()) {
    cerr << "'" << text << "' is not a valid binary number!" << endl;
    return 1;
  }
  else {
    cerr << "binary  number: " << text << endl;
    cerr << "decimal number: " << decimal_number << endl;
    return 0;
  }
}
Zastai
  • 1,115
  • 14
  • 23
  • You don't need that `flush` after the prompt; `std::cin` flushes `std::cout` automatically. Similarly, those `endl`s can be simply `'\n'` -- the output streams get flushed when the program terminates. – Pete Becker May 23 '20 at 15:36
  • True. But they are also generally not harmful, and it's better to get into the habit so that things still work when later you write code where the streams involved are not cin/cout/cerr. – Zastai May 23 '20 at 15:40
  • No, it's a bad habit, precisely because when you're not using the console those flushes will kill performance. File I/O is buffered internally, and tuned for the particular file system. Flushing like that will make everything sloooooow. – Pete Becker May 23 '20 at 15:46
0

Keep in mind that input from the console is text. If you need to check that the text matches a particular format (in this case, consists entirely of 1's and 0's), the simplest approach is to look at that text:

std::string input;
std::cin >> input;
bool input_is_valid = true;
for (int i = 0; input_is_valid && i < input.length(); ++i) {
    if (input[i] != '0' && input[i] != '1')
        input_is_valid = false;
}

then, if the input is valid, convert the text to a numeric value:

long long n = std::stoll(input);
Pete Becker
  • 74,985
  • 8
  • 76
  • 165