我想根据用户输入打印由数字和美元符号组成的模式。我要求用户选择一个模式的选项,然后我要求模式的大小。我使用交换来创建模式,所以如果用户选择选项1然后输入4的大小,他们应该得到类似的东西:打印阵列元素给出错误的输出
4 $$$ $ 4 $$ $$ 4 $ $$$ 4。
现在我只是想让patternOne(0
工作。我无法打印出我在patternOne()
函数中创建的数组元素。这里是我的代码:
#include <stdio.h>
int main(void) {
int option, size;
do {
printf("\nMENU\n");
printf("1. Pattern one\n"
"2. Pattern two\n"
"3. Pattern three\n"
"4. Pattern four\n"
"5. Quit\n");
fflush(stdout);
do {
printf("Please enter your choice (between 1 & 5): ");
fflush(stdout);
scanf("%d", &option);
}while(option < 1 || option > 5);
switch(option) {
case 1:
do {
printf("Choose a pattern size (between 2 & 9): \n");
fflush(stdout);
scanf("%d", &size);
}while(size < 2 || size > 9);
patternOne(size);
break;
case 2:
printf("Testing case 2.");
break;
case 3:
printf("Still testing.");
break;
case 4:
printf("Testing case 4.");
break;
case 5:
printf("Thank you for playing.\n");
break;
}
}while(option != 5);
return 0;
}
void patternOne(int size) {
char ar[size];
ar[0] = size;
for(int i = 1; i < size - 1; i++) {
ar[i] = '$';
}
int x = 0, y = 1, temp, iter = 0;
while(iter <= size) {
for(int j = 0; j < size - 1; j++)
{
printf(j);
}
temp = ar[x];
ar[x] = ar[y];
ar[y] = temp;
x++;
y++;
iter++;
}
}
打开编译器警告。这段代码中有几个项目应该关注,其中最值得注意的是'patternOne'中错误执行的[**'printf' **](http://en.cppreference.com/w/c/io/fprintf) '函数,为此您不提供格式字符串。 – WhozCraig