visual c++ - C++ Function is not returning a value -
here code, can not figure out why won't work function when exact code in main() produces correct answer. assignment convert binary number decimal.
#include <iostream> #include <cstdlib> #include "std_lib_facilities.h" using namespace std; int binarycon(int binum); int main() { int num, bin, bnum; cout << "enter binary number(1s , 0s) : "; cin >> num; bin = num; bnum = binarycon(num); cout << "the decimal equivalent of " << bin << " : " << bnum << endl; } int binarycon(int binum) { long dec = 0, rem = 0, base = 1; enter code here`while (binum > 0) rem = binum % 10; dec = dec + rem * base; base = base * 2; binum = binum / 10; return binum; }
corrected code:
#include <iostream> #include <cstdlib> using namespace std; int binarycon(int binum); int main() { int num, bin, bnum; cout << "enter binary number(1s , 0s) : "; cin >> num; bin = num; bnum = binarycon(num); cout << "the decimal equivalent of " << bin << " : " << bnum << endl; getchar(); return 0; } int binarycon(int binum) { long dec = 0, rem = 0, base = 1; while (binum > 0){ rem = binum % 10; dec = dec + rem * base; base = base * 2; binum = binum / 10; } return dec; }
Comments
Post a Comment