1

I'm trying to write a simple c++ program that takes a number of miles as input and returns a conversion to kilometers. Here is my kilo.cpp:

 #include <iostream>
 using namespace std;

 int main()
 {
    double miles;
    double km_to_mile = 1.609;
    cout << "Please enter number of miles: ";
    cin >> miles;
    miles *= km_to_mile;
    cout << "That is " << miles << " kilometers.";
 }

The program compiles, but when I run it it crashes when it tries to output the double. The last line I get in console is "That is " and I get the Windows error message "kilo.exe has stopped working."

I've tried a few other code samples and whenever I try to use cout to output a double value, the program crashes with the same error. I assume this is some problem with my compiler (mingw on Windows 8.1), but I've tried reinstalling the compiler a few times now to no avail.

Danny Ayoub
  • 31
  • 1
  • 1
  • 4
  • 1
    I see nothing wrong with it, and indeed, it works fine for me. (On Linux.) – John Bollinger Mar 18 '15 at 14:53
  • I also tested it on Windows 7 using Visual studio 2013 and it works fine. Maybe you inserted a typo while entering the number or pressing the "Enter" key. – ElCapitaine Mar 18 '15 at 14:59
  • No problem over here as well ( gcc 4.8.1 DevC++, Windows 7 ) – Arun A S Mar 18 '15 at 15:06
  • I also see no error. A few things you could try: Use a debugger; check what happens, when you add a line termination (`cout << endl`); Test what happens, when you e.g. put every output into a separate line (`cout << "That is "; cout << miles; cout << " kilometers.";`); – MikeMB Mar 18 '15 at 15:12
  • 2
    See if http://stackoverflow.com/questions/20621639/stdendl-crashes-windows-8-compiled-using-mingw helps – MikeMB Mar 18 '15 at 15:18
  • @MikeMB, I tried changing the output format, no matter how I place the outputs the program crashes right when cout tries to print the double. However, when I use printf to print the double, it prints it out just fine. I'm in the process of installing gdb, I'll report back with any new info. – Danny Ayoub Mar 18 '15 at 15:46
  • 1
    Hi all. It seems my computer wasn't using the MinGW compiler, but instead some other (I assume out of date) compiler that was installed by another program. I determined where the other compiler using "where g++" in cmd and removed the location from the PATH. Upon recompiling the program works like it should – Danny Ayoub Mar 18 '15 at 16:20

0 Answers0