Thread: need help...c++ using strings...difficult for me

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    18

    need help...c++ using strings...difficult for me

    can someone help me out here. I have 2 documents named Emma and Letter. The file contained Emma has a lot of introductory material which I need to skip. Easiest way is to hunt for the string "Chapter I". Then start with file position marker on the first line. The way this works is with groups of 3 words from letter.txt. The first word says how many lines you need to move down in the text by the number of letters in the words. Second word tells you the word on that line by the number of letters in the word. Third word, use the length of the word to find the particular letter. Repeat thus until the letter or character found is a period(.). The output should be "timeisimportant"

    Example, the letter.txt document starts like this:

    Hi cousin,

    I might be at school with you soon............

    So you would use Hi, cousin and I as first, second, and third word. And use the directions in Emma to find each letter. And keep repeating.
    Here is my code so far: (which is not much)
    Code:
    int main()
    {
    ifstream inputFile;
    ifstream input;
    
    string a, w, x, z;
    a = "CHAPTER I";
    
    
    inputFile.open("Emma.txt"); // opens the txt file
    input.open("Letter.txt");
    
    while (w != a)
    getline(inputFile, a);
    
    while (!input.eof())
    getline(input, z);
    
    input >> ;
    
    
    
    
    return 0;
    if you are to offer help please keep it simple as I prob. am not as advanced as most of you. Thanks

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well first of all, you should learn to indent the code properly, otherwise it gets hard to read.

    Next, I'm not sure what a, w, x, & z stand for and a few months from now after you've moved on to something else, you probably won't remember what they meant either, so you should always give variables & functions meaningful names.

    It looks like a good start, but unless emma.txt has a line with exactly "CHAPTER I" on a line by itself, it won't find it. So you should verify that the file has a line with just that string, same case & everything, otherwise you'd need to do a case-insensitive string comparison (convert both strings to upper-case before comparing, for example).

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    while (w != a) - w is not initialized here


    while (!input.eof()) - don't do this, read FAQ

    Code:
    while (getline(input, z))
    {
       //do something with z
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    It's an interesting homework problem, but don't get overwhelmed by it. Break it into small chunks - divide and conquer. First, code the part that reads through a file a line at a time. Print out each line to stdout. That's one accomplishment.

    Then learn how to find substrings in a string so you can find the keywords (like CHAPTER). Then learn how to a break a line into separate words. Once you have the first three words of a line, you learn how to count the characters in that word.

    Find a reference on std::string (Google should be able to help) and marvel at the wealth of member functions it has to assist you.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by vart View Post
    while (w != a) - w is not initialized here


    while (!input.eof()) - don't do this, read FAQ

    Code:
    while (getline(input, z))
    {
       //do something with z
    }
    Actually, since it's a string, it's initialized to "".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM