Problem with my code

I have a problem with my code, the file won't open.



#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <iterator>
using namespace std;




int main() {


int nbrpage = 1, nbrLigneD, nbrligne = 1, nbrligneTot = 0, trouve = 0, nbrmottot;
map <string, string> index;
map <string, string> ::iterator itr;
string ligne, lignetexte, mot, mot1, mot2, mot3, mot4, mot5, mot6, mot7, mott, motF;


cout << "Entrez le nombre de ligne par page"; cin >> nbrLigneD;





ifstream fichier2("clef.txt", ios::in | ios::app);
if (fichier2.is_open()) {


while (getline(fichier2, mot1)) {
mot = mot1;
mot2 = mot1;
mot3 = mot1;
mot4 = mot1;
mot5 = mot1;
mot6 = mot1;
mot7 = mot1;
mot1 = " " + mot1 + " ";
mot2 = "'" + mot2 + " ";
mot3 = " " + mot3 + ".";
mot4 = " " + mot4 + ",";
mot5 = "'" + mot5 + ",";
mot6 = " " + mot6 + "!";
mot7 = " " + mot7 + "?";

nbrmottot = 1;
nbrligneTot = 0;
nbrligne = 0;
nbrpage = 1;

ifstream fichier("docum.txt", ios::in | ios::app);
while (getline(fichier, ligne)) {

nbrligneTot++;
nbrligne++;




if ((std::string::npos != ligne.find(mot1)) || (std::string::npos != ligne.find(mot2)) || (std::string::npos != ligne.find(mot3)) || (std::string::npos != ligne.find(mot4)) || (std::string::npos != ligne.find(mot5)) || (std::string::npos != ligne.find(mot6)) || (std::string::npos != ligne.find(mot7))) {



trouve++;
//Concatenation du nbrligne et nbrpage
motF = to_string(nbrligne) + ": " + to_string(nbrpage) + ", ";
// Ajout du mot a la map
itr = index.find(mot);
if (itr != index.end()) {
itr->second += motF;
}
else {

index.insert(std::pair<string, string>(mot, motF));
}



}


//calcul le nbr de page et du nbr de ligne par page
if (nbrligne % nbrLigneD == 0)
nbrpage++;
if (nbrligne == nbrLigneD)
nbrligne = 0;


}
fichier.close();


}


ifstream fichier("docum.txt", ios::in | ios::app);
while (getline(fichier, ligne)) {
while (fichier >> mott)
{
nbrmottot++;
}
}
fichier.close();




ofstream fichier3("Index.txt", ios::out | ios::app);

for (itr = index.begin(); itr != index.end(); ++itr)
fichier3 << itr->first << " => " << itr->second << '\n';

fichier3 << "\nLe nombre de ligne(s) total est de : " << nbrligneTot << "\nLe nombre de page(s) est de : " << nbrpage << "\nLe nombre de mot(s) est de : " << trouve << "\nLE nombre de mot total est de : " << nbrmottot;

fichier3.close();
}
else { cout << "\nLe fichier n'a pas pu s'ouvrir correctement"; }





fichier2.close();

system("pause");
return 0;










}
Hello Volzvolz,

What happened to the code tags in your fitst post?

In what directory is your input file? I needs to be in the same directory as the ".cpp" file for "main".

In the line ifstream fichier("docum.txt", ios::in | ios::app); the , ios::in | ios::app is not needed as the "i" says it is already an input file and there is nothing to append to the file because you can not not write to it.

It is also possible that the file will not open because of the , ios::in | ios::app

Hope that helps,

Andy
Last edited on
You have written initialize nbrmottot on the begining of code.After that you have given it a value 1 but you have done that in {} blocks and when it get out of the blocks i believe it gets destroyed.It's better for you to initialize it on the begining like nbrmottot.This is your: error Run-Time Check Failure #3 - The variable 'nbrmottot' is being used without being initialized.
Enter
nbrmottot=1; on the begining of your code.
Hello Volzvolz,

As I suspected the , ios::in | ios::app was the problem. When I first ran the program it created the file "clef.txt" which was an empty file so the while failed to read anything.

Not having anything for input files I could not test the program any farther.

I do agree with Zivojin that you have two uninitialized variables. With VS2017 all it takes to initialize a variable is an empty set of {}s or you can put a number inside them if you need something else.

I will see what I can do for the input files and if I find anything else in your code.

Hope that helps,

Andy
Topic archived. No new replies allowed.