使用锐利(#)我试图在马里奥游戏结束时打印金字塔

时间:2016-07-26 06:26:51

标签: c cs50

我的语法不正确吗?我在某个地方犯了错误吗?

这些是我收到的错误消息。我已经尝试修复所有这些错误,但我已经碰壁了,不知道该怎么做。

~/workspace/pset1 $ make mario
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow    mario.c  -lcs50 -lm -o mario
mario.c:27:15: error: incompatible pointer to integer conversion assigning to 'char' from 'char [2]' [-Werror,-Wint-conversion]
        blank = "s";
              ^ ~~~

mario.c:29:14: error: incompatible pointer to integer conversion assigning to 'char' from 'char [2]' [-Werror,-Wint-conversion]
        hash = "#";
             ^ ~~~

mario.c:30:16: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Werror,-Wint-conversion]
        printf(blank*(h-(i + 1)));
               ^~~~~~~~~~~~~~~~~

/usr/include/stdio.h:362:43: note: passing argument to parameter '__format' here
extern int printf (const char *__restrict __format, ...);
                                          ^

mario.c:30:16: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
        printf(blank*(h-(i + 1)));
               ^~~~~~~~~~~~~~~~~

mario.c:31:40: error: adding 'int' to a string does not append to the string [-Werror,-Wstring-plus-int]
        printf(hash*((h+1)-(h-(i + 1)))+"\n");
               ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~

mario.c:31:40: note: use array indexing to silence this warning
mario.c:31:16: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
        printf(hash*((h+1)-(h-(i + 1)))+"\n");
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

6 errors generated.
make: *** [mario] Error 1

来源

#include <stdio.h>
#include <cs50.h>

int main()
{
    int h;
    // asking the user to pick the height of the half pyramid
    do
    {
        printf("Pick a number from 1-23: \n");
        h = GetInt();
    }
    while (h > 23);

    //  if the number is 5 the half pyramid should look like this
    //   ----##
    //   ---###
    //   --####
    //   -#####
    //   ######
    //  like at the end of super mario

    int i;
    for (i = 0; i < h; i++)
    {
        char blank;
        blank = "s";
        char hash;
        hash = "#";
        printf(blank * (h-(i + 1)));
        printf(hash * ((h+1)-(h-(i + 1)))+"\n");
    }
    // I wanted to test my code with s and #
    // because I don't know how to print blank spaces in C
}

2 个答案:

答案 0 :(得分:3)

printf width *精确 .*标记一起使用:

char *blank = "";//use char* (or const char*) for string literal
char *hash = "########################";//#*(23+1)
for (int i = 0; i < h; i++)
{
    printf("%*s", h-(i + 1), blank);
    printf("%.*s\n", i + 2, hash);
}

C中不存在字符串常量倍数的基本运算。
(例如&#34;#&#34; * 5 =&gt;&#34; #####&#34;)
所以,要自己实施。
例如,类似以下内容。

#include <stdlib.h>
#include <string.h>
//...
char *stringXn(const char *string, size_t n){
    size_t len = strlen(string);
    char *ret = malloc(len * n + 1);//allocate myself, +1 : for terminator('\0')
    if(ret == NULL){
        fprintf(stderr, "malloc failed in %s function\n", __func__);
        exit(EXIT_FAILURE);
    }
    *ret = 0;
    for(size_t i = 0; i < n; ++i){
        memcpy(ret + i * len, string, len+1);//+1 : for terminator('\0')
    }
    return ret;
}

有了它,你可以写下面的

for (int i = 0; i < h; i++)
{
    char *blank = stringXn(" ", h-(i + 1));
    char *hash  = stringXn("#", i + 2);
    printf(blank);
    puts(hash);//printf("%s\n", hash);
    free(blank);//deallocate myself
    free(hash);
}

答案 1 :(得分:0)

我知道这已经很老了,但可能会对某人有所帮助

    char *blank = "";
    char *hash = "
    for (int i = 0; i < h; i++)
    {
        //if this code is run the first # will not start with 1 but ## so fix that make changes to the below prinf statements

        printf("%*s", h-(i + 1), blank);
        printf("%.*s\n", i + 2, hash);
    }
    //the fixes are minor as below h as we see here is the var we looping on so total number of loops minus 1 blanks

    printf("%*s", h-i , blank);
    //and add 1 to i fixes it.

    printf("%.*s\n", i + 1, hash);