Stacks with Chars
Hello, I cant seem to get my code to work. Any advice?
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
|
#include<iostream>
using namespace std;
class stack
{
private:
char a[80];
int counter;
public:
void clearStack(){counter = 0;}
bool emptyStack(){if(counter = 0) return true; else return false;}
bool fullStack(){if(counter = 80) return true; else return false;}
void pushStack(char x){a[counter] = x; counter++;}
char popStack(){counter--; return a[counter];}
};
bool isvowel(char x)
{
x = toupper(x);
if(x == 'A' ||
x == 'E' ||
x == 'I' ||
x == 'O' ||
x == 'U') return true; else return false;
}
int main()
{
stack UPP, LOW, VOW;
char c;
cout << "Enter a sentence: ";
while(cin.get(c))
{
if(isupper(c)) UPP.pushStack(c);
if(islower(c)) LOW.pushStack(c);
if(isvowel(c)) VOW.pushStack(c);
}
cout << "Uppercase letters: ";
while(!UPP.emptyStack())
{
char y = UPP.popStack();
cout << y;
}
cout << endl;
cout << "Lowercase letters: ";
while(!LOW.emptyStack())
{
char y = LOW.popStack();
cout << y;
}
cout << "Vowels; ";
while(!VOW.emptyStack())
{
char y = VOW.popStack();
cout << y;
}
}
|
> I cant seem to get my code to work
you need to be more descriptive
1 2
|
bool emptyStack(){if(counter = 0) return true; else return false;}
bool fullStack(){if(counter = 80) return true; else return false;}
|
= is assignment
== is comparison
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
|
#include <iostream>
using namespace std;
class stack {
// NOTE(mbozzi): A constant number max_size is used to avoid writing 80
// everywhere.
static int const max_size = 80;
char a[ max_size ]{};
// `counter' always contains the index of the next element
int counter = 0;
public:
void clear() { counter = 0; }
// two equals signs are required to test for equality.
bool empty() const { return counter == 0; }
bool full() const { return counter == max_size; }
void push(char x) { a[ counter++ ] = x; }
char pop() { return a[ --counter ]; }
};
bool isvowel(char x) {
x = toupper(x);
return x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U';
}
void display_stack(stack s) {
while (!s.empty())
std::cout << s.pop() << " ";
std::cout << '\n';
}
int main() {
stack upper, lower, vowels;
cout << "Enter a sentence: ";
for (char c; cin.get(c); ) {
if (isupper(c)) upper.push(c);
if (islower(c)) lower.push(c);
if (isvowel(c)) vowels.push(c);
}
cout << "Uppercase letters: ";
display_stack(upper);
cout << "Lowercase letters: ";
display_stack(lower);
cout << "Vowels: ";
display_stack(vowels);
}
|
Last edited on
Topic archived. No new replies allowed.