"What will happen when I try to divide by zero (0) using C++?"
2 posters
:: C Languages :: C++ :: General Discussion
Page 1 of 1
"What will happen when I try to divide by zero (0) using C++?"
Hello guys! I would like to start off this thread with a small (slightly entertaining) story.
I was sitting in my algebra class earlier today, just paying attention, when, for no particular reason other than to reinforce it in our brains in a friendly reminder, my teacher made the following statement: "Always remember that whenever the divisor in a division problem is 0, the answer is undefined..." She then continued on with the lesson of the day. While I was paying attention to her, a small thought was brewing in the back of my mind: "What happens when you try to divide by 0 in a C++ program? Ahh...the program probably just crashes. Or does it?" Well, as soon as I got home, I threw together a small, fast, easy-to-write program to find out (and, compared to the tons of awesome things I could have done, the program is pretty crappy, but my goal was to get something that worked together quickly so that I could make this thread and then go do my homework, and was not to make something user-friendly).
I am assuming that you can somewhat figure out what the code does by yourself, so I am not going to bore you with explanations, however, if you do want an explanation of the code, just let me know, and I will happily provide one.
So anyway, I wrote the program, and then ran the program doing regular division, and of course, everything worked. I then tried to divide by 0. Instead of telling you what happened, I want you to watch this video, download the program, and then, using the attached screen capture software (click here to download it), record your screen as you run the program, and then attach the video to this post.
To watch my video, click here.
PLEASE NOTE: ALL OF THE ATTACHED FILES ARE IN ZIPPED FOLDERS, AND TO ACTUALLY USE THESE FILES, YOU WILL HAVE TO UNZIP THE FOLDER. I AM ASSUMING THAT YOU KNOW HOW TO DO THIS. IF YOU DO NOT, PLEASE LET ME KNOW IN A REPLY TO THIS THREAD, AND I WILL REPLY TO THIS THREAD WITH THE INSTRUCTIONS. (I WANT THAT REPLY TO LIST YOUR OPERATING SYSTEM SO I KNOW WHICH INSTRUCTIONS TO WRITE!)
I was sitting in my algebra class earlier today, just paying attention, when, for no particular reason other than to reinforce it in our brains in a friendly reminder, my teacher made the following statement: "Always remember that whenever the divisor in a division problem is 0, the answer is undefined..." She then continued on with the lesson of the day. While I was paying attention to her, a small thought was brewing in the back of my mind: "What happens when you try to divide by 0 in a C++ program? Ahh...the program probably just crashes. Or does it?" Well, as soon as I got home, I threw together a small, fast, easy-to-write program to find out (and, compared to the tons of awesome things I could have done, the program is pretty crappy, but my goal was to get something that worked together quickly so that I could make this thread and then go do my homework, and was not to make something user-friendly).
- Code:
// Simple Division -- this program performs simple division of two numbers,
// and outputs the results without rounding or providing the remainder, the
// program truncates the quotient without providing any fractional part
/// This program was written for the sole purpose of being used in the tutorial
/// which can be found on the Programming Language Community Forums
/// (www.programminglanguageforums.com), and should not be used for actual
/// division. No copyright protection secures this code from being modified,
/// redistributed, recompiled, etc., etc., etc. (You may use this code as you wish.)
/// Originally written by David B of the Programming Language Community Forums
/// (www.programminglanguageforums.com).
#include
#include
using namespace std;
int main()
{
// Declare Variables
int dividend = 1;
int divisor = 1;
int quotient = 1;
// Collect Info
cout << "Enter Dividend: ";
cin >> dividend;
cout << "\nEnter Divisor: ";
cin >> divisor;
// Do the math
quotient = dividend/divisor;
// Time to display the math!
system("CLS");
cout << dividend << " / " << divisor << " = " << quotient << "\n\n\n\n\n\n";
system("PAUSE");
return 0;
}
I am assuming that you can somewhat figure out what the code does by yourself, so I am not going to bore you with explanations, however, if you do want an explanation of the code, just let me know, and I will happily provide one.
So anyway, I wrote the program, and then ran the program doing regular division, and of course, everything worked. I then tried to divide by 0. Instead of telling you what happened, I want you to watch this video, download the program, and then, using the attached screen capture software (click here to download it), record your screen as you run the program, and then attach the video to this post.
To watch my video, click here.
PLEASE NOTE: ALL OF THE ATTACHED FILES ARE IN ZIPPED FOLDERS, AND TO ACTUALLY USE THESE FILES, YOU WILL HAVE TO UNZIP THE FOLDER. I AM ASSUMING THAT YOU KNOW HOW TO DO THIS. IF YOU DO NOT, PLEASE LET ME KNOW IN A REPLY TO THIS THREAD, AND I WILL REPLY TO THIS THREAD WITH THE INSTRUCTIONS. (I WANT THAT REPLY TO LIST YOUR OPERATING SYSTEM SO I KNOW WHICH INSTRUCTIONS TO WRITE!)
- Attachments
Re: "What will happen when I try to divide by zero (0) using C++?"
Loops are your friend; use them.
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: "What will happen when I try to divide by zero (0) using C++?"
-_____________-legolizard wrote:Loops are your friend; use them.
I think you mean if/else statements are your friend, as I don't see how loops would help you in this situation.
Re: "What will happen when I try to divide by zero (0) using C++?"
Yes if/else statements would be nice. Or even better a switch statement, but then you couldn't show how it can't divide by 0. :/
It is kind of sad that you don't see how loops would be useful. -.- Rather than having to keep running the program you could just add a loop and cycle through until the user wants to stop. .__.
It is kind of sad that you don't see how loops would be useful. -.- Rather than having to keep running the program you could just add a loop and cycle through until the user wants to stop. .__.
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: "What will happen when I try to divide by zero (0) using C++?"
I saw how loops could be useful in the situation, but I did not feel like taking the time to program it. You have to understand that on the weekdays, I am rather loaded down with homework now, and don't want to take the time to program correctly. Loops can be made quickly, but I also needed to take a considerable amount of time to write the thread, so that sucked out some of my homework time as well.legolizard wrote:Yes if/else statements would be nice. Or even better a switch statement, but then you couldn't show how it can't divide by 0. :/
It is kind of sad that you don't see how loops would be useful. -.- Rather than having to keep running the program you could just add a loop and cycle through until the user wants to stop. .__.
P.S.: I also wanted to make sure that no potential bugs in the loop (it sound's stupid, but you've got to admit that it's possible) cause the outcome of the division each time to be skewed by some runtime error.
:: C Languages :: C++ :: General Discussion
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|