.exe has stopped working

Asked

Viewed 126 times

2

Good evening, I wrote a file in c++ but it is no longer working,the code compiles everything right, but if I put to run 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--------------------------*/
  • 1

    What are you calling the program? Not checking argc, then possibly it is that you are not respecting the CLI that you have defined

1 answer

3

Igor,

The @Jefferson-quesado commented on checking the argc to prevent 'strange errors' when running the program. It is essential to make such a check if you expect any arguments in the program call. In your case, you will have 4 arguments: the program name itself, Q1, Q2 and dist.

  1. If you invoke/execute the program without providing the arguments, it executes main(). Within main(), it performs the assignment of Q1 but then stops the execution because argv will attempt to access a memory area not reserved for it when Q2 is trying to store the value of argv. The segmentation fault error arises.

  2. If you invoke/execute the program by providing the arguments partially, it executes main(). Within main(), he interrupts the execution because argv will attempt to access a memory area not reserved for it and, although at least Q1 pass the test, Q2 or dist will attempt to store the missing value. The segmentation failure error.

In this case, if you had checked argc, probably would have noticed that forgot the arguments when invoking the program. For your code is OK, running normally. The only way it provides a segmentation fault (or Segmentation fault) is really not providing the necessary arguments while running your compiled.

Examples with error:

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 the Onlinegdb to verify that it works normally


Note that I exemplified how you would be in Linux in what use the g++ to compile. No Windows may be using some IDE or something like that. But the central idea is the same: it needs the arguments!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.