I am having an issue with trying to send a char[ ] message from main to my function.
InputNum(char msg []) { cout << msg; }
then called as
int main() { InputNum num ("Enter a Number"); }
The above throws an error from the g++ compiler:
ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
So, I am sending a String in main as a char[ ]. But I’m not sure why that is a problem. I was under the illusion that a char[ ] IS a String, which I guess is incorrect. Any general advice on what and how to fix it, I would appreciate greatly.
Also, code is inspired by p.44 "C++ In Action" by Bartosz Milewski (2001).
>Solution :
You are trying (unknowingly) to make a string literal writable. C++ doesnt like this at all.
You have to do
InputNum(const char msg []) {
cout << msg;
}
this promises the compiler that you wont try to change the conents of ‘msg’