• 0

Visual C++ Midterm... HELP!


Question

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.

Edit/Delete Message

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

You have no idea what to do at all ? Do you understand that you will have to code a routine that takes an integer between 0 and 255 and convert it to a string containing its binary representation ? Do you know how to convert from base 10 to base 2, in theory ? Try doing the conversion manually, on paper, and from that you can figure out what the C++ algorithm should be. It's a very simple problem.

Link to comment
Share on other sites

  • 0

I understand that but what I don't understand is that converting decimal to binary was one of the first assignments we ever did in class and it was only a couple lines of code. What I don't understand is the example code he gives because when we did it before it didn't look anything like that.

I'm confused on if he wants the BtoD and DtoB in the same program and, if so, how I would make it so that it can automatically be detected which was entered and then convert it when the program starts.

Link to comment
Share on other sites

  • 0

If I understand well, you already have a program that asks the user for a binary number, converts it to decimal using "int BtoD(string binary)", and displays the result to the user to verify that the conversion is correct. What you need to do is develop a function that does the contrary and have the main method test it in the same way, after the first one. So the program will ask the user for a binary number, display the decimal representation, then ask the user for a decimal number and display its binary representation. There is no need to detect what kind of number is entered, you just tell the user what he's supposed to enter and assume it's valid.

Link to comment
Share on other sites

  • 0

So you're saying use two prompts? First ask the user to input a binary and convert to decimal, then once that is complete, ask the user to input a decimal and convert it to binary?

Link to comment
Share on other sites

  • 0

The assignment says "enhance the driver program to test an additional function DtoB", so yeah I would have the program have two prompts and test both functions one after the other. But in doubt you should ask your teacher, at this point it's not a C++ problem anymore. :)

Link to comment
Share on other sites

  • 0

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 &lt;iostream&gt;
#include &lt;string&gt;
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 &gt; 0)
{
int stepLen = sizeof(steps) / sizeof(int);
for(int x = 0; x &lt; stepLen; x++)
{
if(Decimal &gt;= steps[x])
{
Decimal -= steps[x];
binary += "1";
}
else
binary += "0";
}
}
else
binary = "0";

return binary;

}



// The driver program follows


int main()
{
cout &lt;&lt; "Enter an integer no larger than 255: ";
int Decimal;
cin &gt;&gt; Decimal;
if (Decimal &gt; 255)
{
cout &lt;&lt; "Please follow instructions" &lt;&lt; endl;
}
else
{
cout &lt;&lt; "Binary: " &lt;&lt; dec2bin(Decimal) &lt;&lt; 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?

Link to comment
Share on other sites

  • 0

Well, 1101 is valid in both base 2 and 10, so do as every other program does and ask the user to add a letter after the number, b for binary and either d or nothing for decimal. Then your program has to check for that letter to select which function to use. But reading your assignment I don't think that's what you're required to do; the program is supposed to test both functions, but can very well ask for a single input and do both conversions one after the other, as Leo Nathan said, or ask for two inputs as I proposed.

Link to comment
Share on other sites

  • 0

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 &lt;iostream&gt;
#include &lt;string&gt;
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 &gt; 0)
		{
			int stepLen = sizeof(steps) / sizeof(int);
			for(int x = 0; x &lt; stepLen; x++)
			{
			if(Decimal &gt;= 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&lt;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&lt;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 &lt;&lt; "Please type any valid integer no larger than 255: " &lt;&lt; endl;
		int Decimal;
		cin &gt;&gt; Decimal;

		if (Decimal &gt; 255)
			{
			cout &lt;&lt; "Game Over." &lt;&lt; endl;
			system("PAUSE");
			return(0);
			}
		else
			{
			cout &lt;&lt; "Binary: " &lt;&lt; DtoB(Decimal) &lt;&lt; endl;
			}

		cout &lt;&lt; "Please type any valid binary number of eight or less bits: " &lt;&lt; endl;
		int Decimal2;
		cin &gt;&gt; Bin;

		Decimal2=BtoD(Bin);
		cout &lt;&lt; "Decimal: " &lt;&lt; Decimal2 &lt;&lt; endl;
		system("PAUSE");

	return(0);
}

/* END DP. WHAT SAY YOU? */

Link to comment
Share on other sites

  • 0

Your indentation is inconsistent and even sometimes misleading. This :

(1)	while(Index&lt;Binary.size())
(2)	{
(3)		if (Binary[Index]=='1')
(4)			Dv=Dv+Base[Binary.size()-1-Index];
(5)			Index=Index+1;
(6)	}

visually suggests that both line 4 and 5 are included under the if statement, while in fact only line 4 is. I've seen this lead to a very hard to find bug in a commercial software I was working on. I'd always enclose the body of an if statement with brackets even if it's a single line.

If you're working with Visual Studio, hit CTRL+A, CTLR+K, CTRL+F. This will auto-indent the code. Other IDEs probably also have that functionality. Sometimes you indent your brackets, sometimes you don't, which can be confusing.

Link to comment
Share on other sites

  • 0

Yeah I think there are a few different methods on where to place the brackets also too but the key is to be consistent. It's always a good idea to use braces to enclose the cose as Dr_Asik says. Makes it easy to see what code is contained within the IF

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.