.exe has stopped working

2

Good night, I wrote a file in c ++ but it is no longer working, the code compiles everything right, but if I put it to run it appears the message that the program stopped working, if anyone can help me I will be very grateful. code below!

#include <stdio.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include <string>

#define K 9.0e09  // constante eletrica no vacuo

using namespace std;

int main(int argc,char **argv)
{

   int Q1 = atof(argv[1]);
   int Q2 = atof(argv[2]);
   double dist = atof(argv[3]); 
/*

   int Q1, Q2;
   double dist;

   cin >> Q1;
   cin >> Q2;
   cin >> dist;


  //char *OutputFileName=argv[4];
   int Q1 = 1;
   int Q2 = 2;
   double dist = 0.01;
*/

   ofstream Output1("Output.dat");  // valor da força elétrica (em Newtons)

   cout << "Forca eletrica = " << (K*Q1*Q2*pow(10,-12))/pow(dist,2) << endl;

   Output1 << Q1 << " " << Q2 << " " << dist << " " << (K*Q1*Q2*pow(10,-12))/pow(dist,2) << endl;

   return 0;

} //int main
/*-----------------------end of main program--------------------------*/
    
asked by anonymous 21.12.2018 / 02:39

1 answer

3

Igor ,

@ jefferson-quesado commented on checking argc to prevent 'strange errors' when running the program. It is critical that you do such a check if you expect some argument in the program call. In your case, you will have 4 arguments: the program name itself, Q1 , Q2 and dist .

  • If you invoke / execute the program without providing the arguments, it executes main() . Within main() , it executes the Q1 assignment but then stops executing because argv will attempt to access a non-reserved memory area when Q2 is attempting to store the argv value. The segmentation fault error appears.

  • If you invoke / execute the program by giving arguments partially, it executes main() . Within main() , it stops execution because argv will try to access a non-reserved memory area, and although at least Q1 passes the test, Q2 or dist will try to store the value nonexistent. The segmentation fault error appears.

  • In this case, if you had checked argc , you would probably have noticed that forgot your arguments when invoking the program . Your code is OK , running normally. The only way for it to provide a segmentation fault (or segmentation fault ) is actually not providing the necessary arguments when running your compiled.

    Error examples:

    g++ codigo.cpp -o meu_programa
    ./meu_programa  # sem argumentos
    Segmentation fault (core dumped)
    
    g++ codigo.cpp -o meu_programa
    ./meu_programa 20 # argumentos parciais
    Segmentation fault (core dumped)
    

    Correct example:

    g++ codigo.cpp -o meu_programa
    ./meu_programa 10 20 30 # todos os argumentos
    > Forca eletrica = 0.002
    

    You can use OnlineGDB to verify that it works normally

    Note that I have exemplified how I would be in Linux where I use g ++ to compile. In Windows you may be using some IDE or something. But the central idea is the same: you need the arguments!

        
    21.12.2018 / 06:10