Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,126 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,777 people online right now. Registration is fast and FREE... Join Now!




bool recieves a parameter

 
Reply to this topicStart new topic

bool recieves a parameter

Simply
16 May, 2007 - 11:15 AM
Post #1

New D.I.C Head
*

Joined: 2 Apr, 2007
Posts: 22


My Contributions
i am trying to write a function with return type bool, namely isOdd. It receives a parameter of type int and determines whether it is odd or not. If it is odd the value true should be returned, and if it is even the value false should be returned.

This is my work thus far but its not working.

#include <iostream>
using namespace std;

int main( )
{
bool isOdd ( int i)
{
if (int i % 2 == 0)
return true;
else
return false;
}

int i;
cout << "Type in an integer: " << endl;
cin >> i;


if (isOdd(i))
cout << "The integer " << i << " is odd" << endl;
else
cout << "The integer " << i << " is not odd" << endl;
cout << endl << endl;

return 0;
}

Errors I am receiving:

a function-definition is not allowed here before '{' token
expected `,' or `;' before '{' token
`isOdd' undeclared (first use this function)
[Build Error] (Each undeclared identifier is reported only once for each function it appears in.)

This post has been edited by Simply: 16 May, 2007 - 11:16 AM
User is offlineProfile CardPM
+Quote Post

spullen
RE: Bool Recieves A Parameter
16 May, 2007 - 12:30 PM
Post #2

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
I think you need to declare the method outside the main method so:
CODE

public bool isOdd(int i){
          if(i % 2 == 0)
             return true;
          else
             return false;
}

public static void main(String[] args){
         for(int i = 0; i <= 10; i++){
              if(!isOdd(i))
                System.out.println("ODD");
         }
}

You have to watch what you're testing, and how an if statement works b/c the way you are doing it is thinking about it in an opposite way. You could change the return values in isOdd and that might fix your code.
Hope that helps

This post has been edited by spullen: 16 May, 2007 - 12:32 PM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Bool Recieves A Parameter
16 May, 2007 - 02:44 PM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Agreed...you are returning true when the result of the modulus operation with 2 is zero...meaning that you have an even number. You want to return false in that case.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:18PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month