difficulty with converting numbers

I've thrown together the code in my latest post, which is the program for this assignment:


In this project you are going to use single dimensional array and a pointer to implement a stack. A stack is a list which has elements inserted or deleted on the top of the list. It is a first in last out ordering of elements. You will maintain a pointer to the top of the stack that is an array. Write functions for inserting an element (call it push), deleting an element (call it pop), checking if the stack is full ( call it check_full) and checking
if it is empty (call it check_empty). The check_full and check_empty functions are called from within push (before every push operation) and pop (before every pop operation) respectively. If the stack is full or empty, the element cannot be inserted or popped, hence print an error statement and exit the program at that point. Use an array of size 100.
An integer variable p that holds the subscript value of the array is a pointer to stack. The value of p is the location where the next element should be inserted or removed. The value of p, the array itself and the element to be inserted should be passed to the push function. The array and the address of p should be passed to the pop function, which should return the top most element in the stack. Use these functions of a stack to convert numbers from base 10 to base 2(binary), base 8(Octal) or base 16(hexadecimal). Given a number 100, the following process can be used to convert it to base 2. Divide 100 by 2,
push the remainder into the stack, divide the quotient by 2 and push the remainder into the stack and keep doing this(dividing the quotient by 2) till the quotient is 0.
Then pop all elements (print them out when a number is popped from the stack and the number you get is the binary representation of 100. To get the hexadecimal representation of 100, divide the number by 16 till the quotient is 0. For a hexadecimal representation, you should use characters A, B, C, D, E and F for values from 10 through 15 respectively.

The main part of the program should ask for the number and the new base and have a while loop in which you do the mathematics and call the function push. Have another while or for loop to do the pop operations.


I understand it, however my program is not working most likely due to some fault on my part.
Last edited on
Please note, that this is not a homework site. We won't do your homework for you. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attemts to solve this problem youself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.

The assignment text contains a lot of information. I would be surprised if those concepts have not been introduced before the assignment.

In programming you quite often get a detailed problem like this to solve. To solve it you break the whole problem down into smaller chunks and maybe each chunk into yet smaller chunks. You then solve each chunk and when you have solved them all, you step back and low and behold you have solved the whole original problem. If you are unable to do that, then maybe programming isn't for you.
Ok I've cobbled something together, but it doesn't output anything after inputting the base and the number to be converted. I've gone over it and can't see what I'm doing wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
//  main.cpp
//  #3 Memory Stack
//
//
//
//

#include <iostream>
#include <unistd.h>
using namespace std;

bool check_full(int[], int*);
bool check_empty(int stack[], int* p);
void push(int*, int [], int, int*, int);
void pop(int*, int [], int*);


int main()
{
    const int SIZE = 100;
    int stack[SIZE];
    int* p = stack;
    int* top = stack;

    //setting entire stack to null
    for (int setnull = 0; setnull < 99; setnull++)
    {
        *p = NULL;
        p++;
    }
    //return pointer to top of the stack
    p -= 99;
    
    char run;
    int toconversion, base;
    
    do{
    do
    {
        cout << "Do you want to convert a number? (y/n): \n";
        cin >>run;

        
        cout << "What is the base you want to convert to? 2, 8, 16: \n";
        cin >> base;
        cout << "Enter the number to be converted: \n";
        cin >> toconversion;
    }
    while (base != 2 || base !=8 || base != 16);

        if (base == 2)
        {
            cout << "Your decimal number in binary is: \n";
            
            while (toconversion/2 != 0)
            {
                push(p, stack, (toconversion % 2), top, base);
                toconversion /= 2;
                pop(p, stack, top);
            }
        }
        
        if (base == 8)
        {
            cout << "Your decimal number in binary is: \n";
            
            while (toconversion/2 != 0)
            {
                push(p, stack, (toconversion % 2), top, base);
                toconversion /= 2;
                pop(p, stack, top);
            }
        }
        if (base == 16)
        {
            cout << "Your decimal number in hecadecimal is: \n";
            
            while (toconversion/2 != 0)
            {
                push(p, stack, (toconversion % 2), top, base);
                toconversion /= 2;
                pop(p, stack, top);
            }
        }
        
    }
    while (run != 'n');
    return 0;
}

void push(int* p, int stack[], int toconversion, int* top, int base)
{
    if ( check_full(stack, top) == true)
        {
            cout << "error stack is full";
            exit(1);
        }
    *p = toconversion;
    cout << *p;
    if(base == 16 && *p ==10)
    {
        cout << "A";
    }
    else if(base == 16 && *p ==11)
    {
        cout << "B";
    }
    else if(base == 16 && *p ==12)
    {
        cout << "C";
    }
    else if(base == 16 && *p ==13)
    {
        cout << "D";
    }
    else if(base == 16 && *p ==14)
    {
        cout << "E";
    }
    else if(base == 16 && *p ==15)
    {
        cout << "F";
    }
    p++;
}

void pop(int* p, int stack[], int* top)
{
    if ( check_empty(stack, top) == true )
    {
        cout << "error stack is empty";
        exit(1);
    }
    *p = NULL;
    p--;
}

bool check_full(int stack[] , int* p)
{
    bool full;
    for (int count = 0; count < 99; count++)
    {
        p++;
        if (*p == NULL)
        {
            full = false;
        }
        else if (*p != NULL)
        {
            full = true;
        }
    }
    p -= 99;
    return full;
}
bool check_empty(int stack[], int* p)
{
    bool full;
    for (int count = 0; count < 99; count++, p++)
    {
        
        if (*p == NULL)
        {
            full = true;
        }
        else if (*p != NULL)
        {
            full = false;
        }
    }
    p -= 99;
    return full;
}
Last edited on
I've put the modified code in the post above, and I was wondering if anyone could look at it and help me figure out where I went wrong.
One thing is the logic on line 50 - the way it goes now, if I enter a valid number, let's say 8, both the first and third conditions there will be true. Since these are connected by logical ORs, only one of the conditions has to be true for the entire expression to be true. Since all of those conditions need to be true for the while loop to terminate, use a logical AND operator &&.

while (base != 2 || base !=8 || base != 16);

Ok heres the latest revision of code, I made some changes, however it doesn't convert the numbers properly. To my understanding you print number%2, number /= 2 until number/2=0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
//  main.cpp
//  #3 Memory Stack
//
//
//
//

#include <iostream>
#include <unistd.h>
using namespace std;

bool check_full(int[], int*);
bool check_empty(int stack[], int* p);
void push(int*, int [], int, int*, int);
void pop(int*, int [], int*);


int main()
{
    const int SIZE = 100;
    int stack[SIZE];
    int* p = stack;
    int* top = stack;

    //setting entire stack to null
    for (int setnull = 0; setnull < 99; setnull++)
    {
        *p = NULL;
        p++;
    }
    //return pointer to top of the stack
    p -= 99;
    
    char run;
    int toconversion, base;
    
    do{
    do
    {
        cout << "Do you want to convert a number? (y/n): \n";
        cin >>run;

        
        cout << "What is the base you want to convert to? 2, 8, 16: \n";
        cin >> base;
        cout << "Enter the number to be converted: \n";
        cin >> toconversion;
    }
    while (base != 2 && base !=8 && base != 16);

        if (base == 2)
        {
            cout << "Your decimal number in binary is: \n";
            
            while (toconversion/2 != 0)
            {
                push(p, stack, toconversion, top, base);
                toconversion /= 2;
                pop(p, stack, top);
            }
        }
        
        if (base == 8)
        {
            cout << "Your decimal number in binary is: \n";
            
            while (toconversion/2 != 0)
            {
                push(p, stack, (toconversion % 2), top, base);
                toconversion /= 2;
                pop(p, stack, top);
            }
        }
        if (base == 16)
        {
            cout << "Your decimal number in hecadecimal is: \n";
            
            while (toconversion/2 != 0)
            {
                push(p, stack, (toconversion % 2), top, base);
                toconversion /= 2;
                pop(p, stack, top);
            }
        }
        
    }
    while (run != 'n');
    return 0;
}

void push(int* p, int stack[], int toconversion, int* top, int base)
{
    if ( check_full(stack, top) == true)
        {
            cout << "error stack is full";
            exit(1);
        }
    toconversion %= base;
    *p = toconversion;
    cout << *p;
    if(base == 16 && *p ==10)
    {
        cout << "A";
    }
    else if(base == 16 && *p ==11)
    {
        cout << "B";
    }
    else if(base == 16 && *p ==12)
    {
        cout << "C";
    }
    else if(base == 16 && *p ==13)
    {
        cout << "D";
    }
    else if(base == 16 && *p ==14)
    {
        cout << "E";
    }
    else if(base == 16 && *p ==15)
    {
        cout << "F";
    }
    p++;
}

void pop(int* p, int stack[], int* top)
{
    if ( check_empty(stack, top) == true )
    {
        cout << "error stack is empty";
        exit(1);
    }
    *p = NULL;
    p--;
}

bool check_full(int stack[] , int* p)
{
    bool full;
    for (int count = 0; count < 99; count++)
    {
        p++;
        if (*p == NULL)
        {
            full = false;
        }
        else if (*p != NULL)
        {
            full = true;
        }
    }
    p -= 99;
    return full;
}
bool check_empty(int stack[], int* p)
{
    bool full;
    for (int count = 0; count < 99; count++, p++)
    {
        
        if (*p == NULL)
        {
            full = false;
        }
        else if (*p != NULL)
        {
            full = true;
        }
    }
    p -= 99;
    return full;
}
Last edited on
Modding and dividing by 2 is for converting decimal to binary.

It appears your check_full function is returning true on the first number that tries to be converted, so the error message on line 96 gets triggered.

Topic archived. No new replies allowed.