2016-11-29 1 views
0

goto 및 레이블을 사용하여이 프로그램을 탐색하려고 시도하지만 프로그램을 탐색 할 때 예상대로 결과가 표시되지 않습니다.C - 루핑/탐색 goto를 사용하여

프로그램을로드 할 때 클래스의 학생 수를 입력해야합니다. 숫자가 아닌 값을 입력하면 값을 다시 입력하기 위해 다시 보내야합니다. 그러나 그것은 프로그램을 통해 계속 진행되는 것으로 보인다. 숫자 값을 입력하면 일반 프로그램처럼 나머지 프로그램을 진행해야합니다. 그러나 그것은 또한 계속 반복하는 것으로 보인다. 마지막으로 여기에 컴파일러에서 제공하는 오류/경고가 없습니다.

질문 : 코드에 누락 된 결함이 있습니까? 내가하려고하는 것을 성취 할 수있는 더 좋은 방법이 있습니까? 절차 적/단순한 접근 방식을 사용하는 대신 함수와 while 루프를 사용하여 프로그램을 다시 작성하는 것이 더 좋고/더 똑똑할까요?

프로그램 :

#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 
int areStudents; 

struct Student 
{ 
    int id; 
    char name[50]; 
    char address[50]; 
    char phone[20]; 
}; 

int main() 
{ 
    char userInput1, userInput2, currentName[50], currentAddress[50], currentPhone[20]; 
    begining: 
    printf("\nEnter the number of students in the class: "); 
    scanf("%c", &userInput1); 
    if(!isdigit(userInput1)) 
    { 
     printf("\nThe input you selected was not valid. Please enter another option\n"); 
     //userInput1 = '0'; 
     goto begining; 
    } else if(isdigit(userInput1)){ 
     // asks users to input P, E, H 
     struct Student student[userInput1]; 
     label1: 
     printf("=>Press P to print the list\n\n=>Press E to enter the student data\n\n=>Press H For help\nPlease enter your selection: "); 
     scanf("%c", &userInput2); 

     switch(userInput2) // switch for choices P, E, and H 
     { 
      case 'p': 
      case 'P': 
      if(areStudents != 1) 
      { 
       printf("\n**You have not set any student variables. Please set them before using this option**\n\n"); 
       goto label1; 
      } else { 
       // print the list of student info 
       for(int i=0; i>=userInput1; i++) 
       { 
        student[i].id = i+1; 
        printf("\nStudent %d name is: %s", i+1, student[i].name); 
        printf("\nStudent %d address is: %s", i+1, student[i].address); 
        printf("\nStudent %d phone number is: %s", i+1, student[i].phone); 
       } 
       break; 
      } 

      case 'e': 
      case 'E': 
      for(int j=0; j>=userInput1; j++) 
      { 
       student[j].id = j+1; 
       printf("\nStudent %d name is: ", j+1); 
       scanf("%s", currentName); 
       strcpy(student[j].name, currentName); 

       printf("\nStudent %d address is: ", j+1); 
       scanf ("%s", currentAddress); 
       strcpy(student[j].address, currentAddress); 

       printf("\nStudent %d phone number is: ", j+1); 
       scanf("%s", currentPhone); 
       strcpy(student[j].phone, currentPhone); 
      } 
      areStudents = 1; 
      goto label1; 

      case 'h': 
      case 'H': 
      printf("Select E or e to enter the data entry mode.\n\nAfter you have entered all your data, you can print the data by entering p or P.\n\nYou can only print after entering your data... otherwise, you will see some garbage values."); 

      default: 
      goto label1; 
     } 
    } 
    return(0); 
} 

결과 (비 수치) : (숫자)

Enter the number of students in the class: d 

The input you selected was not valid. Please enter another option 

Enter the number of students in the class: 
The input you selected was not valid. Please enter another option 

Enter the number of students in the class: 

결과 :

Enter the number of students in the class: 3 
=>Press P to print the list 

=>Press E to enter the student data 

=>Press H For help 
Please enter your selection: =>Press P to print the list 

=>Press E to enter the student data 

=>Press H For help 
Please enter your selection: 
+4

* 절차 적/단순한 접근 방식을 사용하는 대신 함수 및 while 루프를 사용하여 프로그램을 다시 작성하는 것이 더 좋고/현명한 방법일까요? ** ** 예 ** –

답변

0

는 프로그램처럼 보인다, 첫째는 scanf 라인을 건너 뜁니다 그 자체.
시도해보십시오. 프로그램에있는 모든 scanf에 대해 % c 앞에 공백을 입력하십시오. 예를 들면 :
또한 scanf(" %c",&userInput1);

0으로 areStudents 변수를 초기화하고는 J와 루프, 코드는 당신이 발견 한 경우
for(j=0;j>=userInput1;j++)
이다에서 제 2 조건은 "J < = userInput1"해야합니다.

관련 문제