c - 这个程序应该画一座房子。但功能无法使用

标签 c cygwin

我正在尝试用 C 语言编写这个程序,用星星、破折号或点画出一座房子。它会询问用户一系列问题(大房子还是小房子)、(使用什么字符?)、(有多少个 window ?),然后它应该绘制房子。我还没有为无效响应添加任何代码,因为我想先检查它是否适用于有效响应,但事实并非如此。在第三个问题之后,Cygwin 说“段错误(核心转储)”。有人可以告诉我问题是什么吗?

#include <stdio.h>
#include <math.h>
#define house 1

char qbs; // for quit big small
char dsd; // for dash star dot
int now; // number of windows
int i, f; // random variables for for loops


main()
{
while(house) {
printf("Do you want a big or small house? (Q: quit, B: big house, S: small house)\n");
scanf("%s", &qbs);
switch(qbs){
    case 'B': 
    case 'b': 
    case 's':
    case 'S': break;
    default : printf("%s is not a valid choice. Please try again.\n", qbs);
              continue;
    }

printf("Which characters do you want for drawing? (DASH(-), STAR(*), or DOT(.))\n");
scanf("%s", &dsd);
printf("Using %c to draw the house.\n", dsd);

printf("How many windows do you want in the house? (0 through 4 for big houses and 0 through 2 for small houses.)\n");
scanf("%d", &now);

switch(dsd){
        case '-': dsd='-';
                  break;

        case '*': dsd='*';
                  break;

        case '.': dsd='.';
                  break;
        }
printf("%c %c %d", qbs, dsd, now);

switch(qbs){
        case 'Q':
        case 'q': printf("QUITTING THE PROGRAM.\n"); break;

        case 'B':
        case 'b': printf("         "); // this line and the next 7 lines draw the BIG roof 
                  for(i=0; i<=42; i++) {printf("%s", dsd);}
                  printf("\n      ");
                  for(i=0; i<=48; i++) {printf("%s", dsd);}
                  printf("\n   ");
                  for(i=0; i<=56; i++) {printf("%s", dsd);}
                  printf("\n");
                  for(i=0; i<=62; i++) {printf("%s", dsd);}
                 switch(now) { 
                    case '0':
                        for(f=0; i<=15; f++) { // the vertical expansion
                            for(i=0; i<=62; i++) {printf("%s", dsd); printf("\n");} // the horizontal expansion
                        }
                        break;

                    case '1':
                        for(i=0; i<=62; i++) {printf("%s", dsd); printf("\n");} // first line with no windows
                        for(f=0; f<=13; f++) { 
                            for(i=0; i<=28; i++) { printf("%s", dsd); } // left side of window
                            for(i=0; i<=6; i++) { printf(" "); } // window
                            for(i=0; i<=28; i++) { printf("%s"); } // right side of window
                        }
                        for(f=0; f<=2; f++) { for(i=0; i<=62; i++) {printf("%s", dsd);}} // bottom two lines with no windows
                        break;

                    case '2': 
                        for(i=0; i<=62; i++) {printf("%s", dsd); printf("\n");} // first line with no windows
                        for(f=0; f<=13; f++) {
                            for(i=0; i<=21; i++) { printf("%s", dsd); } // left side
                            for(i=0; i<=7; i++) {printf(" ");} // first window
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // space between windows
                            for(i=0; i<=7; i++) {printf(" ");} // second window
                            for(i=0; i<=21; i++) { printf("%s", dsd); } // right side
                        }
                        for(f=0; f<=2; f++) { for(i=0; i<=62; i++) {printf("%s", dsd);}} // bottom two lines with no windows
                        break;

                    case '3': 
                        for(i=0; i<=62; i++) {printf("%s", dsd); printf("\n");} // first line with no windows
                        for(f=0; f<=13; f++) {
                            for(i=0; i<=11; i++) { printf("%s", dsd);} // left side
                            for(i=0; i<=6; i++) {printf(" "); } // first window
                            for(i=0; i<=11; i++) { printf("%s", dsd);} // space between window 1 and 2
                            for(i=0; i<=6; i++) {printf(" "); } // second window
                            for(i=0; i<=11; i++) { printf("%s", dsd);} // space between window 2 and 3
                            for(i=0; i<=6; i++) {printf(" "); } // third window
                            for(i=0; i<=11; i++) { printf("%s", dsd);} // right side
                        }
                        for(f=0; f<=2; f++) { for(i=0; i<=62; i++) {printf("%s", dsd);}} // bottom two lines with no windows
                        break;

                    case '4': 
                        for(i=0; i<=62; i++) {printf("%s", dsd); printf("\n");} // first line with no windows
                        for(f=0; f<=13; f++) {
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // left side
                            for(i=0; i<=8; i++) {printf(" ");} // first window
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // space between win1 and win2
                            for(i=0; i<=8; i++) {printf(" ");} // second window
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // space between win2 and win3
                            for(i=0; i<=8; i++) {printf(" ");} // third window
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // space between win3 and win4
                            for(i=0; i<=8; i++) {printf(" ");} // fourth window
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // right side
                        }
                        for(f=0; f<=2; f++) { for(i=0; i<=62; i++) {printf("%s", dsd);}} // bottom two lines with no windows


        case 'S':
        case 's': printf("         "); // this line and the next 7 lines draw the SMALL roof
                  for(i=0; i<=12; i++) {printf("%s", dsd);}
                  printf("\n      ");
                  for(i=0; i<=18; i++) {printf("%s", dsd);}
                  printf("\n   ");
                  for(i=0; i<=24; i++) {printf("%s", dsd);}
                  printf("\n");
                  for(i=0; i<=30; i++) {printf("%s", dsd);}
                  switch(now) { 
                    case '0':
                        for(f=0; i<=6; f++) { // the vertical expansion
                        for(i=0; i<=30; i++) {printf("%s", dsd); printf("\n");} // the horizontal expansion
                        }
                        break;

                    case '1':
                        for(f=0; f<=5; f++) { 
                        for(i=0; i<=12; i++) { printf("%s", dsd); } // left side of window
                        for(i=0; i<=6; i++) { printf(" "); } // window
                        for(i=0; i<=12; i++) { printf("%s"); } // right side of window
                        }
                    for(f=0; f<=1; f++) { for(i=0; i<=30; i++) {printf("%s", dsd);}} // bottom line with no windows
                    break;

                    case '2': 
                        for(f=0; f<=5; f++) {
                            for(i=0; i<=6; i++) { printf("%s", dsd); } // left side
                            for(i=0; i<=6; i++) {printf(" ");} // first window
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // space between windows
                            for(i=0; i<=6; i++) {printf(" ");} // second window
                            for(i=0; i<=6; i++) { printf("%s", dsd); } // right side
                        }
                    for(f=0; f<=1; f++) { for(i=0; i<=62; i++) {printf("%s", dsd);}} // bottom line with no windows
                    break;
                  printf("Small house with %d windows\n", now);
                  break;
}
}
}
}

最佳答案

在整个代码中,尝试替换

printf("%s", dsd)

printf("%c", dsd)

对你的 scanf 做同样的事情。您需要字符,而不是字符串。

无论如何,这很有趣,一天前有人想画同样的房子,这是你的学习练习吗? ^^

关于c - 这个程序应该画一座房子。但功能无法使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22153195/

相关文章:

c - C中的多线程服务器/客户端实现

无法打开共享对象 cygmpfr-1.dll

python - shell脚本检查python版本

bash - 用 cygwin 打开 chrome

Bash echo 命令不使用转义字符

c - 在 C 中使用 libxml 进行 XML 解析不显示属性名称

c - 为什么不能在声明后初始化常量变量?

创建文件并将其附加到目录中

c - 如何从 C 运行外部程序并解析其输出?

cygwin - 为 Cygwin 安装附加包