Combining strings and numbers

What is wrong with my code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main() {
  int age = 20;
  char name = "Jake";
  
  cout << "Hello " + name + ". You are " + age + " years old.";
  
  float x = 1234;
  float y = 5678;
  float sum = x + y;
  cout << "The sum of " + x + "and " + y + " is" + sum + ".";
}


Here is the Error

exit status 1
main.cpp: In function 'int main()':
main.cpp:6:15: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
char name = "Jake";
^~~~~~
main.cpp:8:27: error: invalid operands of types 'const char*' and 'const char [11]' to binary 'operator+'
cout << "Hello " + name + ". You are " + age + " years old.";
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
main.cpp:13:25: error: invalid operands of types 'const char [12]' and 'float' to binary 'operator+'
cout << "The sum of " + x + "and " + y + " is" + sum + ".";
~~~~~~~~~~~~~~^~~

What am I doing wrong here?
char name
What's this? One char. A single char.

"Jake";
What's this? It's a pointer to an array of char, containing the FOUR chars 'J', 'a', 'k' and 'e', and also a zero on the end to mark the end.


One char. Pointer to four chars. How can you make one char the same as a pointer to four chars?


"Hello " + name
Here, you're trying to use the + operator on a pointer to five chars (left), and a single char (right). There is no such operator. C++ just doesn't know what to do when you try to use + on these types.


Are you coming to this from a different programming language? Why do you think these things should work?
Last edited on
Maybe, I can't remember how this stuff works to well.

I am just simply assigning the value "Jake" to the variable called name with the type as a char.

I'm guessing I should use string as the data type? I can't remember the syntax of char. do you write the size with the char datatype? Like char[256] name = "Jake"?

Why do say this is a pointer? I am not even using a reference or a de-reference operator? "* | &"

I'm guessing now that the + is used to add together the strings.

What are you trying to get me to understand with your questions?
What are you trying to get me to understand with your questions?

That you're guessing how to program in C++. That's not going to work. You'll have to learn it.

I'm guessing I should use string as the data type?
string is the sensible choice for storing strings.


Why do say this is a pointer?
That's how C and C++ work. When you write a string literal into your code ("anything inside quotes like this"), that string literal is of type const char [someNumber] and in typical use that decays to a char pointer at the point of use.

char name = "Jake";
On the left, an object of type char. On the right, a string literal; an object of type const char[5] , which gets treated as a char-pointer to the 'J'.
Your saying that I'm guessing and that is not going to work. I have to learn it.
I am guessing it because It's been a while. I was writing this little bit of code to remember. You are wrong. This is how it works. Through trial and error. That is how "I" learn it. Remember that people have different perspectives on how they learn.

So to be clear, your answer is yes. I would use strings.

Last edited on
Put your ego back in its box.

Trial and error? Sure, that's great.

ngsjkghrjkehkg

Hmm. Didn't compile.

gnrtjk66nhk

That didn't compile either! This trial and error sure is going to take a while.

You throw away huge amounts of your own time guessing when you could spend sixty seconds looking it up. Nobody learns from scratch by guessing. There is literally an infinity of possible combinations of letters and numbers. You'd be here until the sun burned out and the universe grew cold.

You didn't do it by trial and error anyway. You got it wrong, and then you asked someone and got an answer.
Last edited on
Topic archived. No new replies allowed.