开发者

Trouble with functions returning bool!

开发者 https://www.devze.com 2023-01-02 01:30 出处:网络
I don\'t understand exactly how to use a function that returns a boolean. I know what it is, but I can\'t figure out how to make it work in my program. I\'m trying to say that if my variable \"selecti

I don't understand exactly how to use a function that returns a boolean. I know what it is, but I can't figure out how to make it work in my program. I'm trying to say that if my variable "selection" is any letter beween 'A' and 'I' then it is valid and can continue on to the next function which is called calcExchangeAmt(amtExchanged, selection). If it is false I want it to ask the user if they want to repeat the program and if they agree to repeat. I want it to clear the screen and restart to the main function. How do I make my program work as intended?

This is my bool function:

bool isSelectionValid(char selection, char yesNo, double amtExchanged)
{
    bool validData;

    validData = true;

    if ((selection >= 'a' && selection <= 'i') || 
        (selection >= 'A' && selection <= 'I'))
    {
        validData = calcExchangeAmt (amtExchanged, selection);
    }
    else(validData == false);
    {
        cout << "Do you wish to continue?  (Y for Y开发者_JAVA技巧es / N for No)";
        cin >> yesNo;
    }

    do
    {
        main();
    }

    while ((yesNo =='y')||(yesNo == 'Y'));
    {
        system("cls");
    }
    return 0;
}

I get this warning:

warning C4800: 'double' : forcing value to bool 'true' or 'false' (performance warning)


A bool function should return true or false. I'm guessing your warning is caused by the fact that you're declaring validData as bool, but then assign it a different value (returned by calcExchangeAmt function). That value is getting converted from its value type (double) to boolean (true or false).

So, your IsSelectionValid method should just return true if selection is valid, or false if it's not. Then whatever code needs to know that information can proceed accordingly.

I don't know much C++, so forgive me for syntax problems my code is bound to have, but your code should look something like this:

bool isSelectionValid(char selection)
{
  return (selection >= 'a' && selection <= 'i') || (selection >= 'A' && selection <= 'I');
}

void myCallingFunction(double amtExchanged, char selection)
{
   bool isSelectionValid = isSelectionValid(selection);

   if(isSelectionValid)
  {
     double exchangeAmt = calcExchangeAmt (amtExchanged, selection);
  }
  else 
  {
     cout<<"Do you wish to continue?  (Y for Yes / N for No)";
     cin>>yesNo;

     if((yesNo =='y')||(yesNo == 'Y'))
     {
        main(); // or whatever code starts another attempt
     }
}


This code is seriously confusing and very non-C++ like. We normally expect main() to be the function that drives things and calls other functions, not to have it called from some other place. We generally avoid do unless there is a compelling reason (and I don't see one here). I think it's highly unlikely that a function called calcExchangeAmt returns true or false; I suspect it actually returns a number that you should be doing something else with (showing to the user?).

With all this going on, trying to explain your actual compiler error messages is of limited value. Your code is all inside out and backwards. Anna Lear's answer seems like a better starting point if it makes sense to you.


The type of 0 is not bool; true or false is bool. It is telling you that 0 is a double, but it is forcing it to a boolean type.

0

精彩评论

暂无评论...
验证码 换一张
取 消