CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Posts
    7

    Need help with reading and outputting files

    Hi everyone, I have got an assignment. It is about comparing two files one file has sentences and the other abbreviations. so far i have managed to compare the abbreviations which appear in the sentence and erase punctuation linked to abbreviations.

    However I am currently stuck on putting the punctuation back in when outputting the file. I am also stuck with replacing matching words in the sentences with the expanded abbreviations. Any help would be greatly appreciated. Not sure which code to show. but sentences and abbreviations are below.

    ABBREVIATIONS

    Aberd Aberdeen
    admin administration
    approx approximate
    Austral Australia
    div division
    Capt Captain
    Comdr Commander
    e east
    HQ Head Quarters
    m metres
    mil million
    mt Mount
    n north
    NATO North Atlantic Treaty Organisation
    NSW New South Wales
    pop population
    s south
    sq square
    w west

    Text file

    NATO troops were on exercise in Aberd Capt Jones of the first div told Comdr Frank that his troops were near the E flank of the NATO forces. Complaining about the amount of Admin, Capt Jones radioed the NATO HQ asked for navigation relative to the HQ. The Captain then left.

    For example the abbreviation "Aberd" in the sentence above needs to be replaced with Aberdeen from the abbreviations file. I need the last part of the abbreviation not "Aberd Aberdeen" need just "Aberdeen". Hope the info is understandable.

    Any help will be greatly appreciated.

  2. #2
    Join Date
    Dec 2012
    Posts
    7

    Re: Need help with reading and outputting files

    the way I took out punctuation:

    CODE::

    while(!text1.eof())
    ----{
    -----------text1 >> str;
    -----------text1Vector.push_back(str);
    -----------for(int j = 0;j < text1Vector.size(); j++)
    -----------{
    --------------string& str = text1Vector[j];
    --------------for(int k = 0; k < str.length(); k++)
    --------------{
    ------------------if (ispunct(str[k]))
    ------------------{
    ---------------------str.erase(k, 1);
    ------------------}
    --------------}
    -----------}
    -------cout << str << " ";
    -----}
    Last edited by abub93; March 26th, 2013 at 09:30 AM.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Need help with reading and outputting files

    I'm not following how punctuation applies to this problem, given the examples you provided.

    If it were me, I'd create a map of the abbreviation to the unabbreviated words.. I'd parse the sentence, look up each word in the map, and replace it if it were found in the map. If you don't take out the punctuation, you don't need to put it back.

    Also, your remove punctuation routine won't work if there are consecutive punctuation characters.

  4. #4
    Join Date
    Dec 2012
    Posts
    7

    Re: Need help with reading and outputting files

    ^^^Thanks for your reply. Punctuation is a separate problem not the same sorry if that wasn't clear. And for map/parse, I dont know how to do them things I'm just a beginner, know the basics

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need help with reading and outputting files

    Quote Originally Posted by abub93 View Post
    the way I took out punctuation:
    I know you're a beginner, but I would just like to show you all that code here:
    Code:
    for(int k = 0; k < str.length(); k++) 
    {
        if (ispunct(str[k]))
        {
            str.erase(k, 1);
        }
    }
    can be done in one line:
    Code:
    #include <algorithm>
    //...
    str.erase(std::remove_if(str.begin(), str.end(), ::ispunct), str.end());
    This eliminates mistakes such as

    1) Not looping enough times
    2) Looping too many times.
    3) Not erasing all of the punctuations.
    4) Using non-optimal methods of looping (e.g. calling length() each time in the loop).
    5) Just plain old writing the loop wrong.

    For 4) and 5) look at your code. You are at the same time erasing characters from str, thus making the length() shorter, but you're using str.length() as the loop terminator. Two things at once going on at the same time will lead to bugs.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 26th, 2013 at 01:04 PM.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Need help with reading and outputting files

    Victor Nijegorodov

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Need help with reading and outputting files

    This is a simple example showing use of the STL map class.

    Code:
    #include <map>
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    typedef map<string, string> MAP;
    typedef MAP::iterator MPOS;
    
    MAP	words;
    MPOS	mpos;
    string	ab;
    
    	words["n"] = "North";
    	words["s"] = "South";
    	words["e"] = "East";
    	words["w"] = "West";
    
    	cout << "Enter abrev: ";
    	cin >> ab;
    	cout << "'" << ab << "' ";
    	if ((mpos = words.find(ab)) == words.end()) {
    		cout << "not found" << endl;
    	} else {
    		cout << "maps to '" << mpos->second << "'" << endl;
    	}
    
    	return (0);
    }
    The words map would be initialised from your abbreviations file. Then from your sentence file for each word read find it in the word map. If the word exists in the word map, use the replacement from the map otherwise if it doesn't exist in the map use the original word.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured