2014-03-01 3 views
-1

if 및 if else 루프가 작동하지 않는 이유를 알지 못합니다. 나는 또한 내가 과거의 B 등급 프로그램이 아닌 가치 등급을 얻을 수 없다는 것을 이해하지 못한다. 논리는 그냥 흐르지 않는다. 이 프로그램은 세 가지 입력 성적 int (0 -100)을 취하고 "> 또는 <"이 설정된 기준으로 등급을 부여합니다. 그러나 그들은 나를 위해 일하지 않습니다. 다른 사람에게 일하게하려면 어떻게해야합니까? 감사합니다. . 당신의 else 문이 잘못 때문에C else 루프가 작동하지 않는 경우

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h> 
#include <stdbool.h> 

// Functions 
void readScores(int* test1, int* test2, int* test3); 
int determineAverage(int test1, int test2, int test3); 
void print(int test3, int average, int test2_test3Av); 
int avgtest2test3(int test2, int test3); 


int main(void) 
{ 
    int test1; 
    int test2; 
    int test3; 
    int average; 
    int test2_test3Av; 

    readScores(&test1, &test2, &test3); // takes input 
    average = determineAverage(test1, test2, test3); // finds average 
    test2_test3Av = avgtest2test3(test2, test3); // gets average of test one and two 
    print(test3, average, test2_test3Av); // Prints grade resluts 
    return 0; 
} 

void readScores(int *test1, int *test2, int *test3) 
{ 
    // Promts 
    printf("\nHello, this program will determine"); 
    printf("\nthe grades of an average test scores"); 
    printf("\nto see if you passed or not this year."); 
    printf("\nPlease enter in the three test..."); 
    printf("\nNote: only enter scores that are (0-100)\n"); 

    printf("Enter in test 1\n"); 
    scanf(" %d", test1); 
    printf("Enter in test 2\n"); 
    scanf(" %d", test2); 
    printf("Enter in test 3\n"); 
    scanf(" %d", test3); 
    return; 
} 
int determineAverage(int test1, int test2, int test3) 
{ 
    // Local declrations 
    int average; 

    // Math 
    average = (test1 + test2 + test3)/3; 
    return average; 
} 
void print(int test3, int average, int test2_test3Av) 
{ 
    if (average >= 90) 
    { 
     printf("Great job you have an A %d int the class\n", average); 
    } 
    else if (average >= 70 && average <= 90, test3) 
    { 
     if (test3 >= 90) 
     { 
      printf("Good job you got a A %d\n", average); 
     } 
     else if 
     { 
      printf("Easy Beezy you got a B %d for the class\n", average); 
     } 
    } 
    else if (average >= 50 && average <= 70) 
    { 

     if (test2_test3Av >= 70) 
     { 
      printf("You passed congrats you have a C %d for the class\n", average); 
     } 
     else if 
     { 
      printf("You have a D for the class %d\n", average); 
     } 
    } 
    else if (average <= 50) 
    { 
     printf("Yeah you might want to take this class again you have a F %d\n", average); 
    } 
    return; 
} 

int avgtest2test3(int test2, int test3) 
{ 
    int holder; 
    holder = ((test2 + test3)/2); 
    return holder; 
} 
+2

안녕하세요 [StackOverflow의] 환영 (http://stackoverflow.com)이다. [도움말 페이지] (http://stackoverflow.com/help), 특히 [여기에 대해 내가 물어볼 항목은 무엇입니까?] (http://stackoverflow.com/help) 섹션을 읽으십시오./on-topic) 및 [ "어떤 유형의 질문을하지 않아야합니까?"] (http://stackoverflow.com/help/dont-ask). 더 중요한 것은 [Stack Overflow question checklist] (http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)를 읽어보십시오. 또한 [SSCCE] (http://www.sscce.org/)가 무엇인지 배우고 싶을 수도 있습니다. – hivert

+0

표시된 코드에 루프가 없습니다. – alk

답변

1

여러분의 프로그램은 컴파일되지 않습니다 :

if (test3 >= 90) 
{ 
    printf("Good job you got a A %d\n", average); 
} 
else if // << here is the problem 
{ 
    printf("Easy Beezy you got a B %d for the class\n", average); 
} 

그냥 드롭 if else 후 :

if (test3 >= 90) 
{ 
    printf("Good job you got a A %d\n", average); 
} 
else 
{ 
    printf("Easy Beezy you got a B %d for the class\n", average); 
} 
1

이 문제 문을 명확하게. 프로그램이 실행되고 있지 않습니다. else-if를 사용하면 검사 할 조건이 있어야합니다. 조건이 없으면 if-else를 사용할 수 있습니다. 당신이

if (test3 >= 90) 
     { 
      printf("Good job you got a A %d\n", average); 
     } 
     else 
     { 
      printf("Easy Beezy you got a B %d for the class\n", average); 
     } 
    } 
    else if (average >= 50 && average <= 70) 
    { 

     if (test2_test3Av >= 70) 
     { 
      printf("You passed congrats you have a C %d for the class\n", average); 
     } 
     else 
     { 
      printf("You have a D for the class %d\n", average); 
     } 
} 

하지만 당신은 확인해야 논리에

if (test3 >= 90) 
     { 
      printf("Good job you got a A %d\n", average); 
     } 
     else if 
     { 
      printf("Easy Beezy you got a B %d for the class\n", average); 
     } 
    } 
    else if (average >= 50 && average <= 70) 
    { 

     if (test2_test3Av >= 70) 
     { 
      printf("You passed congrats you have a C %d for the class\n", average); 
     } 
     else if 
     { 
      printf("You have a D for the class %d\n", average); 
     } 

이 코드 부분을 변경하는 경우 즉, 프로그램이 실행됩니다. 에러 코딩이

0
else if (average >= 50 && average <= 70) 
{ 

}

if (test3 >= 90) 
{ 
    printf("Good job you got a A %d\n", average); 
} 
else 
{ 
    printf("Easy Beezy you got a B %d for the class\n", average); 
} 
관련 문제