[prog] more C++

Andrea Landaker qirien at icecavern.net
Sat Dec 13 15:04:41 EST 2003


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

Well, it sounds like you have the right idea -- any even number modulo 2 will 
give 0, while any odd number modulo 2 will give 1.  So that's a pretty easy 
way of checking.  But your program seems to be doing something 
different . . . I don't see anyplace where you're doing "% 2".

There are a number of problems with your code, though . . . there are a lot of 
semicolons missing, and I'm not sure what you're trying to do with "x=even" 
and "y=odd" . . . At the time the program is running, it tries to assign the 
variable x to have the value even . . . but it doesn't know anything about a 
variable called even.  Since you haven't assigned x and y any values, the 
computer can't do "x % y" in the if statement -- but you wanted to modulo by 
2, right?  Not y?  Unless you meant to assign y the value of 2 . . . but you 
wanted to check the number the user entered, which is integer1, right?

I think you can figure this out; it seems like you know what you want to do.  
You might want to write some pseudocode on a piece of paper that's similar to 
what you described, and then translate that into C++ code.  Good luck!

-- 
Andrea Landaker
http://www.icecavern.net/~qirien/



More information about the Programming mailing list