My program has stopped working every time I input 0

My program has stopped working every time I input 0 on the divisor. How do I fix this this?

My output looks like this:
Enter a number : 12
Enter a divisor : 0


After I type in 0, my program shuts down or stopped working immediately.The cout doesn't even show up.

1
2
  	else if (divisor = 0 ){
	cout <<"\n" << " Error: cannot divide by zero." << endl;}
Last edited on
else if (divisor == 0 )

You are assigning (=) 0 (zero) to divisor, that will always evaluate to true.

The problem might actually be somewhere else in the code that you didn't post.

Ooops, brain fart! assigning zero will always evaluate to false. Zero equates to false.
Last edited on
@furry guy thanks! I tried it but the program still shuts down. Here's the rest of my codes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

int main (){
	
	int number, divisor, qoutient, remainder;
	
	cout<<" Please enter a number: ";
	cin>>number;
	
	cout<<" Please enter a divisor: ";
	cin>>divisor;
	
	qoutient = number / divisor;
	remainder = number % divisor;
	
	if (remainder){
	cout << "\n" << " " << divisor << " does not divide the number " << number << "." << endl;
	cout << " " << number <<"/"<< divisor << "=" << qoutient << "." << remainder << endl;
	}
	
	else if (qoutient>1){
	cout << "\n" << " " << divisor << " divides the number " << number << "." << endl;
	cout << " " << number <<"/"<< divisor << "=" << qoutient << endl;
	}
	
	else if ( number < 0 ){
	cout <<"\n" << " Enter a positive integer only." << endl;
	}
	
	else if (divisor == 0 ){
	cout <<"\n" << " Error: cannot divide by zero." << endl;	
	}
	
	
	return 0;
}
Last edited on
Your problem is how your program is structured. You are doing the division BEFORE you check to see if the divisor is zero. *BLAM!* *CRASH!*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

int main()
{
   int number, divisor;

   std::cout << "Please enter a number: ";
   std::cin >> number;

   std::cout << "Please enter a divisor: ";
   std::cin >> divisor;

   if (divisor == 0)
   {
      std::cout << "\nError: cannot divide by zero.\n";
   }
   else if (number < 0)
   {
      std::cout << "\nEnter a positive integer only.\n";
   }
   else
   {
      int qoutient  { number / divisor };
      int remainder { number % divisor };

      if (remainder)
      {
         std::cout << "\n" << divisor << " does not divide the number " << number << " without a remainder.\n";
         std::cout << number << " / " << divisor << " = " << qoutient << "." << remainder << '\n';
      }
      else
      {
         std::cout << '\n' << divisor << " divides the number " << number << ".\n";
         std::cout << number << " / " << divisor << " = " << qoutient << '\n';
      }
   }
}

Please enter a number: 12
Please enter a divisor: 0

Error: cannot divide by zero.
Please enter a number: -5
Please enter a divisor: 5

Enter a positive integer only.
Please enter a number: 12
Please enter a divisor: 5

5 does not divide the number 12 without a remainder.
12 / 5 = 2.2
Please enter a number: 12
Please enter a divisor: 4

4 divides the number 12.
12 / 4 = 3

If the entered number is smaller than the divisor and not zero that condition caught by the remainder if.
@FURRY GUY WOW! TAHNK YOU SO MUCH FOR THE HELP!!!! I REALLY APPRECIATE IT! MY PROGRAMMING TEACHER DID NOT TEACH US THE BASICS SO YEAH I HAD TO LEARN ON MY OWN! I LOVE YOU DUDE!! U SAVED MY ASS YOU HAVE NO IDEA HOW HAPPY I AM RIGHT NOW!!!!!
Last edited on
If you want a program that ALWAYS crashes no matter what numbers you enter just change the equality check (==) in my source on line 13 to assignment (=). divisor will always be zero and so the division by zero crashes the program.

That is why I always write my if checks:

if (constant/literal == variable)

Try to assign a variable's value to a constant/literal and the compile will fail.

There could be one more check before you do the math:

if the divisor < 0.

Change line 17 to else if (number < 0 || divisor < 0)

Now both entered numbers must be positive.
Last edited on
Topic archived. No new replies allowed.