-4

Peeps. I'm a noob. I have a question to ask but here's my code below.

int main() {
int a, c, favNum;
favNum = 3;
cout << "Hello! Please enter a number in the space below. This number will be called 'a'. " << endl;
cout << "Enter:  " << endl;
cin >> a;


if ( typeid(a) == typeid(int) && cin.fail()==false){
    cout << "Great Job!" << endl;
    cout << "Let's do cool stuff with this program! " << endl;
   * cout << "Type '1' for checking whether your number, " + a + ", is a 
    number divisible by," + favNum + "." << endl;
    cout << "Type '2' to recieve a compliment " << endl;
    cin >> c;
 switch (c) {
    case 1:
       cout << "Awesome!" << endl;
       if( a%favNum == 0 ){
      *   cout << "Your 'a' number, " +a+ ", is a number divisible by 
         'favNum'," +favNum+ "." << endl;
       } else if (a%favNum != 0){
      *   cout << "Your 'a' number, " +a+ ", is a number not divisible by 
         'favNum'," +favNum+ "." << endl;
       }
       break();

   case 2:
    cout << "Great Job!" << endl;

    }

    } else {
      cout << "Oops.. Might wanna try to write a number. Doesn't look like a 
      number to me.." << endl;
      cout << "Please restart your program and follow instructions next 
    time." 
     << endl;
}

Basically what my program does is it takes an input of a number 'a', and checks if it's a number and if it is, it uses "switch case" to direct me to 2 options.

Problem: It shows me this weird error "error: invalid operands of types 'const char' and 'const char [28]' to binary 'operator+'" and I have no idea what it means. It popped me this error on lines 16,23,and 25, and I asterisked the lines so you can see where the error shows up. Thanks if you can help me!

EDIT: My question WAS what is the error? and why my program is not working? I realized that I had done a + b instead of a << "+" << b. I've been working on 2-3 languages in coding, and I made an error. Just 1 day ago, I even said my question was answered. Thank you.

BTW: Program worked just fine before switch case! LOL EDIT: Question solved.

EmCode
  • 17
  • 7
  • 3
    It's not a" weird error", it's very basic C++ - you can't add arrays. Wherever you are learning this nonsense from, learn it from somewhere else, like from a good C++ textbook. –  Feb 18 '18 at 00:19
  • 2
    Why don't you write `<<` instead of `+` in your `cout` statements? – Al Kepp Feb 18 '18 at 00:22
  • 3
    Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. – Ron Feb 18 '18 at 00:26
  • 4
    *"BTW: Program worked just fine before switch case! LOL"* when this happens, go back to the before case and start making changes one at a time to see what you changed that broke the program. If you have a large number of changes, sometimes it is more effective to perform a "binary search" for the error. Add half of the new code. If you get the bug, remove half of the added code. Keep adding or subtracting less and less code until you have isolated the problem. It's better if you make as few changes as possible between compiling and testing though. – user4581301 Feb 18 '18 at 00:34
  • Possible duplicate of [How to concatenate two strings in C++?](https://stackoverflow.com/questions/15319859/how-to-concatenate-two-strings-in-c) – Konrad Borowski Feb 18 '18 at 00:41
  • @user4581301 I'd upvote twice if I could. Excellent advice. – Jules Dupont Feb 18 '18 at 00:54
  • I agree with all, sorry about my silly mistake. – EmCode Feb 18 '18 at 00:58
  • Nobody is going to say anything about typeid(a) == typeid(int) ? – Andrei Damian Feb 18 '18 at 11:09

1 Answers1

2

The line

cout << "Type '1' for checking whether your number, " + a +
        ", is a number divisible by," + favNum + "." << endl;

Does not work since:

"Type '1' for checking whether your number, " + a does not do what you are hoping it will do.

That line is equivalent to:

const char* cp1 = "Type '1' for checking whether your number, ";
const char* cp2 = cp1 + a; // Depending on the value of a, cp2 points
                           // to something in the middle of cp1 or something
                           // beyond.
cout << cp2 + ", is a number divisible by," + favNum + "." << endl;

That is a problem since the plus operator is not defined for the type of cp2 and the string literal that follows the + operator. The error message from the compiler refers to that term.

Type of cp2 is const const*.
Type of the string literal is const char[28].

You can get what you want by using the insertion operator (<<) repeatedly.

cout
   << "Type '1' for checking whether your number, "
   << a
   << ", is a number divisible by,"
   << favNum 
   << "."
   << endl;

Make sure to make similar changes to other lines that suffer from the same problem.

R Sahu
  • 204,454
  • 14
  • 159
  • 270