Thread: Regaurding SetConsoleTitle() and whitespace.

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    220

    Regaurding SetConsoleTitle() and whitespace.

    I'm messing around a bit with Windowsz stuffy, i'm really knew..but i'm at a bit of a problem here. I need, somehow, for SetConsoleTitle() to recognize whitespace in a string and not terminate the string..somehow this is happening..

    So heres my code:
    Code:
    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    int main()
    {
        char *title;
        cout << "What would you like the title to be? ";
            cin >> title;
        SetConsoleTitle(title);
        cout << "Hello World!" << endl;
        getch();
        return 0;
    }
    Very general and of coarse i'm coming from C++, just want to get a bit more of a grasp on console aps. But for some reason..either cin >> is recognizing whitespace in the input as the variable terminator, or somehow SetConsoleTitle() doesn't like the whitespace that is supposedly in the char array. Anyone know which one it is? And what I could do(and why it happens), to fix it?

    Also, do you know of any good win programming links that you could pass my way? I would appreciate it.

    NOTE:In this case, the string length and amount of whitespace is obviously dynamic in nature.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Both are a problem.
    "title" is an uninitialized, 4-byte pointer, ie. points to nothing. Try "char title[128]".
    cin parses strings just like scanf("%s") does...one space/tab/newline delimited word at a time. Try getline() to get a line of input at a time.

    gg
    Last edited by Codeplug; 03-22-2004 at 11:05 PM.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    I'm using the pointer to point to the beginning memory address of the character variable, to make it a string..is there a problem with that?

    Ahh, get line, thank you That lies in the standard iostream header yes?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Originally posted by Tronic
    I'm using the pointer to point to the beginning memory address of the character variable, to make it a string..is there a problem with that?
    What?

    Are you saying you're using title to point to char? char isn't a variable, it's a type. It describes title as being "a pointer to data of type char". A string in C is defined as a pointer to an area of memory containing data of type char, the end of the string being denoted by a null character (A character with a value of 0) at the end of it, e.g.:-
    Code:
    char *title = "My title";
    Is a string, as "My title" is a constant and is compiled into the program, title being set to point to "M" (The compiler also throws in the null terminator at the end for you).

    For your program to work, you would need, as Codeplug described, to define title as "char title[128];". This means that title points to an area of memory 128 characters long (You can store a string in there 127 characters long, you need the last character for the null terminator).

    I apologise if I misunderstood you, but it sounded as if you were having trouble understanding the concept of a string in C.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    >A string in C is defined as a pointer to an area of memory containing data of type char.

    That was what I was attempting to do. But I guess my code was incorrect.

    This is what I was attempting to do, or something like it.

    Code:
    #include <iostream>
    #include <conio.h>
    
    int main()
    {
        char *title;
        std::cout << "Enter in a string w/o whitespace: ";
        std::cin >> title;
        int len = strlen(title);
        for (int i = 0; i < len; i++)
        {
            std::cout << title[i];
        }
      getch();
    return 0;
    }
    If I input 'string', it outputs 'string'.

    I was attempting to do something of the sort.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

Popular pages Recent additions subscribe to a feed