Stack of Strings

Hi there ,Below is my code to declare a Stack that accepts Strings. I will be using this stack to contain operators , numbers and symbols .
I use CodeBlocks 10.05

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef STACK_H
#define STACK_H
#include<string>
#define arrsize 10

class Stack
{
private:
    string arr[arrsize];
    int top ;

public:
    Stack();
    void push(string);
    string pop();
    bool isEmpty();
    bool isFull();
    string peep();
};

#endif // STACK_H 

It gives the following errors:
1. 'string' has not been declared.
2. 'string' does not name a type.

I don't understand that why are these errors coming when I have included the file string . ie #include<string>

Any ideas or suggestions?
I tried " using namespace std ; ", however it is recommended not to use it in header files. So Instead of that I have used std::string which made things alright.

There are some doubts in my mind regarding char and string

I am scanning characters from an char array.
1
2
3
4
5
6
7
8
If (char is a number){
  //push on stack.
}

if(char is operator){
  //pop two top elements from stack and perform the operation on them.
  //then the result is pushed on the top of stack. This process continues till all  //the elements on the char array are scanned.
}


1. Many type conversions are involved in this :
a.) char to string (when the characters are pushed onto stack).
b.) string to float (when numbers are popped and operations are performed on them).
c.) float to string (when the result is pushed back onto stack.)

Here is my 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
94
95
#include <iostream>
#include "stack.h"
#include <cstdlib>
#include <string>
#include <sstream>
#include <list>

float str_to_float(std::string in)
{
    std::ostringstream o(in);
    float f;
    o >> f;
    return f;
}

std::string float_to_str(float f)
{
    std::istringstream i;
    i << f;
    return i.str();
}

bool isOperator(char op)
{
    switch(op)
    {
    case '+':
    case '-':
    case '*':
    case '/':
        return true ;
        break;
    default:
        return false;
    }
}

bool isNumber(char num)
{
    return ( num>='0' && num<='9' );
}

int main()
{

    char post[] = "75-92/*";
    Stack myStack ;
    int count = 0;

    while(post[count] != '\0')
    {
        if(isOperator(post[count]))
        {
            //string to float 
            float ntop = str_to_float(myStack.pop());
            float ntopp = str_to_float(myStack.pop());
            float ans ;

            std::cout<<"Top: "<<ntop<<std::endl ;
            std::cout<<"Top-1: "<<ntopp<<std::endl ;
            switch(post[count])
            {
            case '+':
                ans = (float)ntopp + ntop;
                break;
            case '-':
                ans = (float)ntopp - ntop;
                break;
            case '*':
                ans = (float)ntopp * ntop;
                break;
            case '/':
                ans = (float)ntopp / ntop;
                break;
            default:
                std::cout<<"Invalid op";
                exit(0);
            }

            std::cout<<"Ans: "<<ans<<std::endl<<std::endl;
           //float to string 
            myStack.push(float_to_str(ans));
        }

        if(isNumber( post[count] ))
        {
            //char to string
            std::string mystr = (std::string) post[count];
            myStack.push(mystr);
        }
        count++ ;
    }
    std::cout<<"\nThe value of postfix expression "<<myStack.pop();
    return 0;
}


It gives to conversion errors
Line 86 : invalid conversion from 'char' to 'const char*'
Line 86 : initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'|
Line 12: no match for operator '>>' in 'o>>f'.
Line 19: no match for operator '<<' in 'i<<f'.

I am not sure whether my isNumber() returns true for floats as well. Any ideas abt that ?
I am really fed up after lots of tries. Any suggestions ?
Last edited on
stringstream came to the rescue
Problem solved !!!
Topic archived. No new replies allowed.