Wil*_*iam 2 c arrays for-loop scanf
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char firstName1[20];
char lastName1[20];
char firstName2[20];
char lastName2[20];
char firstName3[20];
char lastName3[20];
int scores[18];
scanf("%s", &firstName1[0]);
scanf("%s", &lastName1[0]);
printf("%s ", &firstName1[0]);
printf("%s ", &lastName1[0]);
//this for loop here is not being executed, could it be a formatting
issue?
//%i or %d?
int i=0;
for(i=0;i>6;i++)
{
scanf(" %d ", &scores[i]);
}
int j=0;
for(j=0;j>6;j++)
{
printf("%i", &scores{j]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
提供的图片显示输出结果,输入名称和打印名称有效,但是没有执行带有scanf的循环得分
更改
for(i=0;i>6;i++)
Run Code Online (Sandbox Code Playgroud)
和
for(j=0;j>6;j++)
Run Code Online (Sandbox Code Playgroud)
至
for(i=0;i<6;i++)
Run Code Online (Sandbox Code Playgroud)
和
for(j=0;j<6;j++)
Run Code Online (Sandbox Code Playgroud)
循环只运行而条件为真,并在一开始i=0这样i>6是假的......或英文的方式,你写它的循环运行,而我是大于6 -我从0开始,所以它不运行的循环-你的下一个循环同样的问题.
还有一些其他的东西 - 比如在第一个循环中我会排成一行说
printf("please enter score %d: ",i);
Run Code Online (Sandbox Code Playgroud)
因为当你输入数字时它会更有意义,你会看到更好的结果.
编辑下面的代码....**加上注意&只是为了scanf不printf**在下面的代码中看到它已被删除为printf最后一个循环.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char firstName1[20];
char lastName1[20];
char firstName2[20];
char lastName2[20];
char firstName3[20];
char lastName3[20];
int scores[18];
scanf("%s", &firstName1[0]);
scanf("%s", &lastName1[0]);
printf("%s ", &firstName1[0]);
printf("%s ", &lastName1[0]);
//this for loop here is not being executed, could it be a formatting
issue?
//%i or %d?
int i=0;
for(i=0;i<6;i++)
{
printf("please enter score %d: ",i);
scanf(" %d ", &scores[i]);
}
int j=0;
for(j=0;j<6;j++)
{
printf("score %i: %i\n", j, scores{j]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)