I am relearning C++ and I am trying to make a hangman game. which I have working from a YouTube video.
they use the following hard coded cout statement: cout << "+--------------------+\n";
with 33 dashes. I did this:
<pre lang="C++">
#include <iostream>
int main()
{
std::string t_prefix = "+";
std::string t_middle = "";
std::string t_suffix = "+";
int max_message_length = 33;
for (int i = t_middle.length(); i < max_message_length; i++) {
t_middle = "-" + t_middle;
}
std::string complete_message = t_prefix + t_middle + t_suffix;
std::cout << complete_message;
}
and it prints out like it is suppose to. Then I got the ideal to use the extended ascii or Unicode characters for the box drawing.
and came up with this and it works but it is again hard coded for a total of 35 characters:
#include <iostream>
#include <io.h>
#include <fcntl.h>
void PrintMessage(std::string message, bool print_top = true, bool print_bottom = true)
{
int max_message_length = 33;
if(print_top){
std::wcout << L"\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"
<< L"\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\n";
std::wcout << L"\u2551";
}
else{
std::wcout << L"\u2551";
}
bool front = true;
for (int i = message.length(); i < max_message_length; i++){
if (front){
message = " " + message;
}
else{
message = message + " ";
}
front = !front;
}
std::wcout << message.c_str();
if (print_bottom){
std::wcout << L"\u2551\n";
std::wcout << L"\u2560\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"
<< L"\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2563\n";
}
else{
std::wcout << L"\u2551\n";
}
}
int main(){
_setmode(_fileno(stdout), _O_U16TEXT);
PrintMessage("HANGMAN");
}
I was trying to combine what I learned from the first example with what I learned from the Unicode example, and I came up with the following:
#include <iostream>
#include <io.h>
#include <fcntl.h>
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
std::string t_prefix = "\u2554";
std::string t_middle = "";
std::string t_suffix = "\u2557\n\u2551";
int max_message_length = 33;
for (int i = t_middle.length(); i < max_message_length; i++) {
t_middle = "\u2550" + t_middle;
}
std::string complete_message = t_prefix + t_middle + t_suffix;
std::wcout << complete_message;
}
it prints out garbage because I can not figure out how to output the wcout line correctly it needs the L for it to work right but when I put the L in it like this std::wcout << L complete_message; the L is not reconized.
What I have tried:
I tried to do it this way
<pre>std::wcout << L"\u2554" << t_middle << "\u2557\n\u2551";
still printed out garbage. then I tried a bunch of other stuff I can not remember but in the end it was not printing anything to the console window at all.
all I am trying to accomplish here is to print the following ╔ ═*33 ╗ ║on a new line
preferably cross platform.
Sorry for any errors in formatting I tried my best to do things the right way for this post.