What's the Difference???

This is a discussion on What's the Difference??? within the C++ Programming forums, part of the General Programming Boards category; I have been experimenting with different functions and created this little program that displays a box around a string Code: ...

  1. #1
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96

    What's the Difference???

    I have been experimenting with different functions and created this little program that displays a box around a string
    Code:
    #include <windows.h>
    #include <vector>
    #include <string>
    
    int main() {
        std::string text = "this is a test";
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        LPDWORD NumWritten;
        COORD position = {1, 1};
        std::string line, spaces;
        std::vector<std::string> box;
        std::vector<std::string>::iterator iter;
        std::string::size_type text_length = text.length();
        std::string::size_type box_length  = text_length + 2;
     
        char top_left_corner      = (unsigned char)201,
             top_right_corner     = (unsigned char)187,
             bottom_left_corner   = (unsigned char)200,
             bottom_right_corner  = (unsigned char)188,
             vert_sides           = (unsigned char)186,
             horz_sides           = (unsigned char)205;
        spaces.assign(text_length, ' '); 
        line.assign(text_length, horz_sides);
    
        //  create box
        box.push_back(top_left_corner + line + top_right_corner);    
        box.push_back(vert_sides + spaces + vert_sides);
        box.push_back(vert_sides + text + vert_sides);
        box.push_back(vert_sides + spaces + vert_sides);
        box.push_back(bottom_left_corner + line + bottom_right_corner);
    
        // display box
        for (iter = box.begin(); iter != box.end(); ++iter) {
            SetConsoleCursorPosition(hOut, position);
            WriteConsole(hOut, iter->c_str(), iter->length(), NumWritten, NULL);
            ++position.Y;
        }
    
        SetConsoleCursorPosition(hOut, position);
        system("pause");
        return 0;
    }
    this works fine. But when I do this
    Code:
    #include <windows.h>
    #include <vector>
    #include <string>
    
    void display(std::string);
    
    int main() {
        std::string text = "this is a test";
        display(text);
        system("pause");
    
        return 0;
    }
    
    
    void display(std::string text) {
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        LPDWORD NumWritten;
        COORD position = {1, 1};
        std::string line, spaces;
        std::vector<std::string> box;
        std::vector<std::string>::iterator iter;
        std::string::size_type text_length = text.length();
        std::string::size_type box_length  = text_length + 2;
     
        char top_left_corner      = (unsigned char)201,
             top_right_corner     = (unsigned char)187,
             bottom_left_corner   = (unsigned char)200,
             bottom_right_corner  = (unsigned char)188,
             vert_sides           = (unsigned char)186,
             horz_sides           = (unsigned char)205;
        spaces.assign(text_length, ' '); 
        line.assign(text_length, horz_sides);
    
        //  create box
        box.push_back(top_left_corner + line + top_right_corner);    
        box.push_back(vert_sides + spaces + vert_sides);
        box.push_back(vert_sides + text + vert_sides);
        box.push_back(vert_sides + spaces + vert_sides);
        box.push_back(bottom_left_corner + line + bottom_right_corner);
    
        // display box
        for (iter = box.begin(); iter != box.end(); ++iter) {
            SetConsoleCursorPosition(hOut, position);
            WriteConsole(hOut, iter->c_str(), iter->length(), NumWritten, NULL);
            ++position.Y;
        }
    
        SetConsoleCursorPosition(hOut, position);
        return;
    }
    I get an access viotation fault and the program crashes at the very end. I don't see what the problem is. The only thing I did different is put where the box is built and displayed in its own function.

    BTW, I know that it is not a good idea to use system("pause"), but this is only an experiment.....
    Last edited by rwmarsh; 07-12-2006 at 09:18 AM.
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  2. #2
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Try to move the declaration of the handle, to a global declaration:
    Code:
    #include <windows.h>
    #include <vector>
    #include <string>
    
    void display(std::string);
    
    HANDLE hOut;
    
    int main() {
        std::string text = "this is a test";
        display(text);
        system("pause");
    
        return 0;
    }
    
    
    void display(std::string text) {
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        LPDWORD NumWritten;
        COORD position = {1, 1};
        std::string line, spaces;
        std::vector<std::string> box;
        std::vector<std::string>::iterator iter;
        std::string::size_type text_length = text.length();
        std::string::size_type box_length  = text_length + 2;
     
        char top_left_corner      = (unsigned char)201,
             top_right_corner     = (unsigned char)187,
             bottom_left_corner   = (unsigned char)200,
             bottom_right_corner  = (unsigned char)188,
             vert_sides           = (unsigned char)186,
             horz_sides           = (unsigned char)205;
        spaces.assign(text_length, ' '); 
        line.assign(text_length, horz_sides);
    
        //  create box
        box.push_back(top_left_corner + line + top_right_corner);    
        box.push_back(vert_sides + spaces + vert_sides);
        box.push_back(vert_sides + text + vert_sides);
        box.push_back(vert_sides + spaces + vert_sides);
        box.push_back(bottom_left_corner + line + bottom_right_corner);
    
        // display box
        for (iter = box.begin(); iter != box.end(); ++iter) {
            SetConsoleCursorPosition(hOut, position);
            WriteConsole(hOut, iter->c_str(), iter->length(), NumWritten, NULL);
            ++position.Y;
        }
    
        SetConsoleCursorPosition(hOut, position);
        return;
    }
    Be easy on me...only 14

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I think both ought to crash, you're just getting lucky. NumWritten is your problem. You never initialize it, and pass a pointer to some random location in memory to WriteConsole. Furthermore, you never allocate any memory for the pointer to point to.

    Declare NumWritten as a DWORD, not a LPDWORD. (Then pass &NumWritten)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    Declare NumWritten as a DWORD, not a LPDWORD. (Then pass &NumWritten)
    Ahh, I knew it was something simple. MSDN said that NUmWritten was a LPDWORD so that was what I used. I thought that I should be passing the address of NumWritten but it would not work since I was declaring it as a LPDWORD. Thanks, I'll do that and see what happens!
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 06:36 PM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 11:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 03:27 AM
  4. Replies: 6
    Last Post: 08-27-2006, 12:09 AM
  5. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21