Thread: Help with Bool function

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    5

    Help with Bool function

    Okay I'm trying to use a bool function to either continue or terminate the program, but I'm not quite sure what I'm doing wrong. I'm trying to use what I've learned from my previous programs but I think I'm messing something up with the parameters that I'm sending or maybe with the bool test. Any help would be appreciated. Thanks.

    Code:
    #include <cstdlib>
    #include<iostream>
    
    using namespace std;
    
    bool repeat(char choice)
    {
         bool test=true;
         
         
         test = (choice =='y') || (choice =='Y');
         
         if (test=true)
         {
         return true;
         }
         else
         {
         return false;
         }
    }
    
    
    
    int main()
    {
    
    cout<< "do while loop" << endl;
    
    int i=1, sum,num;
    bool answer;
    char choice;
    sum =0;
    do {
        cout<< "Enter number "<< i << "->";
        cin>> num;
        sum += num;
        i++;
        cout<<"Enter Y or y to try again >>"<<endl;
        cin>>choice;
        answer=repeat(choice);
        
    } while(answer!=false);
    cout<< "The sum is: "<< sum<< endl <<endl;
    
    
        system("pause");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This operator "=" is a assignment.
    This operator "==" is a comparison.

    Code:
    if (test=true)
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    Thank you so much! This definitely needed a fresh pair of eyes.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A few more minor notes:
    1. Please be careful to post in the appropriate forum next time. This is C++ code, in the C forum. Mods may move this.
    2. Your indentation/formatting could use some work. Well-organized code with consistent indentation and formatting are key to code that is easy to write, read and maintain; it reduces the likelihood of bugs, and makes them easier to find and fix.
    3. You don't really need a temp variable in your repeat() function, or an if/else. You can return the value of the expression directly (making your function one line).
    Code:
    return (choice == 'y') || (choice == 'Y');
    // or even better yet
    return std::toupper(choice) == 'Y';
    Note: std::toupper is a function that converts a char to upper case. I'm not a C++ expert, but a quick Google search suggests you should #include <cctype> to use it. Since you're using namespace std; you can drop the namespace part.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to C++ programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    Quote Originally Posted by anduril462 View Post
    3. You don't really need a temp variable in your repeat() function, or an if/else. You can return the value of the expression directly (making your function one line).
    I'm not sure I understand how to do this. This was the only way I could figure to do it without copy and pasting something else. Could you explain in more detail?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by jaimeribg View Post
    I'm not sure I understand how to do this. This was the only way I could figure to do it without copy and pasting something else. Could you explain in more detail?
    I gave you the alternate version of the function body -- what you can replace lines 8-20 with in your original example. But here it is with the function declarator and curly brackets:
    Code:
    bool repeat(char choice)
    {
        return (choice == 'y') || (choice == 'Y');
    }
    Or, another alternative:
    Code:
    bool repeat(char choice)
    {
        return std::toupper(choice) == 'Y';
    }
    Both of those return the value of the expression comparing choice to upper/lower case Y. The comparison (==) and logical OR (||) result in boolean values of true/false, which is what you want to return. They "combine" the if/else in your code with returning the variable you set in the if/else into a shorter, but equivalent form. A return statement can return any expression (of the right type for that function)*. That expression could be a single variable, which has the value you want to return (as you did in your first post). It could be a literal value, like return false; or return 42;. It could be a complex expression like return a + b; or return (choice == 'y') || (choice == 'Y');. The expression being returned is evaluated to a single value, and that value is returned. In the examples I gave, the result of comparing choice to 'y' or 'Y' (i.e. true if it's equal to one or the other, false if not) is returned directly. No need for an if/else to determine the return value, nor a temporary variable ('test' in your code) to store that value.

    Hope that's clear.

    * Syntactically, it can return any expression, meaning any valid expression (of the right type) can be returned from a function. However, that doesn't mean that returning anything of the right type is safe or is a good idea. Examples being trying to return the address of a local variable or a local array. The memory those addresses refer to is invalid when the function exits, so returning them is pointless, and can cause problems if you try to access the memory at those addresses.
    Last edited by anduril462; 03-12-2014 at 01:39 PM. Reason: clarification

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A function that returns a bool if true.
    By MCDD in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2012, 03:58 PM
  2. Bool Find Price function help
    By 16800960 in forum C++ Programming
    Replies: 2
    Last Post: 06-01-2010, 05:44 AM
  3. Writing a bool function
    By findme in forum C++ Programming
    Replies: 6
    Last Post: 11-29-2005, 11:31 PM
  4. Damned bool function
    By Welshy in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2005, 01:28 AM
  5. bool function
    By MB1 in forum C++ Programming
    Replies: 10
    Last Post: 04-22-2005, 05:31 PM