2013-03-28 8 views
0

프로그램 할 자신을 가르치면서 간단한 와트 계산기를 만들기로 결심했습니다. 각 주마다 (KWh) 평균 가격이 다릅니다. 내 문제는 코드에서 34 행을 시작합니다. curr-> next는 null이 아닙니다.링크 된 목록을 통한 루핑 - 무한 루프

내가 뭘 잘못하고 있니?

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 


float calculate(float w, float cents, float h); 

struct states 
{ 
    char *name; 
    char *price; 
    struct states *next; 
}; 

int main() 
{ 
    float watts,hours,cents; 
    int a = 0; 
    struct states *curr, dict[52]; 
    char st_ab[52][3] = {"ID","ND","WA","AR","UT","LA","WY","SD","NE","KY","WV","OK","OR","MT","MS","TN","IN","NC","MS","IA","KS","VA","NM","MN","SC","AZ","GA","AL","CO","OH","TX","IL","FL","NV","MI","WI","PA","MD","DC","DE","MA","RI","CA","ME","VT","NJ","NH","AK","CT","NY","HI"}; 
    char st_pr[52][5] = {"8.0","8.1","8.2","8.7","8.8","8.9","8.9","9.0","9.0","9.0","9.2","9.2","9.4","9.6","9.7","9.8","10.0","10.2","10.3","10.5","10.5","10.5","10.7","10.9","11.0","11.1","11.1","11.1","11.2","11.2","11.3","11.7","11.7","11.7","13.0","13.0","13.2","13.7","13.7","13.7","14.8","14.9","15.2","15.5","16.1","16.3","16.5","17.5","18.1","18.1","33.2"}; 
    char state[2]; 

    for(a=0;a<=52;a++) 
    { 
     memset(&dict[a],0,sizeof(struct states)); /* zero out structure */ 
     if(a==52) 
     { 
      break; /* if this is the last struct, leave it NULL so we can loop through linked list */ 
     } 
     dict[a].next=&dict[a+1];   
    } 

    for(curr = dict;curr ->next != NULL;curr = curr->next) /* fill our list with state abbr. and cost/KWh */ 
    { 
     curr->name = st_ab[a]; 
     curr->price = st_pr[a]; 
     a++; 
    } 

    puts("Dictionary loaded!"); 
    printf(" Two letter state code: ");     /* get info from user */ 
    scanf("%s",state); 

    printf(" Wattage of appliance: "); 
    scanf("%f",&watts); 

    printf(" Hours of use per day: "); 
    scanf("%f",&hours); 

    for(curr = dict; curr->next != NULL; curr=curr->next) 
    { 
     printf("comparing %s with %s\n", curr->name,state); /* search for our state, set cents respectively */ 
     if((strcmp(curr->name,state)) == 0) 
     { 
      printf("State found! -- %s\n",state); 
      cents=atof(curr->price); 
      printf("%f cents\n",cents); 
      break; 
     } else { 
      printf("state didn't match\n"); 
      continue; 
     } 
    } 

    printf("\nAverage cost per day: %.2f\n", calculate(watts,cents,hours)); 
    printf("Average cost per year: %.2f\n\n", (calculate(watts,cents,hours)) * 365);  

    return 0; 

} 

float calculate(float w, float c, float h) 
{ 
    float kwh = (w/1000) * h; 
    float cos = kwh*(c/100); 
    return(cos); 
} 
+2

'for (a = 0; a <= 52; a ++)'마지막 유효 인덱스는 51. –

답변

2

문제는 배열의 끝에 지난 1 임에도 불구하고 &dict[a+1]은 결코 NULL입니다

dict[a].next = &dict[a+1]; 

C 표준 보증입니다. 이로 인해 다음 루프가 목록 끝에 도달하게되고 정의되지 않은 동작이 발생합니다 (프로그램에서 아무 것도 할 수 있음). 첫 번째 루프 이후에 연결된 목록을 끝내려면

dict[51].next = NULL; 

을 수행하십시오.

편집 : for(a=0;a<=52;a++) 실제로 배열 끝에서 두 개의 요소를 지나치므로 두 위치에서 동작이 정의되지 않습니다. 이 문제를 해결하려면 a<52까지 반복해야합니다.

+0

빠른 응답을 보내 주셔서 감사합니다! 다른 미성년자를 조정 한 후 이제는 훌륭하게 작동합니다! – elixxir

0

는 "curr-> 다음 null는"- 글쎄, 당신은 어떻게 그 null로 동일 옆에 정지 조건이 curr-> 인 경우 루프 중지하도록 기대합니까?