CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2009
    Posts
    135

    Error involving invalid operands of a const char* [Solved] [code up for critique?]

    Here's the error, it's on line 16:
    16 [censored] Menu 2.0.cpp invalid operands of types `const char*' and `const char[15]' to binary `operator+'
    Here's the code:

    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;     using std::cin;
    using std::string;   using std::endl;
    
    int main()
    {
        const int _num_items = 5;
        
        int row = _num_items + 2;
        string::size_type cols;
        
        for (int r=0; r != row; ++r)
         {
            string _menu_option;
            
            if (r != 0)
             {
               _menu_option = "| " + r + ". Menu Option " + r + " |";
               cols = _menu_option.size();
             }
             
             int c = 0;
             
             while (c != cols)
              {
                   if (r = 0 ||  r = row)
                    {
                      if (c = 0 || c = cols-1)
                       {
                        cout << "|";
                       }
                      else
                       {
                        cout << "-";
                       }
                    }
                   else
                    {
                        cout << _menu_option;
                        c += _menu_option.size();
                    }
              }
              cout << endl;
         }
        
        system("PAUSE");
        return 0;
    }
    I know what a * means, and what a char is, and what defining it as a const means. However, I don't see how that is relevant to a non-constant string that I defined. It's looking as if it doesn't like the + operands in the string, but that's silly since they should work. I'm not sure what's wrong.

    I understand that a C++ string is actually just a character array, so I suppose that's why it says character instead of string. But I still don't get why it's failing.
    Last edited by BleaS; February 11th, 2009 at 02:10 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Error involving invalid operands of a const char*

    The problem is that you are adding a string literal to an int here:
    Code:
    "| " + r
    The result is a pointer to const char. Now, you attempt to add a string literal to that const char*:
    Code:
    "| " + r + ". Menu Option "
    One way to do what you want to do is via a stringstream, e.g.,
    Code:
    std::stringstream ss;
    ss << "| " << r << ". Menu Option " << r << " |";
    _menu_option = ss.str();
    You would need to #include <sstream>
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2009
    Posts
    135

    Re: Error involving invalid operands of a const char*

    Ah, it was my assumption that integers could be written into a string literal as is. Thank you, and I will keep that in mind for the future.

    Edit:

    Also, yes, I forgot to do == rather than =, just caught that.

    Edit 2:

    Here's the "finished" code, finished the menu display, now I need to offer some other things.
    Last edited by BleaS; February 11th, 2009 at 01:37 PM.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Error involving invalid operands of a const char*

    Quote Originally Posted by BleaS View Post
    Ah, it was my assumption that integers could be written into a string literal as is.
    Only in Java, unfortunately. That's one thing it'd be nice if C++ got better about.

  5. #5
    Join Date
    Feb 2009
    Posts
    135

    Re: Error involving invalid operands of a const char*

    I suppose I can post up the finished code for critique. However, as I'm following the Accelerated C++ book, I would like you to keep that in mind if you're familiar with it. Certain things are not as efficient as they could be because of what I "know". The book hasn't gotten to functions or anything else. It has covered basic things like variables and data types, for and while loops, operators, etc... however, as shown by my assumption of numbers, it has not yet covered string streams, etc... so, if you can keep that in mind, I'd like to know what you think about the part of the code whee I added stuff that was different from what is shown in the Accelerated C++ book. This book had the following example program:

    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;   using std::cin;
    using std::endl;   using std::string;
    
    int main ()
    {
        // Ask for the user's name:
        cout << "Please enter your first name: ";
        
        // Initialize the Variable name, and assign it a value
        // from user input
        string name;
        cin >> name;
        
        // Initializing the Variable greeting to store the value
        // of the entire greeting statement
        const string greeting = "Hello, " + name + "!";
        
        // Default padding around the greeting
        const int pad = 1;
        
        // Defining our rows and columns based off of the size of the
        // string and our padding
        const int rows = pad * 2 + 3;
        const string::size_type cols = greeting.size() + pad * 2 + 2;
        
        // Separating input from ou last output by flushing with a
        // blank line
        cout << endl;
        
        // Writing the actual output, including: greeting, border,
        // and any padding. Our invariant: We have written r rows
        // so far
        for (int r = 0; r != rows; ++r)
         {
            string::size_type c = 0;
            
            // invariant: we have written c characters in
            // the current row (aka columns)
            while (c != cols)
                  {
                        // Check to see if this is the proper row
                        // to write the greeting
                        if (r == pad + 1 && c == pad + 1)
                           {
                                 // We're in the proper row for
                                 // the greeting
                                 cout << greeting;
                                 c += greeting.size();
                           }
                        else
                            {
                                 // Not the proper row
                                 // Check to see if we're in a 
                                 // border or padding place
                                 if (r == 0 || r == rows - 1 ||
                                     c == 0 || c == rows - 1)
                                     cout << "*";
                                 else
                                     cout << " ";
                             ++c;    
                            }
                  }
            cout << endl;
            system("PAUSE");
         }
        
        return 0;
    }
    Fairly simple and straightforward.

    Since I was trying to make a menu program before, from my lingering, remnant knowledge. I decided to see if I could use some of the concepts in the simpler program to do that. So, I suppose, that's what I'm asking you to critique. How could I have implemented my basic knowledge more efficiently?

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using std::cout;     using std::cin;
    using std::string;   using std::endl;
    
    int main()
    {
        const int _num_items = 9;
        
        int row = _num_items + 2;
        int r_row;
        string::size_type cols;
        
        string _menu_option;
        
        for (int r=0; r != row; ++r)
         {
            r_row = r;
     
            std::stringstream ss;
            
            ss << "| " << r_row << ". Menu Option " << r_row << " |";
            _menu_option = ss.str();
            cols = _menu_option.size();
             
             int c = 0;
             
             while (c != cols)
              {
                   if (r_row != 0 && r_row != row-1)
                    {
                      cout << _menu_option;
                      c += _menu_option.size();
                    }
                   else
                    {
                      cout << "-";
                      ++c;
                    }
              }
              
              cout << endl;
         }
        
        system("PAUSE");
        return 0;
    }

  6. #6
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Error involving invalid operands of a const char*

    Nows as good a time as any to learn to use the debugger.

    Your last line will print 2 too many '-' chars. Why? How do you fix it?

    Follow your code in a debugger, set breakpoints, add variables to the watch window and work out why this happens and how to effect a fix.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  7. #7
    Join Date
    Feb 2009
    Posts
    135

    Re: Error involving invalid operands of a const char*

    Quote Originally Posted by Russco View Post
    Nows as good a time as any to learn to use the debugger.

    Your last line will print 2 too many '-' chars. Why? How do you fix it?

    Follow your code in a debugger, set breakpoints, add variables to the watch window and work out why this happens and how to effect a fix.
    I'm actually having a terrible time using the debugger. I can't figure it out at all. I usually manually set in variables and pauses in order to debug.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error involving invalid operands of a const char*

    Quote Originally Posted by BleaS View Post
    I'm actually having a terrible time using the debugger. I can't figure it out at all.
    Which debugger?
    I usually manually set in variables and pauses in order to debug.
    That may work for small programs, but for much larger, complex programs, that method will more than likely not be feasible. Using debugging tools that comes with the compiler suite is part and parcel of learning how to program.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Feb 2009
    Posts
    135

    Re: Error involving invalid operands of a const char*

    I use DevC++ so whatever debugger they have?

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error involving invalid operands of a const char*

    Quote Originally Posted by BleaS View Post
    I use DevC++ so whatever debugger they have?
    They use the gdb debugger with a graphical interface. What problems are you having with it? Are you able to set break points? Single step in code? Watch variables? It should be straightforward.

    But the issue really is that DevC++ is a dead project -- it hasn't been updated in years. If anything, invest the time in getting CodeBlocks IDE, which also uses gcc and gdb.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured