Thread: Stuck on a practice problem- modulus operator

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    2

    Stuck on a practice problem- modulus operator

    Hi guys,

    I'm a complete beginner to programming and have been trying to learn c++ through online tutorials. It's a hobby for me and it has been quite fun learning it. I have been attempting to solve one of the problems that was part of a tutorial, but now I find myself unable to understand why my code won't work.

    Here's what I'm trying to achieve:

    -Write a function called IsEven() that returns true if an integer passed to it is even. Use the modulus operator to test whether the integer parameter is even.

    I know there are probably a million ways to do this, and I'd appreciate if anyone would like to suggest simpler solutions. I'd however really like to know why my code won't work. This code will build a solution fine, but it won't give me the answer I'm looking for.

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    int nValue;
    
    bool IsEven ()
    
    { 
        using namespace std;
        
        if (nValue %2 ==0)
            return true;
        else
            return false;     
    }
    
    int main ()
    {
        using namespace std;
        cout << "Enter an integer" << endl;
        int nValue;
        cin >> nValue; 
        
        IsEven ();    
    
        return 0;
    }
    Any help and advice will be greatly appreciated.

    Cheers.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > -Write a function called IsEven() that returns true if an integer passed to it is even
    You're missing the "is passed" part.

    What you have at the moment is a global variable called nValue.

    What is even more confusing (for you) is that you also have a local variable called nValue as well.

    main uses the local variable, and isEven uses the global variable.

    Oh, and put
    using namespace std;
    just once, after all the include's.


    Edit
    +1 for using code tags right first time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should check the tutorials on how to pass arguments and how to return them. That is what you're missing, and it's quite simple.

    Btw, this code

    if (nValue %2 ==0)
    return true;
    else
    return false;

    can be reduced to simply

    return (nValue %2 == 0);

    because it's a boolean expression. That is, nValue %2 == 0 returns true or false, which you check with your if statement. But since it already returns true or false (what you want), why not directly return it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    2
    Quote Originally Posted by Salem View Post
    > -Write a function called IsEven() that returns true if an integer passed to it is even
    You're missing the "is passed" part.

    What you have at the moment is a global variable called nValue.

    What is even more confusing (for you) is that you also have a local variable called nValue as well.

    main uses the local variable, and isEven uses the global variable.

    Oh, and put
    using namespace std;
    just once, after all the include's.


    Edit
    +1 for using code tags right first time.
    Thanks for the explanation. This makes thing a lot clearer now!

    Oh, and it's not hard to get the tags right, especially if you take a moment to preview prior to posting.

    Quote Originally Posted by Elysia View Post
    ...Btw, this code

    if (nValue %2 ==0)
    return true;
    else
    return false;

    can be reduced to simply

    return (nValue %2 == 0);

    because it's a boolean expression. That is, nValue %2 == 0 returns true or false, which you check with your if statement. But since it already returns true or false (what you want), why not directly return it?
    Thanks for the tip Elysia, it makes perfect sense.

    So this is the revised code, and it works fine. Any more tips and suggestions are most welcome. I am pleasantly surprised as to how helpful these forums have been.


    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    bool IsEven (int nValue)
    
    {     
        return (nValue %2 == 0);    
    }
    
    void PrintResult (int nResult)
    
        {
        cout<< "Your result is:" << nResult << endl;
    
        }
    int GetUserInput ()
    
    {
        cout << "Enter an integer" << endl;
        int nValue1;
        cin >> nValue1; 
        return nValue1;
    }
    int main ()    
    {
        int nInput= GetUserInput ();
        
        int nResult= IsEven (nInput);
        
        PrintResult (nResult);
        
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I would be a little more consistent with the indentation and formatting:
    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    bool IsEven(int nValue)
    {
        return (nValue % 2 == 0);
    }
    
    void PrintResult(int nResult)
    {
        cout<< "Your result is:" << nResult << endl;
    }
    
    int GetUserInput()
    {
        cout << "Enter an integer" << endl;
        int nValue1;
        cin >> nValue1;
        return nValue1;
    }
    
    int main()
    {
        int nInput= GetUserInput();
        int nResult= IsEven(nInput);
        PrintResult(nResult);
    }
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modulus Operator
    By g8ortech14 in forum C Programming
    Replies: 6
    Last Post: 08-11-2010, 10:47 AM
  2. Help understanding the modulus operator
    By matrixx333 in forum C Programming
    Replies: 5
    Last Post: 10-01-2009, 06:06 AM
  3. Modulus Operator!
    By siLent0 in forum C Programming
    Replies: 7
    Last Post: 05-16-2008, 08:53 AM
  4. Using the Modulus Operator
    By tlove71 in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2007, 11:59 AM
  5. Modulus Operator
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 04-19-2002, 11:48 AM