473,382 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

My code is work but the answer didn't appear.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. //#include<process.h>
  3. #define Max 10
  4. class Binary{
  5.         int data[Max];
  6.         int top;
  7.     public:
  8.         int empty(){top=-1;return -1;}
  9.         int full();
  10.         void push(int);
  11.         int pop(int);
  12. };
  13. int Binary::full(){
  14.     if(top==Max-1)
  15.         return 1;
  16.     else
  17.         return 0;
  18. }
  19. void Binary::push(int num){
  20.     top=top+1;
  21.     data[top]=num;
  22. }
  23. int Binary::pop(int num){
  24.     num=data[top];
  25.     top=top-1;
  26.     return num;
  27. }
  28. void main(){
  29.     Binary ptr;
  30.     int num;
  31.     ptr.empty();
  32.     cout<<"Input Number : ";cin>>num;
  33.     while(num!=0){
  34.         if(!ptr.full()){
  35.             ptr.push(num%2);
  36.             num=num/2;
  37.             }
  38.         else{
  39.             cout<<"Full";
  40.             break;
  41.             }
  42.         }
  43.     cout<<"In Binary : ";
  44.     while(!ptr.empty()){
  45.         num=ptr.pop(num);
  46.         cout<<num;
  47.         }
  48. }
Dec 25 '14 #1
3 1305
weaknessforcats
9,208 Expert Mod 8TB
Your program is reaching the end of main() and there is nothing there to stop it so it goes away.

Add a statement at the end of main to enter a character. That will stop the program until you press the enter key . Now you have time to see what was displayed.
Dec 25 '14 #2
Example I input 10 and my output is :

Input Number : 10
In Binary :

// It didn't show answer in Binary
Dec 26 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
Your code is not working.

At the end of main() there is:

Expand|Select|Wrap|Line Numbers
  1.     cout << "In Binary : ";
  2.     while (!ptr.empty()){
  3.         num = ptr.pop(num);
  4.         cout << num;
  5.     }
but ptr.empty() always returns -1 and that causes an exit of the loop.

Beyond that, the ptr.data, for 10, contains 0101 and some garbage values. 0101 is not 10. It's 5.

I found this out in a couple of minutes using the debugger that comes with my compiler. At this point, I would learn how to use your debugger so you can step through the code and see what's going on. The debugger will let you see the contents of all your variables as you go aong.
Dec 26 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Moritz Beller | last post by:
Dear programmers, Have a look at this function to generate random numbers from 1 to count. It simply does not work. Well, actually it compiles silently, but then returns "1" (Compiler: gcc/++...
4
by: Anna Maria | last post by:
Hi at all, please take a look to this code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> </HEAD> <BODY> <SCRIPT LANGUAGE="javascript"...
8
by: mheden | last post by:
Hello all, I'm using an Access 97 front-end with a SQL Server 2000 back-end. One particular bit of code runs a stored procedure to obtain a rowcount from one of the back-end tables. This works...
3
by: Anil | last post by:
hi will the following code work. typedef struct { unsigned char date; unsigned char sortie_num; unsigned int start_address; unsigned short offset; } sortie_data;
7
by: Peter Steele | last post by:
I have code to add a domain user to a local group but I'm not sure if it will work with NT domains or whether it will only work with Active Directory based systems. Here's the code: public void...
9
by: bhavik.patel | last post by:
Hi I have a rather simple question: I have following class definitions: class Base { public: virtual void display ()
3
by: xmail123 | last post by:
Why does this code work? I am new to C# and have been studying this piece of code. It loops through an Adjacency Matrix table to populate a tree view. I have two questions about why this code...
2
by: Geoff Cox | last post by:
Hello, I have 2 lines of code which are different but both work. I refer to the 3rd line with the url in it. In the first case the // are not escaped, in the second they are. Either ...
3
by: =?ISO-8859-1?Q?Arne_Vajh=F8j?= | last post by:
Russell Mangel wrote: You have a MsgFile object and it does not implement INoteItem, so you can not cast to it.
7
by: jharrison | last post by:
I'm finding it hard to make this code work in Python 3.0. Been looking at it for some time now: # import module for random functions import random # List of words for the computer to pick...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.