0
#include <cstdlib>
#include <crime>
#include <stream>
#include <cmath>
#include <iostream>

using namespace std;
int Kills;
int Deaths;


int main()
{
cout << "Please Enter The Amount Of Kills You Have: ";
cin >> Kills;
cout << "Please Enter The Amount Of Deaths You Have: ";
cin >> Deaths;

float answer = Kills / Deaths;

cout << "Your KD Is: " << answer;
//creating a .txt file 
ofstream pctalk;
pctalk.open("KDA Tracker.txt", ios::app);

//actually logging
pctalk << "Kills: " << Kills << " | " << "Deaths: " << Deaths << " | " << "KD Ratio:  " << answer << 
"\n";
//closing our file
pctalk.close();
return 0;
}

When code is run inside visual studio console does not exit right away. but when ran from the solution folder aka the exe it closes right after It inputs my deaths. Why is this happening?

  • I assume you mean the "Press any key to exit" prompt? That's a Visual Studio specific thing, when you run your program otherwise there's few places you really want your program to stop and ask for input. If you really want it you could easily implement it yourself, by just asking and [reading a line of input](https://en.cppreference.com/w/cpp/string/basic_string/getline) that you just throw away. – Some programmer dude Apr 20 '20 at 03:20
  • 1
    Not related to your question, but this line `float answer = Kills / Deaths;` is unlikely to do what you want. `Kills` and `Deaths` are both `int`, in integer math 9/12 is 0. You probably want a fractional answer, so you need a cast there; e.g.: `float answer = static_cast(Kills) / Deaths;` – nick Apr 20 '20 at 03:25
  • Very close to [this question](https://stackoverflow.com/q/48750766/10957435), though it doesn't explain the behavior. I believe there is a question that does somewhere. –  Apr 20 '20 at 04:03

1 Answers1

-2

Please put following statement just before return statement.

system("pause");
Haris Shafiq
  • 513
  • 3
  • 5