Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C语言中删除最上面的空格_C_Cs50 - Fatal编程技术网

如何在C语言中删除最上面的空格

如何在C语言中删除最上面的空格,c,cs50,C,Cs50,你看,在顶部,高度的正下方,在金字塔图形和高度之间有一个空格:8我不想要这个。另外,我是一个超级C初学者,没有复杂的库和主题,请只使用基本语法 到目前为止我已经试过了 Height: 8 # # ## ## ### ### #### #### ##### ##### ###### ###### ####### ####### ######## ######## 在主for循环中,像这样 if (h &

你看,在顶部,高度的正下方,在金字塔图形和高度之间有一个空格:8我不想要这个。另外,我是一个超级C初学者,没有复杂的库和主题,请只使用基本语法

到目前为止我已经试过了

Height: 8
          
       #  #
      ##  ##
     ###  ###
    ####  ####
   #####  #####
  ######  ######
 #######  #######
########  ########
在主for循环中,像这样

if (h > 0)
        {
        printf("\n");
        }

我想知道我做错了什么,以及如何修复它,使顶部没有空间。记住我刚开始的没有高级库或主题这是我的第一个问题集。

如果您不希望该行包含0个哈希,请不要将其包含在循环中:

Height: 8
                 #  #
      ##  ##
     ###  ###
    ####  ####
   #####  #####
  ######  ######
 #######  #######
########  ########
从1开始循环会从循环中删除空行。

在for循环中将h的值从0更改为1。它会起作用的

完整代码:

我刚把h的值从0改为1

我的输出:


您知道如何将IDE与调试器一起使用吗?请尝试@Unn at的答案
#include <cs50.h>
#include <stdio.h>

//Prototypes.
void print_hashes(int n);
void print_spaces(int n);

//Main function.
int main(void)
{
    //Recieves,checks and saves user input. Prompts to enter information again if invalid.
    int height = -1;
    while (height < 1 || height > 8)
    {
        height = get_int("Height: ");
    }

    //Outputs graphic to the screen with the use of logic with functions.
    for (int h = 0; h <= height; h++)
    {
        print_spaces(height - h);
        print_hashes(h);
        printf("  ");
        print_hashes(h);
        if (h > 0)
        {
        printf("\n");
        }
    }
}


//Function for printing hashes to the screen.
void print_hashes(int n)
{
    for (int c = 0; c < n; c++)
    {
        printf("#");
    }
}

//Function for printing spaces to the screen so the hashes will be in the right spot.
void print_spaces(int n)
{
    for (int c = 0; c < n; c++)
    {
        printf(" ");
    }
}
Height: 8
                 #  #
      ##  ##
     ###  ###
    ####  ####
   #####  #####
  ######  ######
 #######  #######
########  ########
for (int h = 1; h <= height; h++)
#include <stdio.h>

//Prototypes.
void print_hashes(int n);
void print_spaces(int n);

//Main function.
int main(void)
{
    //Recieves,checks and saves user input. Prompts to enter information again if invalid.
    int height = -1;
    while (height < 1 || height > 8)
    {   
        printf("Previous Height was: %d\n", height);
        scanf("%d", &height);
        printf("New Height is: %d\n", height);
    }

    //Outputs graphic to the screen with the use of logic with functions.
    for (int h = 1; h <= height; h++)
    {   
          print_spaces(height - h);
          print_hashes(h);
          printf("  ");
          print_hashes(h);
          printf("\n");
    }
}


//Function for printing hashes to the screen.
void print_hashes(int n)
{
    for (int c = 0; c < n; c++)
    {
        printf("#");
    }
}

//Function for printing spaces to the screen so the hashes will be in the right spot.
void print_spaces(int n)
{
    for (int c = 0; c < n; c++)
    {
        printf(" ");
    }
}
for (int h = 1; h <= height; h++)
    {   
          print_spaces(height - h);
          print_hashes(h);
          printf("  ");
          print_hashes(h);
          printf("\n");
    }
Please enter new Height in the range between 1 to 8 including 1 and 8.                                                                                                                                                     
9                                                                                                                                                                                                                          
Please enter new Height in the range between 1 to 8 including 1 and 8.                                                                                                                                                     
8                                                                                                                                                                                                                          
New Height is: 8 
       #  #                                                                                                                                                                                                                 
      ##  ##                                                                                                                                                                                                                
     ###  ###                                                                                                                                                                                                               
    ####  ####                                                                                                                                                                                                              
   #####  #####                                                                                                                                                                                                             
  ######  ######                                                                                                                                                                                                            
 #######  ####### 
########  ########