简体   繁体   English

无法在 C 中打印多行字符串

[英]Unable to print multi-line strings in C

I am trying to print a series of multi-line strings (ascii art letters here), and when printing them out, the top of each letter moves to the right while the rest of the letter stays in the same position.我正在尝试打印一系列多行字符串(此处为 ascii 艺术字母),当打印出来时,每个字母的顶部向右移动,而字母的 rest 保持在相同的 position 中。 Here is a screenshot of what occurs:这是发生的情况的屏幕截图: 错误图像

I do not know why this is happening, as I am fairly new to C;我不知道为什么会这样,因为我对 C 还很陌生; if you have any knowledge about this, please share it!如果您对此有任何了解,请分享!


#include <stdio.h>
#include <curses.h>

typedef const char letter[];


letter Y = 
"___      __\n
\\ \\__ / /\n
 \\ \\ / /\n
 |  |  |\n
 |  |  |\n
 |__|__|\n";

letter O =  
"_______ \n
/   __ \\\n
|  |  | |\n
|  |__| |\n
\\_______/\n";

letter U = 
" __    __ \n
 / |   | \\\n
|  |   |  |\n
|   \\_/   |\n
\\_________/\n";

letter L = 
" _\n"
"| |\n"  
"| |\n"
"| |__\n"
"|____/\n";

letter S =
" _________\n"
"/   _____/\n"
"\\_____  \\\n"
"/        \\\n"
"/_______  /\n"
"        \\/\n";

letter T =
"___________\n"
"\\__    ___/\n"
"   |   |\n"
"   |   |\n"
"   |___|\n";

letter EXCLAMATION_POINT =
"_________\n"
"\\\\\\\\|////\n"
" \\\\\\|///\n"
"  \\\\|//\n"
"   \\|/\n"
"   ***\n"
"   ***\n"
"    *\n";

const char *MESSAGE[] = {Y, O, U, L, O, S, T, EXCLAMATION_POINT};

int main() {
    initscr();
    cbreak();
    noecho();

    int maxY, maxX;
    getmaxyx(stdscr, maxY, maxX);

    int spacingPerLetter = maxX / 8;
    
    for (int i = 0; i < 8; i++) {
        mvprintw(maxY / 2, spacingPerLetter * (i + 1), MESSAGE[i]);
        refresh();
        getch();
        clear();
    }

    endwin();
    return 0;
}

The main problem is the newline embedded inside the strings you print.主要问题是嵌入在您打印的字符串中的换行符。

The first "line" of the letters will be printed in the correct position, but then the newline will reset the position to the first column on the next line.字母的第一“行”将打印在正确的 position 中,但随后换行符会将 position 重置为下一行的第一列。

I recommend that you print each "letter" line by line (without the newlines).我建议您逐行打印每个“字母”(不带换行符)。 This could be helped by having each "letter" be an array of arrays of characters, where each sub-array is one line of the letter:这可以通过让每个“字母”成为一个字符数组arrays来帮助,其中每个子数组是字母的一行:

#define LETTER_WIDTH   11
#define LETTER_HEIGHT   6

const char Y[LETTER_HEIGHT][LETTER_WIDTH] = {
    "___      __",
    "\\ \\__ / /",
    " \\ \\ / / ",
    " |  |  |   ",
    " |  |  |   ",
    " |__|__|   "
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM