私は5つの異なるラボで20台のPCの配列にStudent IDを格納するプログラムを作成しています。プログラムが既に使用されているIDを検出したときに、ステートメントを出力して、Student IDを求めるプログラムを再起動する必要があります。しかし、私のプログラムが使用済みのIDを検出すると、ラボを依頼してからステートメントを印刷してから、PCの予約を依頼し続けます。Else引数が無視された場合
チェックアウト(ループ)
for(r=0;r<20;r++)
{
if(sId!=l1->pc[r] || sId!=l2->pc[r] || sId!=l3->pc[r] || sId!=l4->pc[r] || sId!=l5->pc[r])
{
printf("The Student ID '%i' has not made any booking\n", sId);
return;
}
}
構造体コード:
typedef struct
{
int lId,pc[20],vunit;
}Openlab;
主
int main()
{
Openlab lab1;
Openlab lab2;
Openlab lab3;
Openlab lab4;
Openlab lab5;
int option;
lab1.lId = 101;
lab1.vunit = 20;
lab1.pc[20];
lab2.lId = 201;
lab2.vunit = 20;
lab2.pc[20];
lab3.lId = 301;
lab3.vunit = 20;
lab3.pc[20];
lab4.lId = 401;
lab4.vunit = 20;
lab4.pc[20];
lab5.lId = 501;
lab5.vunit = 20;
lab5.pc[20];
printf("***Welcome to OpenLab Booking Service***");
while(option>3)
{
printf("\n\nPlease chose an option:");
printf("\n[1]Check In");
printf("\n[2]Check Out");
printf("\n[0]Quit");
printf("\n\nOption: ");
scanf("%i", &option);
if(option == 1)
{
check_in(&lab1,&lab2,&lab3,&lab4,&lab5);
option = 4;
printf("\nUnits Vacant Left in Lab 101: %i\n", lab1.vunit);
printf("Units Vacant Left in Lab 201: %i\n", lab2.vunit);
printf("Units Vacant Left in Lab 301: %i\n", lab3.vunit);
printf("Units Vacant Left in Lab 401: %i\n", lab4.vunit);
printf("Units Vacant Left in Lab 501: %i\n", lab5.vunit);
}
else if(option == 2)
{
check_out(&lab1,&lab2,&lab3,&lab4,&lab5);
option = 4;
printf("\nUnits Vacant Left in Lab 101: %i\n", lab1.vunit);
printf("Units Vacant Left in Lab 201: %i\n", lab2.vunit);
printf("Units Vacant Left in Lab 301: %i\n", lab3.vunit);
printf("Units Vacant Left in Lab 401: %i\n", lab4.vunit);
printf("Units Vacant Left in Lab 501: %i\n", lab5.vunit);
}
else if(option == 0)
{
printf("\n\nThanks for using the OpenLab Booking Service! See you again!\n\n");
}
}
return 0;
}
機能:
void check_in(Openlab *l1, Openlab *l2, Openlab *l3, Openlab *l4, Openlab *l5)
{
int sId,r,lab,comp;
printf("Please enter your Student ID: ");
scanf("%i", &sId);
for(r=0;r<20;r++)
{
if(sId!=l1->pc[r] && sId!=l2->pc[r] && sId!=l3->pc[r] && sId!=l4->pc[r] && sId!=l5->pc[r])
{
while(lab!=l1->lId && lab!=l2->lId && lab!=l3->lId && lab!=l4->lId && lab!=l5->lId)
{
printf("Choose a Open Lab [101,201,301,401,501]: ");
scanf("%i", &lab);
}
}
else
{
printf("The Student ID '%i' has already been used to book a PC!\n", sId);
break;
}
}
if(l1->vunit!=0 && l2->vunit!=0 && l3->vunit!=0 && l4->vunit!=0 && l5->vunit!=0)
{
while(comp>19)
{
printf("Choose a PC [0-19]: ");
scanf("%i", &comp);
}
}
else
{
printf("\nNo vacant PC left Open Lab %i!\n\n", lab);
}
if(lab==l1->lId)
{
//printf("Booking for %i - Lab %i | PC %i ",sId,lab,comp);
if(l1->vunit!=0)
{
l1->vunit--;
l1->pc[comp] = sId;
}
}
else if(lab==l2->lId)
{
//printf("Booking for %i - Lab %i | PC %i ",sId,lab,comp);
l2->vunit--;
l2->pc[comp] = sId;
}
else if(lab==l3->lId)
{
//printf("Booking for %i - Lab %i | PC %i ",sId,lab,comp);
l3->vunit--;
l3->pc[comp] = sId;
}
else if(lab==l4->lId)
{
//printf("Booking for %i - Lab %i | PC %i ",sId,lab,comp);
l4->vunit--;
l4->pc[comp] = sId;
}
else if(lab==l5->lId)
{
//printf("Booking for %i - Lab %i | PC %i ",sId,lab,comp);
l5->vunit--;
l5->pc[comp] = sId;
}
}
チェックアウト:
void check_out(Openlab *l1, Openlab *l2, Openlab *l3, Openlab *l4, Openlab *l5)
{
int sId,r;
printf("Please Enter your Student ID: ");
scanf("%i", &sId);
for(r=0;r<20;r++)
{
if(sId==l1->pc[r])
{
l1->pc[r] = 0;
l1->vunit++;
printf("Removed");
//return;
}
else if(sId==l2->pc[r])
{
l2->pc[r] = 0;
l2->vunit++;
printf("Removed");
//return;
}
else if(sId==l3->pc[r])
{
l3->pc[r] = 0;
l3->vunit++;
printf("Removed");
//return;
}
else if(sId==l4->pc[r])
{
l4->pc[r] = 0;
l4->vunit++;
printf("Removed");
//return;
}
else if(sId==l5->pc[r])
{
l5->pc[r] = 0;
l5->vunit++;
printf("Removed");
//return;
}
}
}
それは、この行が意味することになっている何 –
switch文を使用するのが最善かもしれ: 'lab2.pc [20];'?ところで、*未定義の動作*です。あなたはこれらの5つを持っています。 –
'main'関数で' while'ループは初期化される前に 'option'を使います。初期化されていないローカル変数の値は* indeterminate *です(ランダムであるように見えます)。 –