简体   繁体   English

我的bool函数不断返回true,但我不确定为什么

[英]My bool function keeps returning true and im not sure why

Im doing an excercise sheet to get an understanding of functions and I am currently working on the following question. 我正在做一份练习单以了解功能,目前正在研究以下问题。

Write function prototypes for each of the following: 为以下各项编写函数原型:

  1. A function HasValue that may be passed a reference to an array, the size of the array and a search value. 函数HasValue可以传递对数组的引用,数组的大小和搜索值。 The function should return true if the search value exists in the array 如果搜索值存在于数组中,则该函数应返回true

In my code I have sent the contents of the array, the array size and the value to be searched in the array to the bool function. 在我的代码中,我已将数组的内容,数组的大小和要在数组中搜索的值发送给bool函数。

In the function I compared the value to each element of the array using a for loop. 在函数中,我使用for循环将值与数组的每个元素进行了比较。

I then created a variable count in the function that will be incremented if the value matches any element in the array. 然后,我在函数中创建了一个变量计数,如果该值与数组中的任何元素匹配,该计数将增加。

I then used an if else statment to return true if count is greater than 0 and false if count is equal to 0. The problem is however that the function is only returning true thus the output will always be "this number appears in the array" 然后我使用的if else statment返回true ,如果计数大于0和false如果计数等于0的问题然而,该功能仅返回true因此输出将总是“这个号码出现在阵列中”

Logically these steps seem correct to me but obviously there is a flaw somewhere that I cant see. 从逻辑上讲,这些步骤对我来说似乎是正确的,但显然我看不到某个缺陷。 I presume its just I do not have a decent understanding of Bool functions yet but if someone could explain where and why I'm going wrong it would be greatly appreciated in my learning process to understanding functions and c++. 我认为它只是我对Bool函数还没有一个很好的了解,但是如果有人可以解释我在哪里以及为什么会出错,那么在学习函数和c ++的过程中将不胜感激。

#include <iostream>
#include <iomanip>
#include "stdafx.h"

using namespace std;

bool HasValue(int Array[], int size, int Value);

int main()
{
    int value;
    int Array[10]{ 3,5,6,8,9,1,2,14,12,43 };

    cout << "enter value you wish to search for in array " << endl;
    cin >> value;

    HasValue(Array, 10 , value);

    if (true)
        cout << "This number appears in the array " << endl;
    else
        cout << "This number does not appear in the array " << endl;

    return 0;
}

bool HasValue(int Array[], int size, int Value)
{
    int count = 0;

    for (int i = 0; i < size; i++)
    {
       if (Value == Array[i])
       {
           count++;
       }    
    }

    if (count > 0)
    {
        return true;
    }
    else
        return false;
}

You test code is the problem 你测试代码是问题

HasValue(Array, 10 , value);
if (true)
    cout << "This number appears in the array " << endl;
else
    cout << "This number does not appear in the array " << endl;

This ignores the return value of HasValue and always prints "This number appears in the array" . 这将忽略HasValue的返回值,并始终"This number appears in the array"

HasValue(Array, 10 , value);

This line of code executes the function but ignores the returned value. 这行代码执行函数,但忽略返回的值。 When a function returns a value, you need to assign it to a variable: 当函数返回值时,您需要将其分配给变量:

bool result = HasValue(Array, 10 , value);

Then if (true) does not have any reference to the returned value. 然后, if (true)没有对返回值的任何引用。 The true inside the if will cause the first cout to always print. true的好像里面将导致第一cout总是打印。 You will never see the output from the else . 您将永远不会看到else的输出。 But once you have the return value in a variable, you can use it in the if : 但是,一旦将返回值包含在变量中,就可以在if使用它:

if(result)

You can reduce this all to one line of code, if you want: 如果需要,您可以将所有内容简化为一行代码:

if(HasValue(Array, 10 , value))

Now the if statement will directly test the return value from HasValue() . 现在, if语句将直接测试HasValue()的返回值。 In this particular case, combining the code into a single line seems reasonable. 在这种特殊情况下,将代码合并为一行似乎是合理的。 You must be careful doing this, though. 不过,您必须小心谨慎。 When you combine too much into a single line, the code becomes more difficult to debug. 当您将太多内容组合到一行中时,代码将变得更加难以调试。 You will need to find a balance between readability and convenience as you continue learning how to program. 在继续学习编程的过程中,您将需要在可读性和便利性之间找到平衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM