Beginner Console Title Question
Sorry about the beginner question, but I am sort of stumped.
I have a program that asks the user for the title of the window should be, then adds "title " to the beginning and calls system(UserEnteredTitle); But i get an error message that says error C2110: cannot add two pointers. How can I set a user entered string as the title? Here's the code, I am using Visual C++ on XP:
//test
#include<iostream.h>
int main()
{
char TempTitle[]="";
char Title[]="title ";
cout<<"Enter Title: ";
cin>>TempTitle;
Title="title "+TempTitle;
system(Title);
return(0);
}
And the error:
Compiling...
test2.cpp
C:\Documents and Settings\Me\My Documents\C++ Programs\Test 2\test2.cpp(12) : error C2110: cannot add two pointers
Error executing cl.exe.
[860 byte] By [
autopilot] at [2007-11-19 12:19:07]

# 1 Re: Beginner Console Title Question
Greetings.
Firstly, I assume you've used a language in the past that has string concatenation built in to it. C++ does not. You are literally trying to add two pointers here.
Secondly, your call to system() is strange at best. It makes no sense to me that you would call it with the parameter you are attempting to construct.
What you are attempting to do is not well defined by what you've posted.
Please clarify your requirements.
Regards.
# 2 Re: Beginner Console Title Question
To add two strings use (I've coded this ad-hoc but what you should be looking to do is to use std::string and string::append() to concatenated the string).
//test
#include<iostream>
#include <string>
int main()
{
using namespace std;
cout<<"Enter Title: ";
string TempTitle;
cin>>TempTitle;
system(string("title").append(TempTitle).c_str());
return(0);
}
# 3 Re: Beginner Console Title Question
Thank you, the program works great. Sorry for not replying sooner but my internet stopped working.
# 4 Re: Beginner Console Title Question
There is one more issue with your character array and that's why I would suggest going ahead with std::string as suggested by freddyflintstone. You don't have enough memory space to hold a string into TempTitle that the user would be entering and hence these statements together make a wrong code:char TempTitle[]="";
cin>>TempTitle;Regards.
# 5 Re: Beginner Console Title Question
Naturally, this program is much more of an expirement in strings (char*, std::string, whatev) than the actual setting console title. But, it is good to note that there is most of the time a better solution than system()! In this case, you might enjoy the ::SetConsoleTitle() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/setconsoletitle.asp) API. And if you wanted to stick with a char array like in your first example, instead of a '+' you might use the strcat function to put the user input onto the end. This is just a awkward walkaround from the obvious STL solution:
char Title[1024]="title ";
char TempTitle[1024];
cout << "Enter Title: ";
cin >> TempTitle;
strcat(Title, TempTitle);
system(Title);