Variable utlisé mais non initialisé
Bonjour la communauté,
Je vous préviens je suis un débutant en programmation et dans le forum aussi !
J'ai un petit problème sur un programme que je dois faire en C++ pour les cours ! Il s'agit d'un programme devant traiter des datas, j'ai fais le script, et quand je génère la solution une première fois il me dit que la génération a réussi cependant, quand j’exécute le débogage il me remonte une erreur et quand je régénère mon programme il me dit "========== Génération : 0 a réussi, 0 a échoué, 1 mis à jour, 0 a été ignoré ==========.
L'erreur remontée lors de l’exécution sans débogage est " la variable prod est utilisé sans avoir été initialisé", je me demande si tel est le cas, pourquoi il m'a pas remonté cette erreur lors de la génération ? Je suis pourtant assez sûre du code et je suis surpris du message.
EN vous remerciant du temps que vous passerez pour me répondre !
Voici le code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| #include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <sstream>
#include <vector>
using namespace std;
class Product
{
// Access specifier
public:
// Data Members
int num_piece;
int ref;
int quantity;
};
int main() {
// File pointer reader
fstream fin;
fin.open("log.csv", ios::in);
// File pointer writer
std::ofstream fout;
fout.open("log-copie.csv");
// Read the Data from the file
std::string line, word, temp;
// vector of products to group products with the same num_piece
vector<Product> products;
// Current product (by line)
Product prod;
// first num_piece
int start = 1;
// boolean indicating the end of the file
bool end;
// to avoid processing the header
getline(fin, temp);
while (!fin.eof()) {
// read an entire row and
// store it in a string variable 'line'
getline(fin, line);
// check that we still have lines with content
if (line.empty()) {
end = true;
}
// used for breaking words
std::stringstream s;
s.str(line);
// read every column data of a row and
// store it in a string variable, 'word'
vector<int> infos;
while (getline(s, word, ';')) {
infos.push_back(stoi(word));
}
// after reading the product infos we create an object with those values
if (infos.size() == 3) {
prod.num_piece = infos[0];
prod.ref = infos[1];
prod.quantity = infos[2];
infos.clear();
}
// if the num_piece of the prod change , then we save lines of the corresponding num_piece into the file
if (prod.num_piece > start || end == true) {
auto iter = products.begin();
for (; iter != products.end(); iter++)
{
fout << (*iter).num_piece << ";" << (*iter).ref << ";" << (*iter).quantity << endl;
}
// we move to the next num_piece
start = prod.num_piece;
// clear the products vector
products.clear();
}
// we put all products with the same num_piece into a vector
// we keep only the (ERgt)max quantity for each reference value
auto iter = products.begin();
int found = 0;
for (; iter != products.end(); iter++)
{
if ((*iter).ref == prod.ref) {
found = 1;
if (prod.quantity > (*iter).quantity) {
(*iter).quantity = prod.quantity;
}
}
}
if (not(found)) products.push_back(prod);
}
// save kept lines
fout.close();
} |