Looking for Assistance on a Program - Stack

I have written a program that asks a user to input a string of digits, accepting a decimal as well, and separates the digits into different stacks. My professor has asked that I edit the program to accept any number of digits so it needs to be modified to dynamically stack the string that is entered. I am unsure how to do this currently and any assistance will be greatly appreciated.

PS: This is my first post so any general formalities regarding the code and future posts will also be appreciated. It does not appear that my spacing in the code is being saved when I copy/paste it over and look at the preview so apologies for that.

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
#include <iostream>
#include <string.h>

using namespace std;
const int MAXSTACK=5;
const int TRUE=100;
const int FALSE=0;
class Stack
{
private:
		char num[MAXSTACK];
public:
	Stack();
	void push(char);
	char pop();
	char isempty();
	char isfull();
	char sort();
	int top;
};

Stack::Stack()
{
	top = -1;
}

void Stack::push(string value)
{
	top++;
	num[top] = value;
}

char Stack::pop()
{
	char topval;
	topval=num[top];
	top--;
	return(topval);
}

char Stack::isempty()
{
	if(top==-1)
		return TRUE;
	else
		return FALSE;
}

char Stack::isfull()
{
	if(top==MAXSTACK-1)
		return TRUE;
	else
		return FALSE;
}

int main()
{
	Stack digits;
	string newnum;
	int s = newnum[0]-'0';
	cout<<"Please enter a string of digits (decimal included): "<< endl;
		while(1000)
	{
		cin>>newnum;
		if (newnum == 'x')
			break;
		if(digits.isfull())
		{
			break;
		}
		else
			digits.push(newnum);
	}
	cout<<endl;

	cout<<"The numbers currently stored in the stack are: "<< endl;
	while(!digits.isempty())
	{
		for(int x = 0; x < MAXSTACK; x++)
		{
			char popval = digits.pop();
			cout<<popval<<endl;
		}
		cout<<endl;
	}
	system ("pause");
	return 0;
}
Last edited on
If you edit your post, highlight the code part, then click the <> button in the format palette on the right side of the post, that will format the code for the forum. That'll make it easier to read :)
That was simple enough. Thank you for that.
Instead of a fixed size array, you might consider using a vector.
http://www.cplusplus.com/reference/vector/vector/


Edit: When I had this assignment to do, we had to build our own array class that would dynamically grow if necessary - not sure if your professor is expecting you to do that instead of use vector.
Last edited on
Unfortunately I can not use a vector due to the specifics on the assignment. I'm sitting at an 80 right now due to the program working as needed, however the 20 points I'm missing is because of the 5 digit maxstack and he requested it to be modified to allow for any # of digits specified by the user. I have searched all over for a way to do this and toyed around with modifying the program many times but can't seem to make it work dynamically.
I guess you could determine the length of the number the user enters, then use that number to dynamically allocate the array in the class. You could implement a constructor that would take the number as an integer and use new to allocate an array of that size. You'd also create a destructor for the class to delete the dynamically allocated memory.

http://www.cplusplus.com/doc/tutorial/dynamic/
You have lots of compile errors in your code.

Line 2: Correct header is <string>, not <string.h>. If your cmpiler doesn;t accept <string>, it is incredibly ancient.

Line 6,7: No need to define your own values for true and false. Use the built-in bool type which has true and false.

Line 27: You have no declaration in your class for push.

Line 31: You can't assign a string to a char. You probably want value[0].

Lines 42,50: These function should be type bool

Line 62: newnum is empty. Referring to newnum[0] is invalid.

Line 67: You're comparing a string to a character. You probably want (newnum == "x")
Topic archived. No new replies allowed.