I need help setting the console title name

Started by
9 comments, last by SiCrane 16 years, 9 months ago
hello, I am trying to let the user title the dos window to whatever they want, but I cannot figure out how to do this. I know how to to do it ( system("TITLE WHATEVER YOU WANT"); ), but I cannot figure out how to title it without pre naming the window. thanks for ANY help! [Edited by - NUCLEAR RABBIT on July 10, 2007 8:12:46 AM]
Advertisement
Assuming you actually mean a win32 console window, you can use the SetConsoleTitle() function.
Quote:Original post by SiCrane
Assuming you actually mean a win32 console window, you can use the SetConsoleTitle() function.


thank you very much [smile]
it worked fine, but then I tried making the title 2 words and i got an error so I made the title a char[] and then I got an windows error message (not a compile error) and now It happens everytime. please tell me if you see anything wrong with my code. I cant see it [sad]

my class code:
class Wordpad{    char * m_WindowTitle;    std::string m_BodyText;        public:        void GetWindowTitle();        void GetBodyText();        void DisplayBodyText();        void SaveBodyText();        void LoadBodyText();};


in one of my class definition member function
    std::cout << "Please Enter your Project name: ";    std::cin >> m_WindowTitle;        SetConsoleTitle(m_WindowTitle);        system("CLS");
The problem is that you don't allocate the memory for that string, just the pointer.

Try this instead:

char m_WindowTitle[128]; // 128 is the maximum length

You can also use cin.getline() with std::string's to get the entire line and not just the first word. There might be a better way to do it though.
Quote:Original post by DvDmanDT
The problem is that you don't allocate the memory for that string, just the pointer.

Try this instead:

char m_WindowTitle[128]; // 128 is the maximum length

You can also use cin.getline() with std::string's to get the entire line and not just the first word. There might be a better way to do it though.


that didnt work. when i run the program and enter the title name, after I press enter a window pops up and says it has encountered a problem and that its sorry for the inconvenience. I tried a bunch of things to get it back to when it worked, but I cant get it to work. hopefully someone here can see something I cant. I'll post all the code I made.

Wordpad.h
#include <iostream>#include <string>class Wordpad{    char * m_WindowTitle;    std::string m_BodyText;        public:        void GetWindowTitle();        void GetBodyText();        void DisplayBodyText();        void SaveBodyText();        void LoadBodyText();};


Wordpad.cpp
#include "Wordpad.h"#include <windows.h>#include <iostream>void Wordpad::GetWindowTitle(){    std::cout << "Please Enter your Project name: ";    std::cin.ignore();    std::cin >> m_WindowTitle;        SetConsoleTitle(m_WindowTitle);        system("CLS");};void Wordpad::GetBodyText(){    std::cout << "Begin wiriting below:\n\n";    std::cin.ignore();    getline(std::cin, m_BodyText);};void Wordpad::DisplayBodyText(){    system("CLS");        std::cout << m_BodyText;}


main.cpp
#include <iostream>#include "Wordpad.h"int Menu(){    int option;        std::cout << "WORDPAD OPTIONS:\n\n"              << "1 = NEW PROJECT\n"              << "2 = LOAD PROJECT\n"              << "3 = EXIT\n\n"              << "Option: ";    std::cin >> option;        system("CLS");        return option;}int main(){    Wordpad wordpad;    bool quit = false;        do     {        switch( Menu() )        {            case 1: wordpad.GetWindowTitle();                    wordpad.GetBodyText();                    break;            case 2: break;            case 3: quit = true;                    break;            default: std::cout << "invalid Option\n\n"; system("CLS");        }    }while( quit == false );        return 0;}


Thanks for ANY help!

[smile]
Did you actually try the solution DvDmanDT posted? You are still not allocating any memory for the m_WindowTitle variable, at least not in that code.

Failure to properly allocate memory will, in this case, result in an access violation error (0xc0000005 in Win32).

Niko Suni

#include <iostream>#include <string>class Wordpad{    char * m_WindowTitle;    std::string m_BodyText;        public:        void GetWindowTitle();        void GetBodyText();        void DisplayBodyText();        void SaveBodyText();        void LoadBodyText();};


should be

#include <iostream>#include <string>class Wordpad{    char m_WindowTitle[128];    std::string m_BodyText;        public:        void GetWindowTitle();        void GetBodyText();        void DisplayBodyText();        void SaveBodyText();        void LoadBodyText();};


Does it still not work? It should, even if using a std::string with cin.getline is probably a far better way.
hey!

yeah, i forgot to put thr [128] after the name. I put it like this: char[128] m_WindowTitle.

thanks for the help guys [smile]
Quote:Original post by DvDmanDT
*** Source Snippet Removed ***

should be

*** Source Snippet Removed ***

Does it still not work? It should, even if using a std::string with cin.getline is probably a far better way.


i tried making m_WindowTitle a string, but the SetConsoleTitle() function only takes char (thats what dev c++ said) so I had no choice [sad]

I wish I could because I cant make the console title 2 words. I tried with the char m_WindowTitle[128] and it went crazy. haha.

if u can help with this problem too thatd be awesome [smile]

thanks for the other help ya gave me!

This topic is closed to new replies.

Advertisement