• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

C++ Midterm... HELP!

cwilhelm

Weaksauce
Joined
Mar 15, 2007
Messages
89
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.
 
So your basically writing a class to take an object and pass it through a class that you wrote? I'm not fully understanding what your trying to do here, could you give a deeper explanation?

-Edit-

Ok I think I sorta understand what your trying to do. Write a class/function DtoB to test decimal number and convert to binary. So is this a test? Is it suppose to display output only if number entered matches? Cause it be a waste of a function to write if it doesn't do anything. Need more info before I can help further....
 
Since BtoDSol.cpp converts binary to decimal, I think all he is asking of this assignment is to add the ability to convert decimal (<= 255) to binary as well.

If the number entered is greater than 255 then I'm assuming he wants the program to tell the user to enter a different number.

I'm about as confused as you on this one, honestly. He's very vague.
 
I know the code already to convert a decimal number to a binary but I'm just curious what the program should display when it's first run. With BtoD, since there is only one capability, it simply asks to enter a binary number and it's converted. With this modification, am I supposed to make it so that it can automatically detect whether a binary or decimal number was entered and then convert it?
 
Phew..I was going crazy trying to do this in C++. I haven't used it in a few years..so I wrote it in Java and was trying to convert over...that a relief..
Anyways, I think he wants you to compare the two binary numbers. When he says 'test' i figure you through a if/else statement to compare B and D binary #'s. If they are the same go ahead and print the number out, if its no prompt the decimal is bigger then 255 or something.

-OR-

I can also see that he just wants you to convert the opposite...if user enters a decimal..return binary. This makes more sense, but question is too vague to know for sure.
 
// 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 ;
}
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;
}
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;
}

Here is the code structured if someone needs it for further help (proper indentation makes things readable).

The program he is asking for is called BtoDSol.cpp. The first function is BtoD which would mean it is Binary to Decimal. This is where some block comments would be nice (0(^_^)0 am I right guys?). So the next function he is asking you to make (which is the first part you have and I just injected it into the main code he gave you) is DtoB which would mean Decimal to Binary. I would assume he wants you to take any valid decimal (of 255 or less (<=255)) and convert it to Binary.

So based on what he has asked I would assume that he wants you to implement the DtoB function and to modify the main code to ask and determine if the user wants to convert binary to decimal or decimal to binary. He does not state it needs to automatically determine this ( you can do that if you want and would be logical) or you can make a small case setup for enter 1 if you wish to convert binary to decimal or press 2 if you want decimal to binary. I'll work on the actual code for now to see what I come up with and just post to this, I will check it for another hour or 2 to see if you need further help.
 
So really, in that spot where he wants me to input the rest of the code, isn't it really only a couple lines to convert decimal to binary? I just throw that in there and it's done, no? If you come up with something different let me know.
 
Yes, YOu need to write the BtoD function (From Binary to Decimal) and also modify main to accept the input for that program. Aka have a menu option set up or determine if the number is binary or decimal then pick the right function.

So write BtoD -> Modify main to get input and utilize BtoD.
 
You think the best way to go about modifying main is to just set it up to ask if the user wants to convert from DtoB or BtoD?
 
I'm not sure how much you have covered or what classes you are allowed to use or not use. So if you need help with writing main, I have it written including error checking for invalid input of any kind if you need it. It does loop and asks for input till you tell it to quit. Not sure if that is what he is looking for. It does use some more advanced techniques.
 
this is how I would do it...

Code:
int DtoB (int a)
{
     if ( a > 1 )
          cout << a % 2 ;
     DtoB( a / 2 ) ;
}

I forget how to use string but I'm sure you could do something similar
 
i can't believe there is an entire thread of people giving help on a _graded_ assignment.

the point of the assignment isn't to see how many people it takes to figure out the answer, or to even figure out the prof's question, for that matter.

if you don't understand the question, and you think it's because of the way it is worded, ask your prof for help. if that person refuses to assist, DEAL WITH IT LIKE THE REST OF US, or talk to his/her supervisor.

get some courage. you're going to have a lot more difficult situations in the work force than being afraid to ask your prof a question.

your post was entirely inappropriate, and i hope the mods will consider some remedial action to deal with this. [H] is a place for discussion and learning. not a place to get your midterms answered for you.
 
Calm down Hitler, the assignment is due by midnight tonight and the professor never responds to emails and this isn't related to my major in any way, just a gen-ed. And to BinaryApe, advanced techniques are acceptable, the program can be solved in any way.
 
I actually have given it effort but I'm told that the program could be written in seven lines or less. When I did a program by itself to convert decimal to binary, this is what I did.

#include <iostream>
using namespace std;

// Decimal to Binary Converter
// Declare
// Dec the Variable to Store the Input
// P7=128, P6=64, P5=32, P4=16, P3=8, P2=4, P1=2, P0=1
// Position Value
// B7, B6, B5, B4, B3, B2, B1, B0
// Hold Results
int main ()
{
int Dec;
int P7=128, P6=64, P5=32, P4=16, P3=8, P2=4, P1=2, P0=1;
int B7=0, B6=0, B5=0, B4=0, B3=0, B2=0, B1=0, B0=0;
// Input Dec
cout << "Please Type a Decimal Number: ";
cin >> Dec;

// Compare Dec to P7

if (Dec >= P7)
{Dec = Dec - P7; B7 = 1;};

if (Dec >= P6)
{Dec = Dec - P6; B6 = 1;};

if (Dec >= P5)
{Dec = Dec - P5; B5 = 1;};

if (Dec >= P4)
{Dec = Dec - P4; B4 = 1;};

if (Dec >= P3)
{Dec = Dec - P3; B3 = 1;};

if (Dec >= P2)
{Dec = Dec - P2; B2 = 1;};

if (Dec >= P1)
{Dec = Dec - P1; B1 = 1;};

B0 = Dec;

cout << "The Binary Number for " << Dec << " is: " << endl;
cout << B7 << " "
<< B6 << " "
<< B5 << " "
<< B4 << " "
<< B3 << " "
<< B2 << " "
<< B1 << " "
<< B0 << endl;

system("PAUSE");
return 0;
}

I realize this is probably the most basic method of doing it and that there has to be a shorter way of going about doing this. And if you look at this code, I don't know how I would fit it in with his partial code or still how to make the program detect or request the input.
 
My code involves no string where his partial does. The method he seems to be using is much much different than that of mine and I'm not sure how to go about doing it with a string.
 
How about something like this, decimal to binary in a string:

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::cin;
using std::endl;

string dec2bin(int num);

int main()
{
cout << "Enter an integer no larger than 255: ";
int num;
cin >> num;
cout << "Binary: " << dec2bin(num) << endl;

system("pause");
return 0;
}

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

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

return binary;
}

How would I go about adding this into the program or a loop? I keep doing these things separately because I don't know how to do the initial prompt.
 
Calm down Hitler, the assignment is due by midnight tonight and the professor never responds to emails and this isn't related to my major in any way, just a gen-ed. And to BinaryApe, advanced techniques are acceptable, the program can be solved in any way.


http://hardforum.com/showthread.php?t=810619

if you are not banned, i will be severely disappointed, as it will show that [H] has fallen into a forum where people will do your homework (or midterms) for you. this isn't the first i've seen this happen this year.
 
Okay I've been working with it, here's what I have now for decimal to binary.

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


string dec2bin(int num);


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

if(num > 0)
{
int stepLen = sizeof(steps) / sizeof(int);
for(int x = 0; x < stepLen; x++)
{
if(num >= steps[x])
{
num -= 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 num;
cin >> num;
if (num > 255)
{
cout << "Your number is too large" << endl;
}
else
{
cout << "Binary: " << dec2bin(num) << endl;
}
system("PAUSE");
return 0;
}
 
my way does work. it just needs to be changed to work with his program and needs to be stringafied. ha.
 
Yeah I did stringify it, now it's just a matter of combining the two programs and making it functional. I'm not sure if there's some kind of command to detect whether it's binary or decimal and then convert it...
 
http://hardforum.com/showthread.php?t=810619

if you are not banned, i will be severely disappointed, as it will show that [H] has fallen into a forum where people will do your homework (or midterms) for you. this isn't the first i've seen this happen this year.

Maybe you should read the sticky yourself. It says not to post homework assignments ASKING FOR THE ANSWER. He was asking for help about what we think would be a good way to go about it and not be like OMFG GUYS THIS PROGRAM DUE PLEASE GIVE ANSWER KTHXBYE. This is in no way against the rules. Not sure why you are so angry and wanting someone to get banned for asking about an assignment thinking if we can help point in the right direction. That is what we are here for is it not?

As to you cwilhelm, why not simply modify the application he gave you? Look at the BtoD, think about the logic in it, and what would work to do the reverse process. If you want to hit 7 lines or less (Minus declarations for variables it is possible in 7
 
It doesn't necessarily have to be seven lines or less, I was just told that it could be done in so. As long as it works, which mine seems to work just fine, it doesn't much matter to me if it turns out as long as it did.

I won't have a problem combining the programs but I'm thinking it'll be nearly impossible to determine whether the user wants one or the other converted, given that someone could put in a number like 101 or something along those lines.

In this case, I'll most likely set the program to ask on startup if the user wants to convert decimal to binary or binary to decimal and then ask for their number. Now I know goto commands are frowned upon but that's what I'm familiar with... would a better way of doing it be to simply state press 1 for DtoB or 2 for BtoD and then do an if/else statement based on their input?
 
my way does work. it just needs to be changed to work with his program and needs to be stringafied. ha.
You've got most of it, but you have no base case. You've got an unconditional recursive call, so it's never going to terminate.

Maybe you should read the sticky yourself. It says not to post homework assignments ASKING FOR THE ANSWER. He was asking for help about what we think would be a good way to go about it...
Take another look at the original post... He just pasted the question in and said "I can't do it. Help me". Without a specific question or a partial attempt, one can only assume that he was waiting to be handed a solution.
 
I dislike goto and jump commands cause they can make things really messy. I would go with a menu option, I also do error checking based on their selection and can error check the input they try sending to make sure it is correct.
 
yes ask the user to press 1 or 2. that's the norm way to do it using a switch. don't use goto. bad.
 
oh woops. yea I had the two lines flipped. should be...

Code:
void DtoB (int a)
{

     if ( a > 1 )
     {
          DtoB( a / 2 ) ;
     }
     
     cout << a % 2 ;

}



sorry :( it's been forever since I used c++
 
yea, it's really confusing to figure it out and how it works. but it does work. haha.
 
I figured you would need to have >= to have it work, but it doesn't since that iteration will return nothing. Only thing you need to do is declare it void instead of int (it doesn't return anything 0(^_^)0)
 
with string can you use += ? so you could do like... string += a % 2 to add the remander to the end of the string? I always used apstring even after high school haha. you could do it with that iirc. I've never used the string class
 
I'm cleaning up my code some and I have it setup so that if the user enters a number greater than 255, it requests a new number. However, is there a function that can make it so that if the user enters random characters or letters the program doesn't crash?

Here's what I came up with for the loop of just DtoB.

Code:
int main()

{

	char yn;

do
{
	{
		cout << "Enter an integer no larger than 255: ";
		int Decimal;
		cin >> Decimal;
		if (Decimal > 255)
{
cout << "Your number is too large" << endl;
}
		else
{
cout << "Binary: " << dec2bin(Decimal) << endl;
}
	}
	cout << "Again? y/n: " << endl;
		cin >> yn;
}
while (yn == 'y');
return 0;
}

Now the user can enter numbers repeatedly if they choose and if they enter larger than 255 it'll ask them if they wish to try again. However, if they enter letters, the program crashes. Also, if they enter a capital Y then it interprets it as not wanting to try again. How can I make it to read y and Y both as repeat? I tried while (yn == 'y'||'Y'); and that didn't work.
 
don't think can use my function exactly how I had it. seems the assignment wants you to use a string. and usually on these kinds of questions you must keep everything else the way it is, just adding the code where it says // you implement the rest etc etc.

at least thats how my teachers/professors/boss wanted it done.
 
Maybe you should read the sticky yourself. It says not to post homework assignments ASKING FOR THE ANSWER. He was asking for help about what we think would be a good way to go about it and not be like OMFG GUYS THIS PROGRAM DUE PLEASE GIVE ANSWER KTHXBYE. This is in no way against the rules. Not sure why you are so angry and wanting someone to get banned for asking about an assignment thinking if we can help point in the right direction. That is what we are here for is it not?

1) he says in the OP that he needs help because it's for his grade
2) he initially didn't even make an attempt at posting the logic of his work; all he stopped in and said was: "i don't know how to do this!!"
3) in a later post, his logic for asking for an answer was: "it's due monday" and that it didn't have "anything to do" with his major

that sounds like a perfect example of the sticky.

why was i so angry? because i've seen absolute morons pass classes with fair grades because they post their homework on the internet. for those of us who actually do our own work, it makes our efforts appear as less. i've been in his situation. i've struggled on assignments much tougher than this. i still do struggle on assignments in grad school, in addition to working.

i saw nothing much in this thread that indicated the OP was willing to put his own effort in and explain why he thinks it isn't working. i saw begging for answers. this isn't "help," this is "cheating."
 
apparently, this dude IS just looking for answers

http://www.daniweb.com/forums/thread151867.html
http://www.gidforums.com/t-19505.html
http://www.dreamincode.net/forums/showtopic68042.htm
http://www.neowin.net/forum/lofiversion/index.php/t684022.html
http://www.go4expert.com/forums/showthread.php?t=14604
http://forumsynd.com/2008/10/c-midterm-help-please/
http://www.dreamincode.net/forums/showtopic68042.htm


and on that last one, i quote: "Seriously, can someone just tell me what to do? I am not getting anywhere, I've been staring at this and messing with it for hours and I keep getting errors and errors and I'm not making any progress. I don't know where to even insert the new code, how to make it capable of detecting whether it needs to be DtoB or BtoD...I can't figure out any of this.

Man this sucks assssssss."
 
Look at the times on the posts. Yeah, you're right, I initially wanted someone to just do it for me. Then go back to every single one of those links you posted. Nobody did it for me. Hence why I've been in this thread working out the code myself and asking for input.

I'm basically done with it now, I'm just not sure how to do do/while statements inside of other ones, inside of other ones, inside of other ones, etc. I'm not even sure if that's possible or if that's what's being asked of me is the thing.

And I think it says something about your social life that you went out and found all those.
 
1) he says in the OP that he needs help because it's for his grade
2) he initially didn't even make an attempt at posting the logic of his work; all he stopped in and said was: "i don't know how to do this!!"
3) in a later post, his logic for asking for an answer was: "it's due monday" and that it didn't have "anything to do" with his major

that sounds like a perfect example of the sticky.

why was i so angry? because i've seen absolute morons pass classes with fair grades because they post their homework on the internet. for those of us who actually do our own work, it makes our efforts appear as less. i've been in his situation. i've struggled on assignments much tougher than this. i still do struggle on assignments in grad school, in addition to working.

i saw nothing much in this thread that indicated the OP was willing to put his own effort in and explain why he thinks it isn't working. i saw begging for answers. this isn't "help," this is "cheating."

1) There is nothing wrong asking for help on a assignment (even if it was for his grade)
2) So we all assumed he didn't know where to start (even if it was not what he was asking), amazing how everyone thought of it like that but you jumped at him thinking it was to ask for an answer. We clarified what it says for him about what he is to do, and gave him nothing else. Except Lmaulle (which someone pointed out)

3) It was about as bad a response as your first one was.

You were angry when other got by because of someone else's help? I could care less in my class if the jock went to someone else and they helped walked him through it, I cared very much if I was Teamed with him and had to do all the work (which I had to do 90% through school).

Even if the do pass, in the end it will hurt them badly. Trust me, nothing funnier then going out to the real world and watching them fail. Just like one of my former class mates (who I know didn't ever do his work) has a major in CS and works at McDonalds.

At Cwilhelm: Why would he have to have no life to find those. Took him probably about 10-15 minutes.
 
Back
Top