I wrote this program which takes the value from the user and makes it into a even number if the number entered was odd, if not then print out a message to say they need to enter a odd number. But the program continues to return a value from the makeEven function even though I just want it to print out the message saying enter an odd number.

I'm relatively new to C++ and just cant get my head around where it is getting the value it happens to print out.

#include <iostream>

using namespace std;

int makeEven(int x)
{ 
	if ( x % 2 != 0 ) 
		return x-1;
	else
		cout<<"Please enter an odd number!";
	goto /*line*/ 18;
}

int main()
{
	int x;

	cout << "Please enter an integer: ";
	cin >> x;
	
	cout << "Your number is now: " << makeEven(x) <<endl; 
	
};

Try This:

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
bool Input(false);
int N;

do {
cout << "Enter an odd number: ";
cin >> N;

if (N%2 != 0)
{
cout << "Changed from Odd to Even ,Result: " << N-1;
Input = true;
}
else
{
cout << "Wrong Input ,Enter an Odd number\n";
}

} while (Input == false);

getch();
return 0;
}

It Should work ,because i tryed it before Posting.
Regards,,
:icon_mrgreen::icon_mrgreen::icon_mrgreen: Kimo

thanks for that!
works perfectly although could you explain the use of conio.h? Sorry its just I would like to fully understand it. Also I would like to keep in in seperate functions

I'v also been told I need to complete the program using referencing, which I do understand but I couldnt get working?

Sorry to be a pain

OK, well for starters you don't want a goto in makeEven, you want to return something....
Also in main you'd need to assign the value of x to the value returned by a call to makeEven(x).

That gives you this:

#include <iostream>

using namespace std;

int makeEven(int x)
{ 
	if ( x % 2 != 0 ) 
		return x-1;
	else
		cout<<"Please enter an odd number!";

	return 0; // must return something here
}

int main()
{
	int x;

	cout << "Please enter an integer: ";
	cin >> x;

        // call makeEven and assign the returned value to x
	cout << "Your number is now: " << makeEven(x) <<endl; 
	
}

Although, as you've correctly pointed out. It would make sense to repeatedly prompt the user to enter an odd integer until an odd integer is entered before calling makeEven. But then makeEven would be pretty much redundant as it would only need one line of code:

#include <iostream>

using namespace std;

int makeEven(int x)
{ 
	return --x;
}

int main()
{
	int x;
	bool isOdd=false;
	while(!isOdd)
	{
		cout << "Please enter an integer: ";
		cin >> x;
		cin.ignore(); // ignore the newline from the previous input
		if(x%2!=0)
			isOdd=true;
		else
			cout << "Please enter an odd number!" << endl;
	}
	
	cout << "Your number is now: " << makeEven(x) <<endl;
 	cin.get(); // press return to continue...
	
}

Which is pretty pointless, but perhaps you could put the entire input and process stage into one function, so your code looks like this:

#include <iostream>

using namespace std;

int makeEven()
{
	int x;
	bool isOdd=false;

	while(!isOdd)
	{
		cout << "Please enter an odd integer: ";
		cin >> x;
		cin.ignore(); // ignore the newline..
		if(x%2!=0)
		{
			return --x;
		}
		else 
			cout << "The number must be odd..Try again!" << endl;
	}

	return 0; 
// NOTE: technically the above return is unreachable, but some 
// compilers may complain if no return is made here!
}


int main()
{
	int x = makeEven();
	cout << "Your value is now: " << x << endl << "Press return to continue...";
	cin.get();
	
}

But it's just as simple to keep it all in main:

#include <iostream>

using namespace std;

int main()
{
	int x;
	bool isOdd=false;
	while(!isOdd)
	{
		cout << "Please enter an integer: ";
		cin >> x;
		cin.ignore(); // ignore the newline from the previous input
		if(x%2!=0)
			isOdd=true;
		else
			cout << "Please enter an odd number!" << endl;
	}
	
	cout << "Your number is now: " << --x <<endl;
 	cin.get(); // press return to continue...
	
}

There ya go, welcome to C++, where there are always several different ways of doing exactly the same thing!

p.s. don't worry about Kim0000's post too much. conio.h is an older c header file and is deprecated in C++...There is an equivalent C++ header to access the same functionality as conio.h, but offhand I can't remember it!
Cheers for now,
Jas.

oh ,thx JasonHippy
I didn't know that it was a C header file, Hope u'll remember the
C++ header file which do the same function

getch();

But it's not so bad to use conio.h ,btw im beginner :icon_cheesygrin:

>goto /*line*/ 18;
goto is restricted to a single function, you can't jump between functions with it.

>conio.h is an older c header file and is deprecated in C++...
Please don't use deprecated to mean anything except "standard, but not recommended because it might not be standard in the next revision". conio.h was never standard, either in C or C++.

>There is an equivalent C++ header to access the same functionality as conio.h
No, conio.h provides functionality that isn't portable, and is thus unavailable in the standard library. However, you can get close for things like getch. cin.get can "pause" the program just as well[1], with the added restriction of the user having to press the enter key:

#include <iostream>

int main()
{
  // ...

  std::cout<<"Press [Enter] to continue . . .";
  std::cin.get();
}

>But it's not so bad to use conio.h
Yes, it is. You just don't realize it because you're a beginner. :icon_rolleyes:

[1] For the most part. There is a sticky thread that discusses further issues if the input stream needs to be flushed.

commented: It's a fair cop guv! I put my hands up to points 2 and 3...I was wrong! ;) +1

I'v also been told I need to complete the program using referencing, which I do understand but I couldnt get working?

OK, try this on for size, this uses references as parameters to the two functions..

#include <iostream>

using namespace std;

/////////////////////////////////////////////////////////////////////
/// Get a value from the user and store it in the passed in reference
/////////////////////////////////////////////////////////////////////
/// \returns<void>
/// \param<num> reference to an int
/////////////////////////////////////////////////////////////////////
void getNumber(int &num)
{
	cout << "Enter a number: ";
	cin >> num;
	cin.ignore();
}

/////////////////////////////////////////////////////////////////////
/// Make input value (passed in reference) even
/////////////////////////////////////////////////////////////////////
/// \returns<void>
/// \param<number> reference to an int
/////////////////////////////////////////////////////////////////////
void makeEven(int &number)
{
	// if number entered was not odd, get a new value
	while(number%2!=1)
	{
		cout << "Number must be odd!!" << endl;
		getNumber(number);
	}
	// number is odd so decrement it
	--number;
}

 
int main()
{
	int x = 0;
	// get initial value for x
	getNumber(x);

	// make it even
	makeEven(x);

	// output the value
	cout << "Number is now: " << x << endl;

	// wait for user to press enter...
	cin.get();
}

So looking at main, we're creating an int variable called x.
We pass x into getNumber() as a reference.
getNumber prompts the user to enter a value which is stored in x.

Back in main we call makeEven and pass x as a reference.
Inside makeEven, if the passed-in number is even, getNumber is called until the input value is odd.
Once we have an odd number, the number is decremented.

By the time we get back into main again, we output the value of x, which will be even.

Passing by reference (and also by passing pointers) allows us to pass our original variable into functions and modify its contents.

Whereas when passing by value (which we were doing in my previous examples), the function makes a copy of the original variable and only the copy is modified inside the function. Changes made to the copy of the variable inside the function are not reflected in the original variable outside of the function...If you catch my drift.

Here is an older thread where I've discussed the different methods of passing parameters to functions in a bit more detail:
http://www.daniweb.com/forums/post1035957.html#post1035957

Cheers for now,
Jas.

Ah, hello again Narue.
Once more you've quite correctly corrected me on my half-witted absent-minded inaccuracy!

>conio.h is an older c header file and is deprecated in C++...
Please don't use deprecated to mean anything except "standard, but not recommended because it might not be standard in the next revision". conio.h was never standard, either in C or C++.

Oh yes, of course {memory kicking in..albeit slowly!} I forgot about that...conio.h was the dos specific implementation for various console related IO things wasn't it? kinda like curses in *nix. Sorry, it's been quite a long time since I did any pure c stuff! Either way it's bad to use it in C++, so I was at least half right, but perhaps I worded it badly!!

>There is an equivalent C++ header to access the same functionality as conio.h
No, conio.h provides functionality that isn't portable, and is thus unavailable in the standard library. However, you can get close for things like getch. cin.get can "pause" the program just as well[1], with the added restriction of the user having to press the enter key:

No, you're right there isn't....I think I was thinking of stdlib.h and <cstdlib> or something! I'd put it down to a blonde moment on my part. And yes, I am blonde...Or at least I would be if I had any hair left! heh heh! ;)

Anyway, thanks for the correction. Consider me well and truly spanked!
Cheers for now,
Jas.

I wrote this program which takes the value from the user and makes it into a even number if the number entered was odd, if not then print out a message to say they need to enter a odd number. But the program continues to return a value from the makeEven function even though I just want it to print out the message saying enter an odd number.

I'm relatively new to C++ and just cant get my head around where it is getting the value it happens to print out.

#include <iostream>

using namespace std;

void makeEven(int x)
{ 
	if ( x % 2 != 0 ) 
		return x-1;
	else
		cout<<"Please enter an odd number!";
	goto /*line*/ 18;
}

void main()
{
	int x;

	cout << "Please enter an integer: ";
	cin >> x;
	
	cout << "Your number is now: " << makeEven(x) <<endl; 
	
};

use The Void main Instead Of Int main bz when u Use the Int main it will re turn a value ........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.