title problem

okay so I know that the function system("TITLE..."); can work for adding a title text to a program, but what if I want to display a variable in the title also, such as the user's name or something they searched for.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    string username;
    cout << "Input your user name: ";
    getline(cin, username);
    system(("title "+username).c_str());
    return 0;
}


but please read this article why system() is evil
http://cplusplus.com/forum/articles/11153/
I know of a way to do this, but I refuse to reply on the grounds that this is wrong.

EDIT: Damn.
Last edited on
is there a way to also include other variables (integers, etc...) and strings in addition to the username?
Use a stringstream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
using namespace std;

#include <windows.h>

int main()
  {
  ostringstream ss;
  ss << "Hello. The answer is: " << 42 << ".";
  SetConsoleTitle( ss.str().c_str() );

  cout << "Press ENTER to quit." << flush;
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
  return 0;
  }

Sorry helios. It isn't always wrong. (But it usually is.)
Well, you didn't really use system(), so I don't mind.
is there a way to do that using system()?
Yeah but in light of these obviously superior ways to do it, and just as os-dependent as system, why would you ask?
@helios
Oops! I thought you meant that setting the console title was morally reprehensible. (heh)

[Seeing as system() is (almost) always evil.]

@programmatic
What!? Don't go backwards.

Good luck!
thanks for the tip about the stringstream, but when i try to put it into the program, i get this error:
error C2664: 'SetConsoleTitleW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

i put in this code for the stringstream, for any corrections:
1
2
3
        ostringstream title;
	title << "titlehere";
	SetConsoleTitle(title.str().c_str());
Change the 'o' in 'ostringstream' to a 'w' and add an L before each string literal (L"title").
Either that, or #undef UNICODE before #include <windows.h>
it works great. thanks so much for helping me with this. now i guess that since i started getting rid of the system() function, are there alternatives for system("cls") and system("pause")?
i was just testing out the program and i have one more little problem. if i want the title to change in the middle of the program, it just adds on to the previous title. is there a way to clear the stringstream?
are there alternatives for system("cls") and system("pause")?
Duoas' example already contains the pause thing: http://www.cplusplus.com/forum/beginner/17947/#msg90988
http://www.cplusplus.com/forum/articles/7312/
http://www.cplusplus.com/forum/articles/10515/
is there a way to clear the stringstream?
Why don't you create a new stringstream object?
thank you. you're a big help
I think you can use the following way to display variable in title:
Username:
system("title %username%");
I think you can use the following way to display variable in title:
Username:
system("title %username%");


Do not use system unless you really have to. Use any other possible way.
Topic archived. No new replies allowed.