【发布时间】:2015-03-24 19:44:01
【问题描述】:
#include <iostream>
#include <cmath>
using namespace std;
int main (){
//Written By: Hannah Stang
//Reads a Binary number and converts it to a decimal
long decimal = 0, remainder, number, base = 1;
long bin;
cout << "Enter a binary number: ";
cin >> number;
bin = number;
while (number > 0)
{
remainder = number % 10;
decimal = decimal + remainder * base;
base = base * 2;
number = number / 10;
}
cout << "Conversion to decimal: " << decimal << endl;
return 0;
}
我遇到的主要问题是试图让程序运行不止一次。我需要它不断重复,直到我输入 -1 作为值。
【问题讨论】:
-
你把'while'放错了,你没有检查流状态
-
@DieterLücking 'while' 放置得很好——但是所有东西都需要另一个'while'。
标签: c++ loops binary type-conversion