Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 1, 2006, 02:29 PM   #1
BobXRoberts
macrumors newbie
 
Join Date: Mar 2006
C++ Vagiaries with the modulo operator

Hello again! I'm about to ask what may be a silly question- but after half an hour's research the answer still eludes me...

The following code compiles well, of course:

#include <iostream>
using namespace std;
int main () {

int a;
int b;
int c;
cin >> a >> b;
c = a%b;
cout << c;

if (c=0)

{
cout << "Modulo is zero";}

return 0;
}

The code works fine- except when the modulo is zero(say when I divide eight by two), in which case the if test does not seem to detect that the modulo is zero, and refuses to print the string. Neither does it do so if I declare c as a float. This seems rather dodgy to me- a modulo is an integer, and not detecting that an integer is zero seems rather serious! I have the suspiscion tha I am doing something horribly wrong and not noticing...
BobXRoberts is offline   0 Reply With Quote
Old Apr 1, 2006, 02:37 PM   #2
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
I think you want if c == 0 (double equals sign). The way you have it, you're assigning 0 to c instead of comparing its value to 0.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Apr 1, 2006, 02:55 PM   #3
BobXRoberts
Thread Starter
macrumors newbie
 
Join Date: Mar 2006
Phweeee-oo. I've really outdone myself there. I noticed that about three minutes after I'd posted, rushed back here in the hope of catching myself, only to find that you helpful person had posted. Cheers! This is what comes from coding eight hours straight... (It's the middle of the night where I am...)

Moral: Be Awake Before Complaining About Faults in Major Programming Languages That Were Around Before you were Born.
BobXRoberts is offline   0 Reply With Quote
Old Apr 1, 2006, 06:52 PM   #4
MarkCollette
macrumors 65816
 
MarkCollette's Avatar
 
Join Date: Mar 2003
Location: Calgary, Canada
Quote:
Originally Posted by BobXRoberts
Phweeee-oo. I've really outdone myself there. I noticed that about three minutes after I'd posted, rushed back here in the hope of catching myself, only to find that you helpful person had posted. Cheers! This is what comes from coding eight hours straight... (It's the middle of the night where I am...)

Moral: Be Awake Before Complaining About Faults in Major Programming Languages That Were Around Before you were Born.
A good defensive programming technique is to always do:

Code:
if( 0 == var ) {}
instead of:

Code:
if( var == 0 ) {}
since the compiler will catch it as an error if you accidently do:

Code:
if( 0 = var ) {}
MarkCollette is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Does the Macbook come preinstalled with the latest OSX operating system? Laughingman13 MacBook 7 May 30, 2010 03:44 PM
is o2 the only operator with btopenzone access? musio iPhone 0 Nov 6, 2009 05:18 PM
Can the 3G operate like the 3GS? reoreilly iPhone 28 Sep 10, 2009 10:04 AM
Whats the latest Operating System that works with Mini vmac? Josh Kahane OS X 1 Jan 18, 2009 09:49 AM
Does a G5 startup normally and operate with the front proc fans disconnected? Counter Mac Basics and Help 6 Jun 4, 2005 06:14 PM


All times are GMT -5. The time now is 02:17 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC