簡體   English   中英

繼續並在do..while循環中中斷語句錯誤

[英]Continue and break statement error in do..while loop

具體來說,我想在開始時介紹6個地區的不同汽車。 該程序會問我哪個地區的汽車,如果輸入的內容相同,我將繼續提供汽車的詳細信息,如果不同,我想回到地區閱讀。 我的問題是當我完全閱讀第一個區域時,該程序可以正常工作並返回到區域讀取,但是在第二個區域中,該程序不再起作用。 我將在上面附加代碼。 我需要提及的是,這是我第一次使用“ continue”和“ break”語句,這是主要問題。 謝謝 !

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

typedef struct record {
    char name[15];
    int year[4];
    char serial[15];
    char owner[24];
    char plate[12];
};

int main() {
    struct record rec[100];
    char search[20];
    int imax=0;
    int i=0,j=0;
    char n;
    char* district[]=     {"Rahova","Giulesti","Crangasi","Militari","Pantelimon","Ferentari"};
    char district_input[20];


printf("\nEnter the cars: ");
do {
    printf("\nCar's number %d -> Enter district: ",i+1);
    scanf("%s",district_input);
    for (;j<=5;j++) {
        if(strcmp(district_input,district[j])==0){
            printf("\t\t    ->Name of the car: ");
            scanf("%s",rec[i].name);
            printf("\t\t    ->Release year: ");
            scanf("%d",rec[i].year);
            printf("\t\t    ->Serial number: ");
            scanf("%s",rec[i].serial);
            printf("\t\t    ->Number plate: ");
            scanf("%s",rec[i].plate);
            printf("\t\t    ->Last 3 owners: ");
            scanf("%s",rec[i].owner);
            printf("\nAdd more? [y/n]\t");
            i++;
            imax++;
            break;
        } 
        continue;
    }
    printf("\nAdd more? [y/n]\t");
} while ((n=getche()) != 'n');

}

breakcontinue不要做您認為他們做的事。 它們僅適用於寫入它們的最內層循環。 在這種情況下,for循環。 它們與外部while循環無關。

因此,例如

for (;j<=5;j++) {
  ...
  continue
}

與...相同

for (;j<=5;j++) {
  ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM