little programming problem

Georgebobbilly

Failure of Custom titles
Adventurer
Joined
Jul 11, 2004
Messages
1,125
Location
oh... I'm around
Main Job
WhiteMage-Female
Secondary Job
Bard-Male
Third Job
BlackMage-Male
Linkshell
BigFunn
ok so I'm in a C++ programming class. On my most recent assignment I am encountering a problem that I need to know the solution to.

The Code
Spoiler:
Code:
//Georgebobbilly
//Chapter 8 Handout, Exercise 2
//Determine the largest number that evenly devides into a number


#include<iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
	//declare variables
	int max;
	int mod;
	int dev;

	//initial input for max
	cout << "Input the integer: ";
	cin >> max;

	//do statement
	do
	{
		//initial calculations
		dev = max / 2;
		mod = max % dev;

		//nested while statement
		while(mod != 0)
		{
			dev = dev -1;
			mod = max % dev;
		}//end while

		//output
		cout << endl << endl;
		cout << max << "'s highest number that can be evenly devided into it is " << dev;
		cout << endl;

		//Get new number for max
		cout << "Input integer (input 0 to end): ";
		cin >> max;
	}while(max >= 0);

	return 0;
}//end main

the problem is, when I try to enter 0 to end the program I get one of these(note had to find this with google images since I don't have access to photobucket at school):
Spoiler:
outlookerror1con.jpg


now after I end that, the program finishes fine, my teacher can't figure out what is wrong, we have tried redoing it in a new instance of Microsoft Visual Studio, got the same error at the same time. Now while she is fine with just letting me turn it in, I'm not, I want to know what is causing the problem... but as I can see nothing wrong I turn to anyone of you who may see something me and my teacher don't.
 
Your loop continues after you enter zero because your While evaluation is

while(max >= 0); // if max is greater than OR EQUAL TO 0 continue


Thus if you enter 0, max is equal to 0, so the loop continues.

These lines then set up the exception:

dev = max / 2; // dev = 0/2 = 0
mod = max % dev; // mod = 0 % 0

Zero Mod Zero is an undefined operation. From MSDN entry on the Mod Operator:

If the value of the right operand is zero, a System.DivideByZeroException exception is thrown.

Change the While evaluation to (max > 0) or (max != 0).
 
Last edited:
Your loop continues after you enter zero because your While evaluation is

while(max >= 0); // if max is greater than OR EQUAL TO 0 continue


Thus if you enter 0, max is equal to 0, so the loop continues. Then the first line of the next iteration divides by zero, which throws the exception:

dev = max / 2; // dev = 0/2 ==> DivideByZero Exception (OH SHI-)


Change the While evaluation to (max > 0) or (max != 0).

TY >.> I am now going to make fun of my teacher for not noticing that
 
Actually you should be making fun of me. Reread my post as I have edited it with the correct solution. 0/2 does not throw the exception - it's the later modulo operation which throws a divide by zero.

I should also advise you to read up on try {} catch(){} blocks; they can be very helpful in dealing with exceptions.
 
Last edited:
Back
Top