Sponsor for PC Pals Forum

Author Topic: c++ files  (Read 2667 times)

Offline ben21

  • New Member
  • *
  • Posts: 11
c++ files
« on: July 26, 2005, 08:13 »
Class, String Processing, File Handling, Exception Handling, Searching, Sorting
The two text files named File1 and File2 each hold a number of English sentences. A C++ program
is required to carry out the following operations:
a) Given a word, it lists how many times that word appears in File1 and File2.
b) List all the words that occur in both the File1 and File2.
c) List all the words that occur in File1 but not in File2.
d) List all the words from both the files that alphabetically come after the word "nice".
e) To print a list where the words appear in alphabetical order.
It is assumed that all the words are in lower case characters, spaces separate the words and each
sentence is terminated by a full stop.


can ytou help me with a sample code of the program

Offline Simon

  • Administrator
  • *****
  • Posts: 77923
  • First to score 7/7 in Quiz of The Week's News 2017
c++ files
« Reply #1 on: July 26, 2005, 09:04 »
Hi Ben, and  :welcome:

I'm sure Sam will be able to help you with this, when he gets a minute - in the meantime, you might like to check out the rest of the forum.  :)
Many thanks to all our members, who have made PC Pals such an outstanding success!   :thumb:

Offline sam

  • Administrator
  • *****
  • Posts: 19977
c++ files
« Reply #2 on: July 26, 2005, 09:24 »
umm its been a while since I had to think in terms of C++.

Before I continue might I ask what level of knowledge you have of this language?

This task shouldnt be too difficult all you have to do essentailly is read the file into a vector or something similar and then search this with the string you type in, however I will need to remind myself of some C++ :-)

If you could reply to my first query and then we can see what we can do.
- sam | @starrydude --

Offline sam

  • Administrator
  • *****
  • Posts: 19977
c++ files
« Reply #3 on: July 27, 2005, 12:22 »
ok then here are some hints - I havent got time to figure it all out sorry. If you need more info I suggest you pick up a book form a library or buy one. A good book is Teach Yourself C++ in 21 day by Liberty but there are coutnless other good ones out there.

Here is how I would open a dictionary file and deal with non ascii chars (you can probably easily mod it to include full stops).

Code: [Select]
////////////////////
//  Dictionary    //
////////////////////
void dictionary_1(){
string words; // where the words will be saved
ifstream file("words.txt"); //opens dictionary file
while ( !file.eof()){
if(file >> words){  
for (character=0; character<words.size(); character++) {
words[character]=tolower(words[character]);
}  // makes all uppercase characters lower case.
dictionary.push_back(words); //inserts words into dictionary
}
else {//checks for any non-ASCII characters and removes them
file.clear();
file.ignore(INT_MAX, '\n');
}
}
}


This essentially puts all the "words" into a string called words until you have reached the end of the file which is defined by words.size().

I'm not sure how to do the rest of your stuff off my head, again it has been a while since I have done this. I'll have a think but I do suggest you look at a book or a list of c++ functions cause some of the things you want to do might already be built in (the sorting procedures).

A site that may, or may not be useful to you is: http://www.codeproject.com/
- sam | @starrydude --


Show unread posts since last visit.
Sponsor for PC Pals Forum