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.....