[英]In C checking the value of every element and return true or function
Can someone help me or give idea on how can I correct my logic in the array loop有人可以帮助我或提供有关如何更正数组循环中的逻辑的想法
My Problem is I have student quiz scores and perfect quiz scores我的问题是我有学生测验分数和完美测验分数
My Goal is I want to compare student quiz scores every element to the Perfect quiz scores, where Student scores cannot be negative value or greater than the perfect quiz scores, and if it is inputted negative I just want to reinput it to the value where it is not negative or greater than the quiz scores, but I really have no idea how to do it.我的目标是我想将学生测验分数的每个元素与完美测验分数进行比较,其中学生分数不能为负值或大于完美测验分数,如果输入为负值,我只想将其重新输入到它所在的值不是负数或大于测验分数,但我真的不知道该怎么做。
Also I tried creating function for quizScore
and input the loop there and if the quiz is either negative or greater than, then I'll just call the quizScore
function but it still fails.我还尝试为
quizScore
创建函数并在那里输入循环,如果测验为负或大于,那么我将只调用quizScore
函数,但它仍然失败。
void initStudents() {
printf("\nEnter number of students: ");
scanf("%d", &studentCount);
for(int i=0; i < studentCount; i++){
printf("\n-------------------------------------------------------------------\n");
printf("\n[Student %d of %d]\n", i + 1,studentCount);
printf("Enter name of student %d: \n", i + 1);
scanf("%s",studentNames[i]);
for(int j = 0; j < qCount; j++){
printf("\n[Quiz %d of %d] \n", j + 1, qCount);
printf("Enter score for quiz %d: \n", j + 1);
scanf("%d", &quiz[j]);
if(quiz[j] < 0 || quiz[j] > qPerfect[j]) {
quiz[j] = 0;
}
else {
//no idea what to put here
}
}
printf("\n-------------------------------------------------------------------\n");
for(int j = 0; j < pCount; j++) {
printf("\n[Project %d of %d]\n", j + 1, pCount);
printf("Enter score for project %d: \n", j + 1);
scanf("%d", &project[j]);
}
printf("\n-------------------------------------------------------------------\n");
for(int j = 0; j < hmCount; j++) {
printf("\n[Homework %d of %d]\n", j + 1, hmCount);
printf("Enter score for homework %d: \n", j + 1);
scanf("%d", &homework[j]);
}
}
//end of initstudents
Loop until there's a valid input:循环直到有一个有效的输入:
while (true) {
printf("\n[Quiz %d of %d] \n", j+1, qCount);
printf("Enter score for quiz %d: \n", j+1);
scanf("%d",&quiz[j]);
if (quiz[j] < 0 || quiz[j] > qPerfect[j]) {
quiz[j] = 0;
} else {
break;
}
}
You can do it using while
你可以使用
while
while (quiz[j] < 0 || quiz[j] > qPerfect[j]) {
printf("Invalid score \n");
printf("Enter score for quiz %d: \n", j + 1);
scanf("%d", &quiz[j]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.