I have a C++ midterm due tomorrow that I can't figure out. Here is the assignment:

// 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.

You need to make an effort to write something .. you cannot honestly expect someone to solve your midterm exam for you.

If it's the instructions you're having trouble with then allow me to assist you:

You have to develop a decimal to binary function.

The decimal number that will go into your function is the one which will be produced from using the given "binary to decimal" function.

You have to add a second test to function main (the driver) to show that your function returns the correct binary number.

So is he basically asking for two prompts? First ask the user to input a binary and convert it to decimal, then once that has been completed, ask the user to input a decimal and convert it to binary?

It is indeed the instructions that I'm having trouble with more than the code. I can't start the code if I'm not clear on what he wants.

Sigh !!

All you need to do is write a function using the prototype string DtoB (int Decimal) that takes a decimal number <= 255 as the input, converts it to its binary form, stores the result in a string and sends it back.

Just write that first and stop worrying about how many prompts are required in your main function.

Man, this is hard. I hella don't know how to do that.

Here is an article that elaborates on how you can convert a decimal number to a binary.

Link

I understand how to do that, that's one of the first assignments we did. But when he used that partial code, it looks completely different than the code I used to write that program. It's like a different style or something...

Read the binary to decimal routine he gave you thoroughly...

You can solve this problem in 7 or less lines of code.

So what are you waiting for? Seven lines?

Seriously, can I get some help here?

And by help I mean please just post the code. This class has nothing to do with my major at all, it's completely useless to me. After this I'll never touch Visual Studio again.

So what are you waiting for?

Here's what I have come up with for the code on converting a decimal number to binary using a string:

// 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;


string dec2bin(int Decimal);


string dec2bin(int Decimal)
{
int steps[] = {128, 64, 32, 16, 8, 4, 2, 1};
string binary;

if(Decimal > 0)
{
int stepLen = sizeof(steps) / sizeof(int);
for(int x = 0; x < stepLen; x++)
{
if(Decimal >= steps[x])
{
Decimal -= steps[x];
binary += "1";
}
else
binary += "0";
}
}
else
binary = "0";

return binary;

}



// The driver program follows


int main()
{
cout << "Enter an integer no larger than 255: ";
int Decimal;
cin >> Decimal;
if (Decimal > 255)
{
cout << "Please follow instructions" << endl;
}
else
{
cout << "Binary: " << dec2bin(Decimal) << endl;
}
system("PAUSE");
return 0;
}

After this, how would I go about making the program capable of distinguishing between a binary or decimal input and then converting it?

You don't need to make your program distinguish between a decimal and a binary input. All you need to do is prompt a user for a decimal number and then send it to your routine.

A couple of things, You should probably throw a message that says "Invalid input" if the user enters a number greater than 255. You can then either exit or ask the user to enter a valid number.
Your function prototype does not match what your teacher requested. You have string dec2bin(int Decimal) , whereas you were asked to use string DtoB (int Decimal) , so fix that.

And finally what you need to do is to add this function to the existing code your teacher gave you, compile it, and probably what you may want to test with, is have the user enter a decimal number, convert it to binary using the function you wrote, and then call your teacher's existing function BtoD with that binary string, and see if you get back your original decimal number !!

Well that's that. He never specifies he wants any type of error detection, so other than decimal numbers greater than 255, it will not be included. This is the final code for both D2B and B2D. Any suggestions are welcome but this seems to work just fine.

/*

Craig Wilhelm
18.October.2008
Midterm Assignment
CISP301, TTH, 19:00

18.October.2008
---------------
Initial Scripts

*/

#include <iostream>
#include <string>
using namespace std;

/* CONVERT DECIMAL TO BINARY */

string DtoB(int Decimal);

string DtoB(int Decimal)
{
	int steps[] = {128, 64, 32, 16, 8, 4, 2, 1};
	string binary;

		if(Decimal > 0)
		{
			int stepLen = sizeof(steps) / sizeof(int);
			for(int x = 0; x < stepLen; x++)
			{
			if(Decimal >= steps[x])
				{
				Decimal -= steps[x];
				binary += "1";
				}
			else
				binary += "0";
			}
		}
	else
		binary = "0";

return binary;
}
/* END DECIMAL TO BINARY CONVERSION*/

/* CONVERT BINARY TO DECIMAL */

void BaseGen(int Base[], int Size)
{
Base[0]=1;
int Index=1;
while(Index<Size)
	{
	Base[Index]=Base[Index-1]*2;
	Index=Index+1;
	}
	return ;
}

int BtoD(string Binary)

{ 
	unsigned int Index=1;
  	Index=0;
	int Dv=0;
	int Base[8];
	BaseGen(Base,8);
	while(Index<Binary.size())
	{
		if (Binary[Index]=='1')
			Dv=Dv+Base[Binary.size()-1-Index];
			Index=Index+1;
	}
		return Dv;
}

/* END BINARY TO DECIMAL CONVERSION */

/* BEGIN DP. WHAT SAY YOU? */

int main()
{
	string Bin;	

		cout << "Please type any valid integer no larger than 255: " << endl;
		int Decimal;
		cin >> Decimal;

		if (Decimal > 255)
			{
			cout << "Game Over." << endl;
			system("PAUSE");
			return(0);
			}
		else
			{
			cout << "Binary: " << DtoB(Decimal) << endl;
			}
	
		cout << "Please type any valid binary number of eight or less bits: " << endl;
		int Decimal2;
		cin >> Bin;

		Decimal2=BtoD(Bin);
		cout << "Decimal: " << Decimal2 << endl;
		system("PAUSE");
	
	return(0);
}

/* END DP. WHAT SAY YOU? */

*claps*

You didn't "model" a binary string like he told you to ( string binary="00000000";)

I think what he wants you to do is something similar to what he was doing in the BtoD routine he gave you, where you directly change the string elements using an array-like assignment.

binary[position] = '1';

or

binary[position] = '0';

But everything does appear as though it will work fine. Those are just really suggestions to make it follow as close to the instructions as possible. Good job getting through it though!

Looks good ! I have one suggestion.

Instead of having a standard array "steps" you could write a BaseGenReverse, similar to your teacher's BaseGen function and use that instead. So if the size increases it will automatically set your steps array to that bit size.

You could start with Base = 1, set Index to Size - 1, and then decrement your Index and loop until Index was 0, setting Base[Index - 1] to Base[Index] * 2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.