deskcalc.cpp???

This is a discussion on deskcalc.cpp??? within the C++ Programming forums, part of the General Programming Boards category; hi guys, i have been trying to make this thing work .heres the code for calculator by the way. Everything ...

  1. #1
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    Unhappy deskcalc.cpp???

    hi guys,
    i have been trying to make this thing work .heres the code for calculator by the way. Everything compiles fine except for push_back(). heres the code snippet.
    Code:
    
     Token_value get_token()
    {
    char ch ;
       do{
        if(!cin.get(ch)) return curr_tok = END;
         }while(ch !=( '/n' && isspace(ch)) ) ;
    
      switch(ch){
      case ';' :
      case '\n' :
               return curr_tok = PRINT ;
    
    default : // NAME, NAME = , or error
    
     if( isalpha(ch)){
            string_value =  ch ;
           while( cin.get(ch) && isalnum (ch)) string_value.push_back(ch) ;//HERES THE ERROR. 
            cin.putback(ch);
            return  curr_tok = NAME ;
            }
        error("bad token");
        return curr_tok = PRINT ;
     }
    }
    
    THE COMPILER ERROR:->
    53 deskcalc.cpp no matching function for call to `basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >::push_back (char &)'
    push_back does take char . doesnt it?? if theres any mistake then please let me know. Atleast i will be able to learn from my mistake.
    iostream , cctype, string, map has already been included.Map is needed to create a table of strings.
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  2. #2
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    hey hey...

    -> means "p"
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    What's string_value declared as?

  4. #4
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77
    a string
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Well what do you expect push_back to do? It's not a method of string. Do you want ch appended onto the end of string_value? Do: string_value += ch;

  6. #6
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    ok heres it.

    heres the code (full)

    Code:
    // deskcalc.cpp by "B. Stroustrup"
    
    #include <iostream>
    #include <cctype>
    #include <map>
    #include <string>
    
    
    using namespace std;
    
    
    
    double expr(bool);
    double number_value ;
    string string_value;
    double term(bool);
    double prim(bool);
    map<string,double>table ;
    
    
     enum Token_value {
               NAME        ,  NUMBER ,  END ,
               PLUS = '+' , MINUS = '-', MUL = '*' ,DIV = '/' ,
               PRINT = ';' , ASSIGN = '=' ,LP = '(' , RP = ')'
     };
    
     Token_value curr_tok = PRINT ;
    
     int no_of_errors;
    
    double error(const string& s)
     {
     no_of_errors++ ;
     cerr<< "error: "<< s << '/n' ;
     return 1 ;
     }
     Token_value get_token()
    {
    char ch ;
       do{
        if(!cin.get(ch)) return curr_tok = END;
         }while(ch !=( '/n' && isspace(ch)) ) ;
    
      switch(ch){
      case ';' :
      case '\n' :
               return curr_tok = PRINT ;
    
    default : // NAME, NAME = , or error
    
     if( isalpha(ch)){
            string_value =  ch ;
            while( cin.get(ch) && isalnum (ch))  
           string_value.push_back(ch) ; 
                     // ch would be pushed in  string_value . isnt it???
            cin.putback(ch);
            return  curr_tok = NAME ;
            }
        error("bad token");
        return curr_tok = PRINT ;
     }
    }
    
    
    
    
     double expr(bool get) // add and subtract
     {
    
      double left = term (get) ;
      for(; ;)
             switch (curr_tok) {
       case PLUS :
                left += term(true);
                break;
       case MINUS:
                left -= term(true);
                break;
       default :
                return left ;
       }
    }
    
    
    
    double term(bool get)
    {
     double left = prim(get);
       for(; ;)
        switch(curr_tok){
        case MUL:
                 left *= prim(true);
                 break;
        case DIV:
                if(double d = prim(true) ){
                      left /= d;
                      break;
                }
    
                return (error("divide by 0"));
    
        default:
                return left;
           }
    
    }
    
    
    double prim(bool get)
    {
          if(get)  get_token();
    
          switch(curr_tok) {
          case NUMBER : //floating point constant
          {
           double v = number_value ;
           get_token();
           return v ;
          }
          case NAME :
          {
           double& v = table[string_value];
           if(get_token() == ASSIGN ) v = expr(true);
           return v;
          }
          case MINUS: // unary minus
          {return -prim(true);}
          case LP:
          {double e = expr(true);
           if(curr_tok != RP) return error("')' expected ") ;
            get_token();
            return e ;
            }
          default :
            return error("primary expected");
    
          }
    }
    
    
     int main()
     {
     table["pi"] = 3.14159 ;
     table["e"] = 2.7182 ;
     while(cin)
      {
      get_token();
      if(curr_tok == END ) break ;
      if(curr_tok == PRINT ) continue ;
      cout << expr(false) <<'\n';
       }
      return no_of_errors ;
     }

    i tried your way Eibro but it isnt working. ~:- (

    is ";"

    I think B. stroustrup didnt see his mistakes. Isn't it Eibro?
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    You might want to invest in learning how to click the box that says 'disable smilies in this post'

  8. #8
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    It works fine for me when I change the direction of the newline characters here
    Code:
    cerr<< "error: "<< s << '/n' ;
    and here
    Code:
    }while(ch !=( '/n' && isspace(ch)) ) ;
    Don't forget that the newline character is a backslash followed by lower case n: '\n' :-)

    >>Well what do you expect push_back to do? It's not a method of string.
    Since when? The standard shows it right between the append and assign methods at the end of page 385. :-)
    Code:
    void push_back(const charT);
    *Cela*

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21