Problem adding words to a stack
Apr 28, 2012 at 9:21pm UTC
So far i have
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
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<math.h>
using namespace std;
void displayMenu(); //menu
void push(char ); //push (option 1)
void pop(); //pop (option 2)
void display(char ); //display (option 3)
void exit(); //exit (option 4)
bool isEmpty(); //funtion to check if stack is empty
int * stack = NULL;
int top = 0;
int size = 0;
void displayMenu() //the menu that is displayed
{
cout << "\nEnter either a 1, 2, 3, or 4: " << endl << endl;
cout << "1. Push <string>" << endl;
cout << "2. Pop" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl << endl;
}//end displayMenu
void push(char * word)
{
stack[top] = *word; //PROBLEM HERE <--
size=size+1;
cout << "pushed '" << word << "'" << endl;
}
void display()
{
if (isEmpty()) //runs function (isempty) to see if stack is empty
cout << "\nThe stack is empty. There is nothing to print." << endl;
else
{
cout << "\nThe stack contains: " << endl;
cout << *(stack) << endl;
}
}
void pop()
{
}
bool isEmpty() //function to check if stack is empty
{
if (size == 0)
return true ; //true if empty
else
return false ; //false if not empty
}//end isEmpty
int main() //start of main function
{
char choice;
int number;
cout << "Welcome to the CSC 130 Dynamic Stack Program!" << endl;
cout << "Make a choice from one of the 4 options below." << endl;
do
{
displayMenu();
cin.get(choice);
switch (choice)
{
case '1' :
cout << "\nEnter the word you would like to push: " ;
char word[80];
cin >> word;
push(word);
break ;
case '2' :
pop(); //runs function pop
break ;
case '3' :
display(); //runs function display
break ;
case '4' :
exit(0); //terminates program
default :
cout << "\nYou entered an invalid choice. Try again." << endl;
}//end switch
cin.ignore();
}while (true );
// system("pause");
return 0;
}//end main
the problem is I dont know how to add words to my array, it might be something silly but i dont recall what I am suppose to do here
Apr 28, 2012 at 9:39pm UTC
stack[top] = *word; //PROBLEM HERE <--
#1 As
word
is a char*, dereferencing it's given you a char; the first char in your string.
#2 Your
stack
(pointer) is (a) pointing nowhere (so there's no memory to store your string) and is (b) the wrong data type (you are wanting to store words??)
I would consider using an array of char buffers for your stack to start with, and worry about dynamic memory allocation later on, if needed.
I can also see a number of other issues, including function signatures, confusion between top/size, etc
Also, you should try to avoid using exit() to quit a program. I would expect its use to limited to error handling. Try using a loop control variable instead and have the exit case change its value to exit the loop.
Last edited on Apr 29, 2012 at 1:38pm UTC
Apr 28, 2012 at 11:30pm UTC
ok i changed it around to use a string but for some reason program skips the problem line completely and I dont understand why
heres the new code
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
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <math.h>
#include <string>
using namespace std;
void displayMenu(); //menu
void push(char ); //push (option 1)
void pop(); //pop (option 2)
void display(char ); //display (option 3)
void exit(); //exit (option 4)
bool isEmpty(); //funtion to check if stack is empty
string sk;
int size = 0;
void displayMenu() //the menu that is displayed
{
cout << "\nEnter either a 1, 2, 3, or 4: " << endl << endl;
cout << "1. Push <string>" << endl;
cout << "2. Pop" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl << endl;
}//end displayMenu
void push()
{
cout << "\nEnter the word you would like to push: " ;
getline (cin,sk); //PROBLEM HERE <---
size=size+1;
cout << "Pushed '" << sk << "'" << endl;
}
void display()
{
if (isEmpty()) //runs function (isempty) to see if stack is empty
cout << "\nThe stack is empty. There is nothing to print." << endl;
else
{
cout << "\nThe stack contains: " << endl;
cout << sk << endl;
}
}
void pop()
{
}
bool isEmpty() //function to check if stack is empty
{
if (size == 0)
return true ; //true if empty
else
return false ; //false if not empty
}//end isEmpty
int main() //start of main function
{
char choice;
int number;
cout << "Welcome to the CSC 130 Dynamic Stack Program!" << endl;
cout << "Make a choice from one of the 4 options below." << endl;
do
{
displayMenu();
cin.get(choice);
switch (choice)
{
case '1' :
push();
break ;
case '2' :
pop(); //runs function pop
break ;
case '3' :
display(); //runs function display
break ;
case '4' :
exit(0); //terminates program
default :
cout << "\nYou entered an invalid choice. Try again." << endl;
}//end switch
cin.ignore();
}while (true );
// system("pause");
return 0;
}//end main
and i know i should try to aviod using exit() but for this program I dont think it matters
Last edited on Apr 29, 2012 at 12:07am UTC
Apr 29, 2012 at 1:39pm UTC
Now you're just providing space for a single string. Just enough for a "stack" of one string?
Last edited on Apr 29, 2012 at 1:39pm UTC
Apr 29, 2012 at 4:36pm UTC
what about:
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
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <math.h>
#include <string>
using namespace std;
void displayMenu(); //menu
void push(char ); //push (option 1)
void pop(); //pop (option 2)
void display(char ); //display (option 3)
void exit(); //exit (option 4)
bool isEmpty(); //funtion to check if stack is empty
string sk;
int size = 0;
string* stack=new string[1];
void displayMenu() //the menu that is displayed
{
cout << "\nEnter either a 1, 2, 3, or 4: " << endl << endl;
cout << "1. Push <string>" << endl;
cout << "2. Pop" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl << endl;
}//end displayMenu
void push()
{
cout << "\nEnter the word you would like to push: " ;
getline (cin,sk); //PROBLEM HERE <---
stack[size]=sk;
++size;
stack=new (stack) string[size];
cout << "Pushed '" << sk << "'" << endl;
}
void display()
{
if (isEmpty()) //runs function (isempty) to see if stack is empty
cout << "\nThe stack is empty. There is nothing to print." << endl;
else
{
cout << "\nThe stack contains: " << endl;
cout << sk << endl;
}
}
void pop()
{
--size;
cout<<"popped '" <<stack[size]<<"'" ;
stack=new (stack) string[size];
}
bool isEmpty() //function to check if stack is empty
{
if (size == 0)
return true ; //true if empty
else
return false ; //false if not empty
}//end isEmpty
int main() //start of main function
{
char choice;
int number;
cout << "Welcome to the CSC 130 Dynamic Stack Program!" << endl;
cout << "Make a choice from one of the 4 options below." << endl;
do
{
displayMenu();
cin.get(choice);
switch (choice)
{
case '1' :
push();
break ;
case '2' :
pop(); //runs function pop
break ;
case '3' :
display(); //runs function display
break ;
case '4' :
exit(0); //terminates program
default :
cout << "\nYou entered an invalid choice. Try again." << endl;
}//end switch
cin.ignore();
}while (true );
// system("pause");
return 0;
}//end main
?
Apr 29, 2012 at 6:17pm UTC
;-)
This is worse than your first version.
Rather than abusing placement new, I would try using an array for your stack.
Apr 29, 2012 at 6:22pm UTC
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
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <math.h>
#include <string>
using namespace std;
void displayMenu(); //menu
void push(char ); //push (option 1)
void pop(); //pop (option 2)
void display(char ); //display (option 3)
void exit(); //exit (option 4)
bool isEmpty(); //funtion to check if stack is empty
string sk;
int size = 0;
const in maxsize=100;
string stack[maxsize];
void displayMenu() //the menu that is displayed
{
cout << "\nEnter either a 1, 2, 3, or 4: " << endl << endl;
cout << "1. Push <string>" << endl;
cout << "2. Pop" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl << endl;
}//end displayMenu
void push()
{
cout << "\nEnter the word you would like to push: " ;
getline (cin,sk); //PROBLEM HERE <---
stack[size]=sk;
++size;
cout << "Pushed '" << sk << "'" << endl;
}
void display()
{
if (isEmpty()) //runs function (isempty) to see if stack is empty
cout << "\nThe stack is empty. There is nothing to print." << endl;
else
{
cout << "\nThe stack contains: " << endl;
cout << sk << endl;
}
}
void pop()
{
delete stack+size;
--size;
cout<<"popped '" <<stack[size]<<"'" ;
}
bool isEmpty() //function to check if stack is empty
{
if (size == 0)
return true ; //true if empty
else
return false ; //false if not empty
}//end isEmpty
int main() //start of main function
{
char choice;
int number;
cout << "Welcome to the CSC 130 Dynamic Stack Program!" << endl;
cout << "Make a choice from one of the 4 options below." << endl;
do
{
displayMenu();
cin.get(choice);
switch (choice)
{
case '1' :
push();
break ;
case '2' :
pop(); //runs function pop
break ;
case '3' :
display(); //runs function display
break ;
case '4' :
exit(0); //terminates program
default :
cout << "\nYou entered an invalid choice. Try again." << endl;
}//end switch
cin.ignore();
}while (true );
// system("pause");
return 0;
}//end main
better?
Topic archived. No new replies allowed.