dimanche 5 novembre 2017

*** stack smashing detected ***: ./a.out terminated

I wrote this code, and i need some help!

as you can see from pictures below, i'm having difficulties figuring out how to fix this issues. i'm submitting my code and the results are as follows

picture are in the link:

![pic1](https://ibb.co/k8koHb

![pic2](https://ibb.co/ht3uOG

![pic3](https://ibb.co/nE7oiG

![pic4](https://ibb.co/hYKZOG

.

Compile command:

g++ lesson7part2.cpp -Wall -Wextra -Wuninitialized -pedantic-errors -Wconversion -o a.out

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

int main() {

   ofstream outFile;
   string outFileName = "invalid-numbers.txt";


char filename[]="";

cin>>filename;


ifstream in(filename);


    if (!in.good())

{

cout<<"File " << "\""<< filename <<"\"" << " could not be opened" <<endl;
exit(0);
}

   outFile.open(outFileName.c_str()); //open file
   if (!outFile) { //check if u opened file properly or not
   cout << "Error opening " << outFileName << " " << endl;
   return 1;

   }

   double inNumber = 0;
   double valTotal = 0;
   double countValNum = 0;
   double count =0;
   while (in >> inNumber) {

       if (inNumber >= 0 && inNumber <= 110) {
           valTotal += inNumber;
           countValNum++;
       } else {
           outFile << inNumber << fixed << setprecision(2) << endl;
       }
       count++;
   }
   cout << "Reading from file " << "\""<< filename <<"\"" << endl;
   cout << "Total values: " << count << endl;
   cout << "Invalid values: " << (count - countValNum) << endl;
   cout << "Valid values: " << countValNum << endl;
   cout << "Average of valid values: " << fixed << setprecision(2) << (valTotal / countValNum) << endl;

outFile.close();
in.close();

   return 0;

}

HERE:

so as u see in pictures I need to get rid of the

"* stack smashing detected *: ./a.out terminated" ERROR.

and fix those number issues.

. . . . . . THIS IS EXAMPLE ON HOW ITS SUPPOSED TO LOOK LIKE

Here is an example of a working program:

Assume the file name read in from cin is:

input.txt

and that input.txt contains:

-12

0

98.5

100

105.5

93.5

88

75

-3

111

89

-12

Your program would output the following:

Reading from file "input.txt"

Total values: 12

Invalid values: 4

Valid values: 8

Average of valid values: 81.19

The contents written out to file invalid-numbers.txt (if they are less than 0 ) are:

-12

-3

111

-12

Aucun commentaire:

Enregistrer un commentaire