C++ C++;在数组中搜索数字,但如果找不到则不会触发else语句 int气体; //输入代码 int用户代码; std::cout用户代码; 标准::cout

C++ C++;在数组中搜索数字,但如果找不到则不会触发else语句 int气体; //输入代码 int用户代码; std::cout用户代码; 标准::cout,c++,arrays,if-statement,C++,Arrays,If Statement,应该是 else if (array2_search != array2_search + 45) { 或更好地使用C++11: else if (array2_search != array2 + 45) { 和int气体=>双气体使用标准容器和更新的C++特征,如果你有C++ 11最小值,你可以这样做: else if (array2_search != std::end(array2)) { int变量没有分数。您每次都将0分配给gas。不要到处对16和45这样的数字进行硬编码。声明

应该是

else if (array2_search != array2_search + 45) {
或更好地使用C++11:

else if (array2_search != array2 + 45) {

int气体=>
双气体< P>使用标准容器和更新的C++特征,如果你有C++ 11最小值,你可以这样做:
else if (array2_search != std::end(array2)) {

int
变量没有分数。您每次都将
0
分配给
gas
。不要到处对
16
45
这样的数字进行硬编码。声明命名常量。
if(array1\u search!=array1+16){
--请使用
if(array1\u search!=std::end(array1))
。此外,使用
45
等数字作为值时很容易出错。如果您有
44个
条目,并且您计算错误并认为有45个条目,该怎么办?您的代码仍然会编译,但最终会得到错误的结果。相反,让编译器进行计数。即
int array2[]={whatever};
对不起,这是一个打字错误,gas在原文中是双精度的
if (array1_search != std::end(array1)) {
else if (array2_search != std::end(array2)) {
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;
}
// 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';
}