Why it keeps giving me a zero answer ?!!! even though there IS a text file to search in it !!

//a C++ project that reads a text from an input file and writes the same text into an output and find how much symbols are there
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
using namespace std;

ifstream infile;
ofstream outfile;
void tokenize ();
void statistic ();


int main()//the main function is gonna to call func tokenize first then the func statistis
{

    tokenize ();
    statistic ();

    return 0;
}

void tokenize()//the tokenize function is gonna ask the user to type their txet in input file then it's re-write it on output file
{

    cout<<"Please write your txet in your file that have been called input"<<endl;
    infile.open("input.txt"); 
    outfile.open("output.txt"); 
    string x;

    while(!infile.eof())
    {
        infile>>x; //write something in the text
        outfile<<x<<endl; //the writting in the input file is ganna to be re-wrote in output file each word in new line
    }

}

void statistic()//the tokenize function is gonna ask the user to type their txet in input file then it's re-write it on output file
{
    string word;
    infile.open("input.txt");

    int dot=0, comma=0, questionmark=0, exclamationmark=0, doublequotation=0;

    while (!infile.eof())
    {
        infile>>word;

        for (int i=0; !infile.eof(); i++)
    {
            if (word == ".")
                dot++;
            else
                if (word == ",")
                    comma++;
            else
                if (word == "?")
                    questionmark++;
            else
                if (word == "!")
                exclamationmark++;
            else
                if (word == "\"")
                    doublequotation++;
        }
    cout<<"Number of dots in the txet: "<<dot<<endl;

    cout<<"Number of commas in the txet: "<<comma<<endl;

    cout<<"Number of question marks in the txet: "<<questionmark<<endl;

    cout<<"Number of exclamation marks in the txet: "<<exclamationmark<<endl;

    cout<<"Number of double quotation marks in the txet: "<<doublequotation<<endl;

    }

Recommended Answers

All 3 Replies

How do you know it's finding the text file to open it? The textfile has to be in the working directory of the program that is running. This may well not be the same directroy as the source code, and it may well not be the same directroy as the executable that gets built.

You can be absolutely sure by providing the complete, absolute path to the text file instead of just "input.txt".

Seems like you could get these stats char by char; stats on unique words used and each word frequency are what demands tokenizing words.

If you code as per the following, you can 'see' if there was a problem finding/opening the files ...

// fileReadWrite.cpp //

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const char* FILE_IN  = "input.txt";
const char* FILE_OUT = "output.txt";


int main()
{
    ifstream fin( FILE_IN );
    ofstream fout( FILE_OUT );
    if( fin && fout )
    {
        string word;
        while( fin >> word ) // read in each 'word' in the file, word by word
        {
            // do any processing here that you wish on this 'word' ... for example
            // just print it to screen and to file, each 'word' on a new line

            cout << word << endl; // write each 'word' on a new line, to the console screen
            fout << word << endl; // write each 'word' on a new line, in the output.txt file
        }

        fout.close();
        fin.close();
    }
    else
    {
        if( !fin ) cout << "There was a problem opening file " << FILE_IN  << endl;
        if( !fout ) cout << "There was a problem opening file " << FILE_OUT  << endl;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.