C ++ - 简单的二进制到ASCII二进制程序

时间:2016-04-29 16:02:42

标签: c++ binary console

好的,这个问题非常具体,我会简短。 我试图将5位数的简单二进制转换为十进制,然后转换为ASCII十进制数; 但不知何故,我的意见是:

  

输入二进制数:10110

     

完成了吗? (y / n):n

     

完成了吗? (y / n):n

它应该是在询问另一个二进制数后才询问完成,然后在输入y时在char数组中显示一个句子。发生什么事了?我真的不明白发生了什么......

//main.cpp
#include <iostream>
#include "binconverts.h"

int main(){
    using namespace std;
    int numBin;
    char acabou;
    bool breakLoop = false;
    int numer = 0;
    cout << "Digite um numero binario: ";
    cin >> numBin;
    for (int a = 0; a < 54; a++){
        if (breakLoop == true) break;
        binconv.text[a] = binconv.convBin(numBin);
        numer++;
        cout << "Acabou? (s/n): ";
        cin >> acabou;
        switch (acabou){
        case 'S':
            breakLoop == true;
            break;
        case 's':
            breakLoop == true;
            break;
        case 'n':
            breakLoop == false;
            break;
        case 'N':
            breakLoop == false;
            break;
        default:
            breakLoop == false;
            break;}
    }
    for (int b = 0; b < numer; b++){
        cout << binconv.text[b];
    }
}
//binconverts.h
#pragma once
#include "rick_math.h"

class BINCONV{
public:
    int convBin(int);
    int setText();
    char text[54];
}binconv;

BINCONV::convBin(int n){
    int decimal(0), i(0), rem;
    while (n != 0)
    {
        rem = n % 10;
        n /= 10;
        decimal += rem * maths.poten(2,i);
        ++i;
    }
    return decimal + 63;}

1 个答案:

答案 0 :(得分:0)

这应该会改善代码的格式和功能

int main(){
    using namespace std;
    int numBin;
    char acabou;
    bool breakLoop = false;
    int numer = 0;
    cout << "Digite um numero binario: ";
    cin >> numBin;
    cout << "Acabou? (s/n): ";
    for (int a = 0; a < 54; a++){
        if (breakLoop == true) break;
        binconv.text[a] = binconv.convBin(numBin);
        numer++;
        cin >> acabou;
        if (acabou="s" || acabou="S"){
            breakLoop = true;
        }else {
            breakLoop = false;
        }
        for (int b = 0; b < numer; b++){
            cout << binconv.text[b];
        }
    }
}