C++ Tree

So im supposed to make a c++ tree that inputs a single character string (minimum of 1 and maximum of 63 characters), and outputs the string as a complete binary tree, one character per line, with indentation to show what level that character is at on the complete binary tree.

Basically, I have no idea how to even start it.
I not sure what you mean. Do you mean have the user enter a single character such as 'a' or a string of characters such as 'Hi there' and then convert it to binary code?

If the string of characters is your case then here is my idea.

1
2
3
4
5
6
//Get a user character string using char[] or char*
//Convert every letter to ascii using the int()
//ex: int( myvar[2] );

//then get the ascii numbers and convert them to binary



That sounds like an interesting challenge, I will have to try and code that but for now that should give you an idea of how to start


Edit:: I have successfully coded this in about a half hour using my method.
To convert the ascii to binary have a look at this function.

http://www.engin.umd.umich.edu/CIS/course.des/cis400/cpp/binary.html

What I did was create an array of char with 63 as the maximum number of characters it could hold. Then I used a for loop combined with an if statement to check for a '0' (that means the string is ended). I then created a vector<char> that held the pure string and no blanks like the character array.
To convert the vector of characters to ascii I created a vector of integers. Using a for loop to cycle through the vector<char> I converted all the characters to integers and then converted them to binary.

Btw This sounds like homework so I'm not going to give out the code unless you make an effort to create something yourself.
Last edited on
FYI: If you are looking for '0' to end the "string" you won't find it. The actually character is '\0'. But other then that, yeah, I don't really get what you are wanting...clarify please?
When you initialize an array without values all of the values are zero, i think. As soon as the code notices a zero it ends. My code works with the zero... I'll test it with a null character

Edit: Both ways work.
Last edited on
When you initialize an array without values all of the values are zero, i think. As soon as the code notices a zero it ends. My code works with the zero... I'll test it with a null character

Edit: Both ways work.


Only with Microsoft's Compiler run through the CLI if I am not mistaken. GCC/MingW/BCC do not default values to a 0, and the C++ standard does not specify this behaviour. So it's definitely not something you should rely on.
Topic archived. No new replies allowed.