值同步无法正常工作

时间:2017-03-27 19:27:23

标签: c arrays loops matrix strcmp

我的程序链接中有完整的代码;请阅读本文,我在这里的代码足够长但不是全部:https://pastebin.com/NpspcJB8

作为评估的一部分,我试图让我的代码“同步”数组索引与另一个。出现的问题是,当我重复运行部分代码时,输​​入值后输出不会改变,例如:

case 'b':

    CaseBPrompt();

    for (t=0;t<350;t++) {
        int result2[350];
        result2[t] = strcmp(blankSpace, parkingSpace[t]); // determine if the value of parkingSpace is the same as strcmp "empty"
        Synchronise(t);

        if (result2[t] == 0) { // if value of parkingSpace == "empty"
            // use spaceNumber; sync int variable w/array index
            LevelPrint(t);

            strcpy(carCustomer[t], customerName);
            // copy value of car number to index
            strcpy(parkingSpace[t], carNumber); // omit array index to ensure this can run
            break; // required, as will print other available spaces otherwise
        }

    }
    printf("You will take %s's car\n", carCustomer[t]);
    printf("Please drive the car over to %c%d\n", level, spaceNumber[t][3]);
break;

t是一个整数变量,我在main之外全局声明。以下是我在这里使用的函数的内容:

void CaseBPrompt() {
    printf("You have chosen to park a customer's car \n");
    printf("Please input the customer's name, using _ in lieu of a space\n");
    scanf(" %s", &customerName);
    // assign customer's name to carNumber - new array?

    printf("Please input the values of the number plate\n");
    // ensure use %s, as collecting string, and so const char/char error won't occur
    scanf(" %s", &carNumber);

}
void LevelPrint(int n) {
    if (spaceNumber[n][3] <= 100) {
    level = 'A';
    // don't forget to write out complete matrix for spaceNumber!

    printf("There is an available space at parking space %c%d\n", level,spaceNumber[n][3]);
    // assign parkingSpace array index w/user input

    } else if (spaceNumber[n][3] > 100 && spaceNumber[n][3]<= 200) {
        level = 'B';
        spaceNumber[n][3]-=100; // display correct parking space at corresponding level
        printf("There is an available space at parking space %c%d\n", level,spaceNumber[n][3]);

    } else if (spaceNumber[n][3] > 200 && spaceNumber[n][3]<= 300) {
        level = 'C';
        spaceNumber[n][3]-=200; // display correct parking space at corresponding level
        printf("There is an available space at parking space %c%d\n", level,spaceNumber[n][3]);

    } else if (spaceNumber[n][3] > 300 && spaceNumber[n][3]<= 350) {
        level = 'D';
        spaceNumber[n][3]-=300; // display correct parking space at corresponding level
        printf("There is an available space at parking space %c%d\n", level,spaceNumber[n][3]);

    } 
}
void Synchronise(int a) {
    spaceNumber[a][3] = n+1; // synchronise w/parking space
    carCustomer2[a][3] = n+1; // synchronise w/car customer name
}

当我运行代码时,我会收到此输出:

empty
350
25
Please write car number (without spaces):
b

b
Valet options (press the corresponding letter to select):
a) Check available spaces
b) Park a customer's car
c) Retrieve a customer's car
q) Exit the program
Your choice: b
You have chosen to park a customer's car
Please input the customer's name, using _ in lieu of a space
b
Please input the values of the number plate
b
There is an available space at parking space A1
You will take b's car
Please drive the car over to A1
Valet options (press the corresponding letter to select):
a) Check available spaces
b) Park a customer's car
c) Retrieve a customer's car
q) Exit the program
Your choice: b
You have chosen to park a customer's car
Please input the customer's name, using _ in lieu of a space
b
Please input the values of the number plate
b
There is an available space at parking space **A1**
You will take b's car
Please drive the car over to **A1**
Valet options (press the corresponding letter to select):
a) Check available spaces
b) Park a customer's car
c) Retrieve a customer's car
q) Exit the program
Your choice: c
You have chosen to retrieve a customer's car
Please write their name, again with _ in lieu of a space:
c
Please write down what was on their number plate:
90jda
c's car is at **1**.
Valet options (press the corresponding letter to select):
a) Check available spaces
b) Park a customer's car
c) Retrieve a customer's car
q) Exit the program
Your choice:

我用输出的星号包围的字符让我感到担忧,因为在case 'c'下添加代码之前,case b下的代码会正确打印。我正在使用DevC ++,我想知道我的代码是否有任何问题。

当我选择再次执行选项B时,代码将在此之前的每次迭代中打印A2,A3等,并且在选项C输出下显示的值是149,因为我预先指定了值90jda下的parkingSpace[150][3]

parkingSpace下的值旨在与blankSpace的{​​{1}}同步,并在其case 'b'循环中打印出该可用空间。对于forcase 'c'用于与来自用户输入parkingSpace的任何索引保持相同的字符串同步,该字符串在carNumber循环的开头进行比较for 1}}。

1 个答案:

答案 0 :(得分:0)

我相信我设法解决了它,这是因为我在数组中声明的其中一个值不相同;程序没有返回错误,因为它们都被声明为整数变量