I have a C++ midterm due tomorrow that I can't figure out. Here is the assignment:
Code:
// Use the program BtoDsol.cpp located at:
BtoDSol.cpp
// which converts any byte to decimal
// enhance the driver program to test an additional function DtoB with a prototype of:
// string DtoB (int Decimal);
// DtoB will convert any decimal number <= 255 to binary
// Here is a possible partial code:
string DtoB (int Decimal)
{
string Bin ="00000000"; // declare a model binary number
// you will need to develop the rest of the code here
//
return Bin;
}




Now, the .cpp referred to, BtoDSol.cpp:


// This program will test the function BtoD
// int BtoD (string Binary); is the prototype
// will convert any byte (8bits) from binary to decimal
#include <iostream>
#include <string>
using namespace std;
void BaseGen(int Base[], int Size)
{
Base[0]=1; // the first element is always 1
int Index=1; // start with the second value
while(Index<Size) // must load all Base elements
{
Base[Index]=Base[Index-1]*2; // left = right *2
Index=Index+1; // Bump the Index
}
return ;
}
int BtoD(string Binary)
{
unsigned int Index=1; //loop control variable
Index=0; // Initialize to zero
int Dv=0; //Dv is the decimal value for the binary number in Binary
int Base[8]; // Base array to store binary base values
BaseGen(Base,8); // Load Base
while(Index<Binary.size()) // loop to check all bits
{
if (Binary[Index]=='1') // if the value is 1
Dv=Dv+Base[Binary.size()-1-Index]; // add the corresponding base value to Dv
Index=Index+1; // Bump the loop control variable
}
return Dv; // Return the decimal value
}

// The driver program follows

int main()

{
string Bin,Anychar;
int Decimal;
// Get the binary number
cout << " Please type any valid Binary Number of eight or less bits"<< endl;
cin>> Bin;
// Convert A to decimal
Decimal=BtoD( Bin);
cout <<" The decimal value of "<< Bin << " is = "<<Decimal<<endl;
cin >> Anychar;
return 0;
}
I have no idea what to do on this one. Any help greatly appreciated, being how my grade depends on it and all.

Last edited by shabbir; 18Oct2008 at 09:48.. Reason: Code block