2017-12-21 3 views
-1
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の値を入力させ、次に2つの配列array1 [16]とarray2 [45]を反復しようとしています。ユーザーの入力値が最初の配列1にある場合、私は0.2387のガスを割り当てたいと思います。入力値が他の配列にある場合は、私は0.2710のガスを割り当てたいと思います。C++数値の配列を検索しても、else文が見つからない場合はelse文をトリガーしない

基本的には、ユーザーの入力がどの配列に含まれているかに応じて値を割り当てたいと思っています。私はC++を非常に新しくしています。

array1またはarray2内の数値を入力し、array1またはarray2にある数字を正しく識別すると正常に動作しているようです。問題は、いずれかの配列内にないことがわかっている数値を入力すると、else文がトリガーされ、array2にあると識別されます。たとえば、user_codeとして12345と入力すると、「Code found in Array2:0」と表示されます。私は12345がarray2に含まれていないことを知っています。なぜ* array2_searchが0に割り当てられているのか分かりません。この問題を解決するにはどうすればよいですか?user1がarray1またはarray2に含まれていない場合、

+2

「int」変数には分数がありません。毎回 'gas'に' 0'を割り当てています。 – Barmar

+1

「16」や「45」のような数字をあちこちにハードコードしないでください。名前付き定数を宣言します。 – Barmar

+1

'if(array1_search!= array1 + 16){' - if(array1_search!= std :: end(array1)) 'を使用してください。また、値として「45」などの数字を使用して間違いを犯すことは非常に簡単です。もしあなたが '44'のエントリを持っていて、あなたがミスカウントして45のエントリがあると思ったらどうなりますか?あなたのコードはまだコンパイルされますが、間違った結果に終わるでしょう。代わりにコンパイラにカウントをさせてください。つまり、 'int array2 [] = {whatever};' – PaulMcKenzie

答えて

1
else if (array2_search != array2_search + 45) { 

C++ 11のstd::endを使用して

else if (array2_search != array2 + 45) { 

か良いはずです:

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

そしてint gas; =>double gas;次のことができるようにしたい場合iだけでなく浮動小数点値を格納するntegers(0.2387と0.2710は整数0を与える)。あなたがC++ 11最小を持っている場合は、このような何かを行うことができ、標準のコンテナおよび新しいC++の機能を使用して

0

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; 
} 

あなたも、2つのブールのフラグを削除することによって、このもう少し簡素化することができます。 arr1またはarr2の値が変更された場合、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'; 
} 
関連する問題