Author Topic: Decimal to Binary converter.  (Read 2212 times)

0 Members and 1 Guest are viewing this topic.

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Decimal to Binary converter.
« on: April 10, 2013, 08:04:59 am »
Dont know why but i keep getting a shit load of "0"s in the result. Check my code pleas. This is my first freertyle project. (so dont run a train on me)

also i tryed to use the b variables as enumerated data types but end up with blank output, i tryed  to set their type as chars but same result. Then i used int and im ending up with all 0s. >.<
My secrets have secrets...

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Decimal to Binary converter.
« Reply #1 on: April 10, 2013, 09:37:24 am »
I wrote you a little example :)

Code: (c++) [Select]
#include <iostream>
#include <bitset>
int main()
{
        int nDec = 20;
        std::cout << std::bitset<8>(nDec);
        return 0;
}
If you don't know what the <8> represents i highly recommend you to study the template and containers features in c++ :)
~Factionwars

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: Decimal to Binary converter.
« Reply #2 on: April 10, 2013, 09:58:51 am »
Yes but whats wrong with my code?
My secrets have secrets...

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Decimal to Binary converter.
« Reply #3 on: April 10, 2013, 10:11:32 am »
Yes but whats wrong with my code?
your code sucks, easily said. It sucks when you can't even debug your own code. I debugged some but the whole flow is incorrect.
~Factionwars

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: Decimal to Binary converter.
« Reply #4 on: April 10, 2013, 10:48:33 am »
>,< i feel like a worthless pice of shit.

But all i want to know is why doesnt program show the correct result? It has the variables fixed and the values are arigned to them correctly(i checked twice before compiling) the compiler doesnt even show any error msg.

I started this small project so i can keep track of the small knowledge i have about c++, im still learning and if you just tell me whats wrong with my code i will have a clear idea of  what i should  revise.
So tell me whats wrong, insted of saying it sucks etc etc.
My secrets have secrets...

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Decimal to Binary converter.
« Reply #5 on: April 10, 2013, 10:53:28 am »
>,< i feel like a worthless pice of shit.

But all i want to know is why doesnt program show the correct result? It has the variables fixed and the values are arigned to them correctly(i checked twice before compiling) the compiler doesnt even show any error msg.

I started this small project so i can keep track of the small knowledge i have about c++, im still learning and if you just tell me whats wrong with my code i will have a clear idea of  what i should  revise.
So tell me whats wrong, insted of saying it sucks etc etc.
I can't just tell you whats wrong. One of the essential skills of a programmer is DEBUGGING, you will have to trace the variables and find out where it does not do what you expect it to do. If you can't debug, and only ask other people to debug your code, you can just quit programming.
So place a shitload of std::couts in functions or use GDB to debug your application.
~Factionwars

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Decimal to Binary converter.
« Reply #6 on: April 10, 2013, 11:18:21 am »
>,< i feel like a worthless pice of shit.

But all i want to know is why doesnt program show the correct result? It has the variables fixed and the values are arigned to them correctly(i checked twice before compiling) the compiler doesnt even show any error msg.

I started this small project so i can keep track of the small knowledge i have about c++, im still learning and if you just tell me whats wrong with my code i will have a clear idea of  what i should  revise.
So tell me whats wrong, insted of saying it sucks etc etc.
You want us to spoonfeed you. TBH you have a bitchy attitude and a wrong mindset. We can advise you on what is wrong and what could be done better, and it is up to you to fix it.

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: Decimal to Binary converter.
« Reply #7 on: April 10, 2013, 11:30:20 am »
Just to let you know, im currently learning from www.learncpp.com/

and im only in chapter 4.6 right now (and still learning everyday). so as i said befor. I only started this project to see if i have any lack of knowledge that i learned in the previous chapters. Becaus i dont want to have lack of knowledge. befor going forward  If there is then i will go back and revise them. But i cant to find them.(i cant test myself) 
My secrets have secrets...

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Decimal to Binary converter.
« Reply #8 on: April 10, 2013, 11:37:54 am »
Just to let you know, im currently learning from www.learncpp.com/

and im only in chapter 4.6 right now (and still learning everyday). so as i said befor. I only started this project to see if i have any lack of knowledge that i learned in the previous chapters. Becaus i dont want to have lack of knowledge. befor going forward  If there is then i will go back and revise them. But i cant to find them.(i cant test myself) 
"(i cant test myself) "  as i said before, if you can't even debug that code you should quit. Debugging is THE learning curve. If you type everything 1 on 1 from the tutorials you won't learn anything. Because your brain does not process the information, it only copies it and does only take little details in memory.

You wrote something yourself, that is good. But when you DEBUG you will learn the real workings of things. You will debug functions you expected to do what you intended but they did not, thus you have learned something wrong. That is why you have to DEBUG so your brain can correct itself and so it knows what it does exactly the next time you try to write that functionality.
~Factionwars

Offline 0poitr

  • Peasant
  • *
  • Posts: 149
  • Cookies: 64
    • View Profile
Re: Decimal to Binary converter.
« Reply #9 on: April 10, 2013, 01:14:50 pm »
I'll tell you one thing. Don't look at the solutions. Because, if you do, you lose the motivation to do it yourself. Know the theory and do it step by step as you think it should be done. Procedure , dude. If it doesn't work alright, debug line by line. Learn to use a debugger. It's worth the time.
Imagination is the first step towards Creation.

Offline icon

  • Serf
  • *
  • Posts: 26
  • Cookies: 6
  • Ghost
    • View Profile
Re: Decimal to Binary converter.
« Reply #10 on: April 13, 2013, 09:48:57 pm »
Interesting. I don't know much about C++, but it would seem like you're going about things the hard way. I'm pulling from Java knowledge, but doesn't C++ also have something like a byte array? Worst case write a program that converts data to this format, then pulls from that format if you need to convert back. Just a thought.
De Oppresso Liber

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: Decimal to Binary converter.
« Reply #11 on: April 14, 2013, 07:03:57 am »
Interesting. I don't know much about C++, but it would seem like you're going about things the hard way. I'm pulling from Java knowledge, but doesn't C++ also have something like a byte array? Worst case write a program that converts data to this format, then pulls from that format if you need to convert back. Just a thought.

That or he could just use bit shifting operators from C to do the conversion which isn't too hard.  and bitset<8> is what Factionwars mentioned but that doesn't always work as I tried it before and it didn't convert a char for instance and I was too lazy to figure out why so I just used bit shifting.
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.