它不会像我想要的那样运行。 [英] It won't run like I intend it to.
本文介绍了它不会像我想要的那样运行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看不出我做错了什么。我无法为Q1答案做输入。它立即结束程序。
I don't see what I'm doing wrong. I can't do an input for Q1 answer. It ends the program right away.
#include <stdio.h>
#include <stdlib.h>
void test();
int check_ans();
int questions(int score);
int main()
{
int a, b, c;
system("cls");
printf("\t\tC Program Test");
test();
questions(a);
return 0;
}
void test()
{
char name[60];
printf("\n\n");
printf("\nEnter Your Name:");
scanf("%c", &name[60]);
}
int questions(int score)
{
int answer[15];
system("cls");
printf("\t\tC Program Test");
printf("\nQ1. What value is assigned to the type double variable x by the statement: x = 25.0 * 3.0 / 2.5");
printf("\n1. 20.8");
printf("\n2. 30.0");
printf("\n3. 15.5");
printf("\n4. None of the above");
printf("\n\nQ1 answer:");
scanf("%d", &answer[0]);
}
我的尝试:
我不知道该尝试什么,因为编译时我没有遇到错误。
What I have tried:
I don't know what to try since I dont get errors when compiling it.
推荐答案
你的test
函数只读取一个字符,因此下次调用scanf
时,它将从键盘读取下一个字符。您对name
数组中的任何输入也没有任何作用,因此该函数没有用处。此外,您使用一个整数变量(a
)调用问题
,这样就会包含垃圾。但这并不重要,因为你从未在问题
函数中使用该变量。
你应该回去学习笔记并学习如何正确地处理数组,如何使用scanf
,如何在函数调用中使用参数,以及如何从函数返回信息。
Yourtest
function only reads a single character, so the next time you callscanf
it will read the next characters from the keyboard. You also do nothing with any input you take into thename
array so that function serves no purpose. Also, you callquestions
with an unitialised variable (a
) so that will contain garbage. But that does not really matter since you never use that variable in thequestions
function.
You should go back to your study notes and learn how to address arrays properly, how to usescanf
, how to use parameters in function calls, and how to return information from functions.
传递给scanf
调用的类型(和值)不正确。尝试
The types (and values) passed toscanf
call are incorrect. Try
#include <stdio.h>
#include <stdlib.h>
void test();
int check_ans(int answer);
int questions();
int main()
{
int a, s;
system("cls");
printf("\t\tC Program Test");
test();
a = questions();
s = check_ans(a);
printf("your score is %d\n", s);
return 0;
}
void test()
{
char name[60];
printf("\n\n");
printf("\nEnter Your Name:");
scanf("%s", name);
}
int questions()
{
int answer;
system("cls");
printf("\t\tC Program Test");
printf("\nQ1. What value is assigned to the type double variable x by the statement: x = 25.0 * 3.0 / 2.5");
printf("\n1. 20.8");
printf("\n2. 30.0");
printf("\n3. 15.5");
printf("\n4. None of the above");
printf("\n\nQ1 answer:");
scanf("%d", &answer);
return answer;
}
int check_ans(int answer)
{
return (answer == 2 ) ? 10 : 0;
}
这篇关于它不会像我想要的那样运行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文