Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, i'm fairly new to C++ (started about 3 or 4 months ago. I posted a previous code but made many changes and haven't came around to posting the new code until now. I always know there will be room for improvement so please judge very aggressively with me as I am always looking for improvements.

Here it is:

C++
#include <iostream>
void recur(int convert)
{
    if(convert == 0) //if input is 0 , return nothing.
    {
            return;
    }
        recur(convert/2); // divide convert by 2, get only a 1 or 0
std::cout<<convert % 2; //write remainder
}


void send(int convert)
{
    while (convert != 0)
    {//while convert is not equal to the value we initialized
    std::cin.ignore();//clears previous input before taking new input
    }
    std::cin>>convert;
recur(convert);//call recursive function to do the conversion for us
}


int main()
{
int convert = 0; //this is to be converted, in the meantime it must be initialized to a value
    while (convert == 0)
    { //this while gives us a loop for a immediate other input
send(0);
std::cout<<""<<std::endl;
    }
}
Posted
Updated 26-Sep-14 12:36pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Sep-14 18:39pm    
What does it mean? Number is already "binary". Do you mean to get a binary string representation, like "11001101"? Then return string instead of using std::cout (instead of string output), because it could not be reused.
—SA
Philippe Mori 26-Sep-14 19:08pm    
What is the purpose of send function? Or the loop Inside it. How would the loop be exited?

No, not good at all. First of all, it does not return anything, does not even allow the user to put data in a string. Second of all, the implementation is way too inefficient, compared even to the simplest reasonable implementation. Finally, the function could be generic, otherwise how would yo reuse the code for long int, unsigned int, and so on? You could use a C++ template.

So first, you need to have a function profile like (let's forget templates for now).
C++
std::string ToBinary(int value);

or, to use just the null-terminated strings, in C style:
C++
void ToBinary(char* string, int value);

In the second case, the user needs to be held responsible for providing a reference to a string which provides enough room for all the binary digits, according to the size of data.

Finally, learn a recipe for extracting separate bits form data.
First, use binary '<<' and '&' operators. If you need to extract N-th byte, where N is the integer index of the bit, counted from least-significant bit #0 (rightmost) toward a most-significant bit, first get the mask equals to 1 << N. If you then AND this mask with your value, you will get either 0 value or not:
C++
if (value & (1 << N) == 0)
   // the extracted Nth bit is 0
else
   // the extracted Nth bit is 1
(Put real code instead of my comments.)
Using these ideas, please write the whole implementation.

See also:
http://www.cplusplus.com/reference/string/string[^],
http://www.learncpp.com/cpp-tutorial/38-bitwise-operators[^].

—SA
 
Share this answer
 
v4
Comments
[no name] 26-Sep-14 22:26pm    
Never mind , I deleted the whole thing, I've been programming for 4 months and can't even get this right , it was stupid for me to even consider learning this stuff. Sorry for wasting your time, and thank you.
Sergey Alexandrovich Kryukov 26-Sep-14 23:01pm    
No, no, this is not wasting time. Go slowly, step by step, ask questions, never hesitate to go back and learn what you did not understand. Why, for example, not completing this problem? Just start over again. It is useful enough. We all make a lot of mistakes.
—SA
[no name] 26-Sep-14 23:38pm    
I will start over again, but with something easier, because clearly I underestimated the difficulty of doing this program right. Be honest, would you expect better from 4 months of trying to teach yourself C++?And also to go back into topic, when you wrote "void ToBinary(char* string, int value);" is it just a matter of preference to write char* string instead of char *string? And char* string means pointer of a string am I correct? Why would I ever need to use a pointer for in this?

As another note, thank you for taking your time to help me, it really does keep me motivated when people encourage each other to keep trying.
Sergey Alexandrovich Kryukov 27-Sep-14 0:35am    
Good idea. It you feel unprepared for solving some problem, getting back to some more basic thing and latter getting back to it, when you gain sufficient confidence, is a reasonable strategy.

No, there is nothing bad with having not enough experience after 4 months of the study, it you are just starting with computer science. I must say that even in that bad solution, you demonstrated your ability to think; there is an interesting idea of using recursion and changing the operand in each step. Such idea would need further elaboration and would be adequate in more complex cases. It's apparent that you are not still aware what approach costs what, and it comes with experience.

—SA
numbers_game wrote:
And also to go back into topic, when you wrote "void ToBinary(char* string, int value);" is it just a matter of preference to write char* string instead of char *string? And char* string means pointer of a string am I correct? Why would I ever need to use a pointer for in this?
Let's see…

The declarations char *string, char * string and char* string are identical in this content, but if you need to have type along, for example in typedef, it will be char*, which makes thing logical. As the variable or member declaration syntax is "typeName variableName", the meaning of declaration with pointer is (char *) variable. (Brackets are redundant, but I put them to show what's the meaning of the construct.

But this is not so trivial, because it's also possible to write:
C++
char *stringValue, characterValue, *anotherStringValue;

which is equivalent to
C++
char* stringValue;
char characterValue;
char* anotherStringValie;


Can you see the point?

Anyway, I would strongly recommend to read some textbook from the very beginning to the very end, preferably doing exercises from the book as you go.

For online tutorial, I would recommend this one: http://www.learncpp.com[^].

It's also good to read the books by Bjarne Stroustrup, which are not exactly the easiest for understanding, but they can give you ideas on C++ rationale.

—SA
 
Share this answer
 
v2
Comments
[no name] 27-Sep-14 22:51pm    
I'm still reading a C++ book (C++ primer , by Stanley Lippmann) I am almost half way through it with going through all the exercises. I do have a Bjarne Stroustrup book , but it is fairly old and i do not know how much i will learn from that. But this book I am reading is recent with C++11 standards. And thanks for making this clear for me, it's little things like writing char* namestring instead of char *namestring that keep me researching for hours why it is that. You cleared my mind Mr.Krukov, I will keep studying via books and research, hopefully in the near future I will be able to write much more complex code for you to judge! Thank you!
Sergey Alexandrovich Kryukov 28-Sep-14 1:36am    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900