[prog] more C++

Dominik Schramm dominik.schramm at gmxpro.net
Sat Dec 13 22:46:43 EST 2003


Hi,

On 12/13/2003 09:28 PM, wolf wrote:

 > I'm trying to work on another sample for C++, I was trying to make a
 > little program that would say if the number you give it
 > is odd or even. Sounded like a simple idea : ) It seems to me it should
 > you the modulus as opposed to just dividing the sample number by 2.

Correct. You made the first step towards a working program.

 > However so far, nothing I've tried seems to work.
 > Am I way off target or have I made an obvious
 > error? Thank you : )

The obvious error to me is that what you just stated
isn't in your program (you didn't implement your specification!).
;-) Where is the "2" in your program?

 > #include <iostream>
 > int main()
 > {
 >      int integer1; // I assume the missing ; is a typo
 >      int x=even;
 >      int y=odd; // same here
 >
 >      std::cout << "Enter a number\n";
 >      std::cin >> integer1;
 >
 >      if(x % y )
 >      { even = integer1; }
 >      else
 >      { odd = integer1; }
 >      // one closing brace deleted!
 >
 >       std::cout << "The number is " << odd << even << std::endl;
 >
 >       return 0;
 > }

"even" is undefined, this makes no sense to C++ because it
doesn't know what to assign to x. same for "odd".

What the program actually does:
The program checks if the remainder of x after dividing by y
(both undefined so far!) is zero. If so, "even" is assigned the
value of the input integer1 and "odd" remains undefined.
If not, "odd" is assigned the value of integer1 and "even" remains
undefined.

Then both odd and even (two integers, one of them undefined!)
are output.

This program should not compile. Does is?
If not, what is the message the compiler gives you?

Just some pointers, don't want to give away the solution...

hope this helps
regards,
dominik





More information about the Programming mailing list