Hi,
I am having problem in making the program work in while loop. The program is working fine but I want to make it work in one big loop and don't want to use strcpy. Instead of that I just wanna get one word and do it in a loop.
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#define DELIMS " .\n"
using namespace std;
int main(int argc, char** argv)
{
char word[100][16]; //Stores Words
char line[101]; //Temporary stores lines
int count[100]; //Coounts the no times word is repeated
char longestword[16]; //Sotes the longest word
char shortestword[16]; //Stores the Shortest Word
int largestlength = 0;
char *p;
int i=0;
int totalnosofwords=0; //Count of total no of words
int noofuniq=0; //Count total no of unique words
int shortestlength=15;
int nosoflines=0; //Counts no of lines
ifstream fin; // fin is like cin.
fin.open(txt.txt); // opens the file
if(!fin) //Checking if input file is Valid
{
cout << "While opening a file an error is encountered" << endl;
exit(1);
}
ofstream fout; // fin is like cin.
fout.open(output.txt); // opens the file
if(!fout) //Checks if output file is valid
{
cout << "While opening a file an error is encountered" << endl;
exit(1);
}
while(!fin.eof()) //Reads the input file till the end
{
fin.getline(line, 101); //Reads one line at a time from input file
fout<<line<<endl; //Writes one line to the output file
nosoflines++; //Count the number of lines
p = strtok(line, DELIMS ); //Strtok() gives pointer to a word
strcpy(word[i],p);
count[i]=1;
while(p != NULL) //Check if next word is valid
{
i++;
p = strtok( NULL, DELIMS ); //Get pointer to next word
if(p !=NULL)
{
strcpy(word[i],p); //Copy word to word array
count[i]=1; //make its count 1
}
}
}
fin.close(); //Close input file
totalnosofwords=i; //Get the total no of words
noofuniq=i; //Get the Max no of words
for(int j=0; j<i; j++)//Check for repeated words, shortest word and longest word
{
if(count[j]>0) //Check is that word has been previously checked
{
if(strlen(word[j])>largestlength) // Comapres longest word to previous word
{
strcpy(longestword,word[j]); //If it is longest store it in word[j]
largestlength=strlen(word[j]); //Get the length of longest word
}
if(strlen(word[j])<shortestlength)//Compres shortest word to previous word
{
strcpy(shortestword,word[j]); //If it is shortest store it in word[j]
shortestlength=strlen(word[j]); //Get the length of shortest word
}
for(int z=j+1; z<i; z++)//Check if the word is repeated
{
if((strcmp(word[j],word[z]))==0)//Compare word[j] with other words
{
count[j]++; //Increase the count if it is found
count[z]=0; //Set the count to zero of the duplicate word
noofuniq--; //Decrease the count of unique words
}
}
}
}
fout<<endl<<"Statistics:"<<endl; //Print the staistics to output file
fout<<"Total no of Words: "<<totalnosofwords<<endl;
fout<<"No of Unique Words: "<<noofuniq<<endl;
fout<<"No of Lines: "<<nosoflines<<endl;
fout<<"Longest word: "<<longestword<<endl;
fout<<"shortest word: "<<shortestword<<endl<<endl;
for (i=0;i<totalnosofwords ;i++ )
{
if(count[i]>0)
{
fout<<word[i]<<" : "<<count[i]<<endl;
}
}
fout.close(); //Close the output file
return 0;
}