Jump to content

Windows Error while running the program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
**edited**
i found my thinking in this program is totally wrong (but i still want to know what cause this run error)






---------------

Hey guys, so I was drafting a simple program on miles per gallon.
Disregard this draft, I had a problem when I tried to run the program.

I used Dev c++, and I can compiled the program.

But when I press ctrl + f10 to run the program, it said gallon.exe has encountered a problem....

just like the screenshot i took.

what is the problem? here is my code.
// Miles per gallon program
// Written in C++

#include <iostream>
using namespace std;

int main()
    {
    
    // assign the type
    int gallon,   // gallon used
        miles,    // miles used
        totalMiles,  // total miles driven
        totalGallon; // total gallon used
    float mpg;       // miles per gallon
    
    // initialization
    totalMiles = 0;
    totalGallon = 0;
    mpg = 0;
    
    //formula
    mpg = totalMiles / totalGallon;
    
    // let us get a welcome message first
    cout << "Welcome to Driver Mile Per Gallon Program! A simple and an easy use program to find out mile per gallon for your tankful!\n" <<endl;
    cout << "Please follow the guideline to operate the program properly:\n" <<endl;
    cout << "[1] Enter any positive value for each input.\n" <<endl;
    cout << "[2] Enter -1 to end this program and will print the mile per gallon result at the end of the program.\n" <<endl;
    
    // now let us begin our program
    while (miles && gallon != -1) {
          cout << "Enter the mile driven:\n";
          cin >> miles;
          cout << "Enter the gallon used:\n";
          cin >> gallon;
          totalMiles = totalMiles + miles;
          totalGallon = totalGallon + gallon;
    }
    
    // how to find the mile per gallon
    
    if (miles && gallon != -1) {
    cout << "Your Mile Per Gallon is: " << mpg << "\n";
    }
    else
        cout << "You did not enter any value!\n" <<endl;
    return 0;
    }

Attached Files

  • Attached File  01.JPG   139.3K   19 downloads


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
mpg = totalMiles / totalGallon;
0/0 will throw a non-numeric result, crashing your program when you try to assign the result to mpg.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
icic (disregard my wrong thinking program here), if i put mpg = totalMiles / totalGallon in the end of the program, in theory, should it work?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Yes. I would put a guard against totalGallon != 0, though.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog