Error involving invalid operands of a const char* [Solved] [code up for critique?]
Here's the error, it's on line 16:
Quote:
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.
Re: Error involving invalid operands of a const char*
The problem is that you are adding a string literal to an int here:
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>
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.
Re: Error involving invalid operands of a const char*
Quote:
Originally Posted by
BleaS
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.
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;
}
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.
Re: Error involving invalid operands of a const char*
Quote:
Originally Posted by
Russco
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.
Re: Error involving invalid operands of a const char*
Quote:
Originally Posted by
BleaS
I'm actually having a terrible time using the debugger. I can't figure it out at all.
Which debugger?
Quote:
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
Re: Error involving invalid operands of a const char*
I use DevC++ so whatever debugger they have?
Re: Error involving invalid operands of a const char*
Quote:
Originally Posted by
BleaS
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