Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
#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:
C++
#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
C++
<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.
Posted
Updated 16-Feb-21 11:58am
Comments
[no name] 16-Feb-21 14:19pm    
a.) Is the problem on unix or windows or on both?
b.) What has this to do with recursion?
c.) Is your compiler ready for unicode?
d.) Maybe worth to read: How to print Unicode character in C++? - Stack Overflow[^]
rtester40 16-Feb-21 15:05pm    
a.)yes
b.)Recursion part is I do not want to hard code 33 \u2550 in the code I want to use a for loop as in the working example for normal character.
c.)yes
d.)I read that and several others. but all examples I find does not do it with variables. I kind of know where my problem lies but I do not know how to properly call the L switch with a variable. wcout << L variable_name.c_str(); does not work. wcout << L"\u2550"; does.
Rick York 16-Feb-21 15:28pm    
A for loop is not necessarily recursion. In fact a for loop is not necessary to recurse. Recursion means a function effectively calls itself, whether directly or not.
Greg Utas 16-Feb-21 15:55pm    
You don't have to write a for loop to add each character to your string. There is a string constructor whose signature is string(n, c), which returns a string of n "c" characters. For example string t_middle(33,'-'); That said, I've never used Unicode characters, so I can't help you with that aspect.

1 solution

No recursion in this found or needed, perhaps wrong title?

When i compile the code i get warning C4566: Character cannot be displayed in the current code page (1252)

Better use wstring instead of string (and better do not use assignments).

You could write this:
C++
std::wstring t_prefix(L"\u2554");
std::wstring t_middle(L"");
std::wstring t_suffix(L"\u2557\n\u2551");
std::wstring complete_message(L"");


The second is when you want to see the text of a string or wstring you have to write:
C++
std::wcout << complete_message.c_str();

otherwise you would output the pointer to the object
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900