I have an error message I can't resolve.

I'm creating a little app to calculate the amount of hours in a given amount of years. I've concatenated the output however I get the following error:

error: invalid operands of types 'const char*' and 'const char [5]' to binary 'operator+'|


This is the line it dislikes:

cout << "There are " + hours + " in " + years + "!" << endl;

And this is the entire code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{

int years;
int hours;

const int oneyear = 24 * 365;

cout << "Please enter the amount of years: ";
cin >> years;

hours = oneyear * years;

cout << "There are " + hours + " in " + years + "!" << endl;

return 0;
}


Help and feedback appreciated :)

Thanks
hi spock.nice to meet u.i repair ur program.i hope this is what u want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27



 #include <iostream>

using namespace std;

int main()
{

int years;
int hours;

const int oneyear = 24 * 365;

cout << "Please enter the amount of years: ";
cin >> years;

hours = oneyear * years;

cout << "There are " << hours << "hours  in " << years << "!" << endl;
system("pause");

return 0;
 
}
When is it best to concatenate rather than using the insertion operator?

Thanks for the help! :)
In the C++ world, you can only concatenate string classes, like std::string. And there is no automatic "to string" for ints, etc.

A literal string is seen as a pointer to an array of chars. And adding an integer to a pointer advances it by sizeof(type). That is ptr + 3 is the same as ptr[3]. So...

cout << "Come in #" + 3 << endl;

doesn't output "Come in #3", it outputs "e in #". As you told the compiler to advance the pointer by 3 chars along the string.

If you want to use concatenation with string literals, you will have to use strcat into a buffer.

If your compiler also support itoa(), you can then do:

1
2
3
4
5
6
7
8
char buffer[256] = {0); // zero buffer
char temp[32] = {0); // for string conversion
strcat(buffer,  "There are ");
strcat(buffer,  itoa(hours, temp, 10));
strcat(buffer,  "hours  in ");
strcat(buffer,  itoa(years, temp, 10));
strcat(buffer,  "!");
cout << buffer<< endl;


But if your compiler doesn't provide itoa, then you need to use sprintf() or ostringstream (preferred in the C++ world). But if you are using an ostream for the conversion, and then writing the final string to cout, another ostream, why not just do as Robertoplusplus suggested. This is the idiomatic C++ way to do thing.

Note that it still doesn't work if the values you are outputting are strings:

1
2
3
4
const char fruit[] = "lemon";
const char fruit_color[] = "yellow";

cout << "The " << fruit << " is a " << fruit_color << " fruit." << endl;


But it you use std::string instead, and avoid trying to join two string literals, then it does work:

1
2
3
4
const string fruit = "lemon";
const string fruit_color = "yellow";

cout << "The " + fruit + " is a " + fruit_color + " fruit." << endl;


But this isn't idomatic C++. The C++ way is to use <<.

cout << "The " << fruit << " is a " << fruit_color << " fruit." << endl;

I use string concatenation (using std::string) when I need to build up a string from substrings for use other than output. If I need to build a string which includes int values, etc then I use ostringstream to build the string using <<, and then the ostringstream::str() to obtain the final string.
Last edited on
adding system("pause); just made the program worse, by the way.
I didn't! :P

What are you meant to use as apose to system("pause")?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{

int years;
int hours;

const int oneyear = 24 * 365;

cout << "Please enter the amount of years: ";
cin >> years;

hours = oneyear * years;

cout << "There are " <<hours<< " hours  in " <<years<< " years!"<< endl;
system("pause");//some compilers would not pause by giving you this sentence Press any key to continue . . . so you have to add just in case!!

return 0;
 
}
You can add cin.get(); to the end of your program (before it terminates of course - ie. just before return 0;). This waits for a user input before it then terminates. However, if the program relies on user input then (for reasons I won't go into) then cin.get(); may not work. You can add another cin.get(); or add cin.ignore(); before it.

Alternatively, you can just run the program from command line. On Windows, you can simply navigate to the correct directory and enter the name of you program.
eg.
C:\Users\Spock>cd desktop
C:\Users\Spock\Desktop>example

This way the output will remain visible once the program has terminated. Make sure you don't use
>start example
as this opens the program in a new window and you will have the same problem as before, with the window closing.

The same is also true for Linux.
eg.
cd spock
./example

Don't take my word on it, but I think the same command also works on a Mac.

I also know that code::blocks adds the run-time at the end of execution, and will not close the window either. I'm sure other IDEs have this feature (DevC++ doesn't though and I don't think Visual C++ does either).
Last edited on
You can use net beans or Xcode. Both have in program running windows. And read the very second post in the beginner section about why system functions are bad...
Last edited on
Topic archived. No new replies allowed.