C ++在数组中搜索数字,但如果找不到则不会触发else语句

问题描述 投票:-1回答:2
int gas;
// Input Code   
int user_code;
std::cout << std::endl;
std::cout << "Please enter the Code: ";
std::cin >> user_code;
std::cout << "The value you entered is " << user_code;
std::cout << std::endl;

int array1[16] = { 42011, 42017, 42029, 42045, 
                   42091, 42101, 34001, 34005,    
                   34007, 34009, 34011, 34015, 
                   34033, 10001, 10003, 24015 }; // 0.2387    (23.87%)

int array2[45] = { 11001, 24003, 24510, 24005, 24009,
                   24013, 24017, 24019, 24021, 24025, 
                   24027, 24029, 24031, 24033, 24035, 
                   24037, 24041, 24043, 51510, 51013, 
                   51043, 51047, 51600, 51059, 51610, 
                   51061, 51069, 51630, 51099, 51107, 
                   51683, 51685, 51153, 51157, 51177, 
                   51179, 51187, 51840, 54003, 54027, 
                   54037, 54065, 42001, 42055, 42133 }; //0.2710 (27.10%)

int * array1_search;
array1_search = std::find(array1, array1+ 16, user_code);

int * array2_search;
array2_search = std::find(array2, array2 + 45, user_code);


if (array1_search != array1+ 16) {
    std::cout << "Codefound in Array1: " << *array1_search << '\n';
    gas= 0.2387;

}

else if (array2_search != array2_search + 45) {
    std::cout << "Code found in Array2: " << *array2_search << '\n';
    gas= 0.2710;
}
else {
    std::cout << "Not found \n";
    gas= 0.1506;
}

以上是我目前的代码。我试图让用户输入一个变量user_code值,然后迭代两个数组array1 [16]和array2 [45]。如果用户输入值在第一个array1上,我想分配气体0.2387,如果输入值在另一个array2上,我想分配气体0.2710,如果它不在任何阵列气体中,则应该是0.1506。

所以基本上我想根据用户输入所包含的数组来分配一个值。我对c ++很新,最好的方法是什么?

如果我输入一个在array1或array2中的数字并且它正确识别在array1或array2中找到的数字,它似乎工作正常。问题是当我输入一个我知道不在任何一个数组中的数字来触发else语句时它将其标识为在array2中。例如,当我输入12345作为user_code时,它显示“在Array2:0中找到的代码”。我知道12345不包含在array2中,我不明白为什么* array2_search被分配0.我该怎么做才能解决这个问题,如果输入的user_code不包含在array1或array2中,它会转到else语句?

c++ arrays if-statement
2个回答
1
投票
else if (array2_search != array2_search + 45) {

应该

else if (array2_search != array2 + 45) {

或者更好地使用C ++ 11的std::end

if (array1_search != std::end(array1)) {
else if (array2_search != std::end(array2)) {

并且int gas; => double gas;如果你想能够存储浮点值,而不仅仅是整数(0.2387和0.2710会给出整数0)。


0
投票

如果你有最低C ++ 11,那么使用标准容器和更新的c ++特性,你可以这样做:

int main() {
    // Use Constants Instead of "Hard Coded Values" 
    // If you noticed these are not even needed.
    // const unsigned code1 = 16;
    // const unsigned code2 = 45;

    // Made gas a float instead of an int due to the decimal values
    // I also initialized it with the default value if the code is 
    // not found in either container.
    float gas = 0.1506f; // Default Price If Not Found

    // created your first array as a const std::vector<int> and
    // used its initializer list to populate its contents: this vector
    // can not be modified: remove the const if this container
    // will need to have entries added in the future.
    const std::vector<int> arr1 { 42011, 42017, 42029, 42045,
                                  42091, 42101, 34001, 34005,
                                  34007, 34009, 34011, 34015,
                                  34033, 10001, 10003, 24015 }; // 0.2387 (23.87%)

    // did the same for the second array
    const std::vector<int> arr2 { 11001, 24003, 24510, 24005, 24009,
                                  24013, 24017, 24019, 24021, 24025,
                                  24027, 24029, 24031, 24033, 24035,
                                  24037, 24041, 24043, 51510, 51013,
                                  51043, 51047, 51600, 51059, 51610,
                                  51061, 51069, 51630, 51099, 51107,
                                  51683, 51685, 51153, 51157, 51177,
                                  51179, 51187, 51840, 54003, 54027,
                                  54037, 54065, 42001, 42055, 42133 }; //0.2710 (27.10%)

    // No changes made here same basic user I/O.
    int user_code = 0;
    std::cout << "Please enter the Code: ";
    std::cin >> user_code;
    std::cout << "The value you entered is " << user_code;
    std::cout << "\n";

    // Created 2 flags for later.
    bool b1found = false;
    bool b2found = false;

    // auto for loop ranged based.
    for ( auto code : arr1 ) {
        if ( code == user_code ) {
            b1found = true; // Set flag
            gas = 0.2387f;  // Set new gas
            // Output code & gas
            std::cout << "Code found in Arr1: " << code << '\n';
            std::cout << "gas = " << gas << '\n';
        }
    }

    for ( auto code : arr2 ) {
        if ( code == user_code ) {
            b2found = true; // set flag
            gas = 0.2710f;  // set gas
            // output code & gas
            std::cout << "Code found in Arr2: " << code << '\n';
            std::cout << "gas = " << gas << '\n';
        }
    }

    // If code not found in either output "not found" and display default gas
    if ( !b1found && !b2found ) {
        std::cout << "Not found\n";
        std::cout << "gas = " << gas << '\n';
    }   

    std::cout << "\nPress any key and enter to quit." << std::endl;
    char c;
    std::cin >> c;

    return 0;
}

你甚至可以通过删除两个bool标志来简化这一点。我们知道如果在arr1arr2中找到gas值会改变的值,那么我们真正需要做的就是检查它是否已被更改。

// auto for loop ranged based.
for ( auto code : arr1 ) {
    if ( code == user_code ) {
        gas = 0.2387f;  // Set new gas
        // Output code & gas
        std::cout << "Code found in Arr1: " << code << '\n';
        std::cout << "gas = " << gas << '\n';
    }
}

for ( auto code : arr2 ) {
    if ( code == user_code ) {
        gas = 0.2710f;  // set gas
        // output code & gas
        std::cout << "Code found in Arr2: " << code << '\n';
        std::cout << "gas = " << gas << '\n';
    }
}

const float defaultGas = 0.1506;
// If code not found in either output "not found" and display default gas
if ( gas == defaultGas ) {
    std::cout << "Not found\n";
    std::cout << "gas = " << gas << '\n';
}   
© www.soinside.com 2019 - 2024. All rights reserved.