Would you like to react to this message? Create an account in a few clicks or log in to continue.

"What will happen when I try to divide by zero (0) using C++?"

2 posters

Go down

"What will happen when I try to divide by zero (0) using C++?" Empty "What will happen when I try to divide by zero (0) using C++?"

Post  David B Thu Mar 22, 2012 7:50 pm

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).

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
"What will happen when I try to divide by zero (0) using C++?" Attachment
Simple Division.zip Contained within this zipped folder are the source files for the project. (THERE IS NO NEED TO DOWNLOAD THIS FOLDER UNLESS YOU USE CODE::BLOCKS AS YOUR COMPILER! ALL OTHER COMPILERS WILL NOT BE ABLE TO READ THE ADDITIONAL SOURCE FILES!)You don't have permission to download attachments.(1.1 Mb) Downloaded 0 times
"What will happen when I try to divide by zero (0) using C++?" Attachment
Simple Division Program (no source files included).zip This zipped folder contains the executible file that the code provided in the thread creates. NO SOURCE FILES ARE INCLUDED IN THIS ZIPPED FOLDER!You don't have permission to download attachments.(1.1 Mb) Downloaded 0 times
"What will happen when I try to divide by zero (0) using C++?" Attachment
Simple Division C++ Code.zip This zipped folder contains the .cpp file that has the code that creates the, "Simple Division.exe" program. NO SOURCE FILES ARE INCLUDED IN THIS FOLDER, AND THE PROGRAM IN ITS COMPILED FORM IS NOT AVAILABLE IN THIS FOLDER!!!!!You don't have permission to download attachments.(1 Kb) Downloaded 0 times
"What will happen when I try to divide by zero (0) using C++?" Attachment
Simple Division (Code and Executable).zip This zipped foler contains both the original program code and the executable that is created when the code is compiled. NO SOURCE FILES ARE INCLUDED IN THIS FOLDER!!!!!You don't have permission to download attachments.(1.1 Mb) Downloaded 0 times
"What will happen when I try to divide by zero (0) using C++?" Attachment
CamStudio.zip This zipped folder contains the install file for CamStudio, the recording software that I want you to use to make your video.You don't have permission to download attachments.(4.2 Mb) Downloaded 0 times
"What will happen when I try to divide by zero (0) using C++?" Attachment
Dividing by 0 Using C++.zip This zipped folder contains the video that I recorded of me running the program! I WANT YOU TO WATCH IT!!!!!You don't have permission to download attachments.(1.1 Mb) Downloaded 2 times
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

"What will happen when I try to divide by zero (0) using C++?" Empty Re: "What will happen when I try to divide by zero (0) using C++?"

Post  legolizard Fri Mar 23, 2012 3:32 pm

Loops are your friend; use them.
legolizard
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

"What will happen when I try to divide by zero (0) using C++?" Empty Re: "What will happen when I try to divide by zero (0) using C++?"

Post  David B Fri Mar 23, 2012 8:54 pm

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.
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

"What will happen when I try to divide by zero (0) using C++?" Empty Re: "What will happen when I try to divide by zero (0) using C++?"

Post  legolizard Fri Mar 23, 2012 11:11 pm

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. .__.
legolizard
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

"What will happen when I try to divide by zero (0) using C++?" Empty Re: "What will happen when I try to divide by zero (0) using C++?"

Post  David B Sat Mar 24, 2012 7:24 pm

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. .__.
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.

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.
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

"What will happen when I try to divide by zero (0) using C++?" Empty Re: "What will happen when I try to divide by zero (0) using C++?"

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum