Need help on outputing.....

This is a discussion on Need help on outputing..... within the C++ Programming forums, part of the General Programming Boards category; I am working on a program to help make a small "dictionary" for our robot project ( see Practice Speaking ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    67

    Question Need help on outputing.....

    I am working on a program to help make a small "dictionary" for our robot project ( see Practice Speaking English for more detail ).
    The purpose for this small program is read in a word and its meaning which are provided in word.txt and meaning.txt. Then the program will convert it to the requested form for our robot.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int  main()
    {
         
         ifstream input;
         input.open("word.txt");
    
         if (input.fail())
         {
            cout<<"File doesn't exist."<<endl;
            system("pause");
            return 0; 
         }
         ofstream output;
         output.open("output.txt");
         
         char word[80];
         char meaning[200];
         
         input>>word;
         cout<<word<<endl;
         input.close();
         input.open("meaning.txt");
         if (input.fail())
         {
            cout<<"File doesn't exist."<<endl;
            system("pause");
            return 0; 
         }
         while (!input.eof())
         {
              input.getline(meaning, 200, '.');
              cout<<meaning<<endl;
         }
         
         input.close();
         
         
         cout<<"Copying....."<<endl;
         
         output<<meaning<<endl;
         
         output<<"<category>"<<endl;
         output<<"<pattern> WHAT IS THE MEANING OF "<<word<<" </pattern>"<<endl;
         output<<"<template><random>"<<endl;
         output<<"<li> Its meaning is "<<meaning<<".</li>"<<endl;
         output<<"<li> It means "<<meaning<<".</li>"<<endl;
         output<<"<li> It is "<<meaning<<".</li>"<<endl;
         output<<"</random></template>"<<endl;
         output<<"</category>"<<endl;
         output<<"<category>"<<endl;
         output<<"<pattern> WHAT DOES "<<word<<" MEAN </pattern>"<<endl;
         output<<"<template><srai> WHAT IS THE MEANING OF "<<word<<" </srai></template>"<<endl;
         output<<"</category>"<<endl;
         
         cout<<"done!"<<endl;
         
         system("pause");
         return 0;
    }
    This is the screen result:

    AIML
    he is good

    Copying.....
    done!
    Press any key to continue . . .
    So it seems my getline works to get the sentence. but when I try to output to the txt file output.txt. The problem occurs:


    <category>
    <pattern> WHAT IS THE MEANING OF AIML </pattern>
    <template><random>
    <li> Its meaning is .</li>
    <li> It means .</li>
    <li> It is .</li>
    </random></template>
    </category>
    <category>
    <pattern> WHAT DOES AIML MEAN </pattern>
    <template><srai> WHAT IS THE MEANING OF AIML </srai></template>
    </category>
    The meaning sentence didn't output! Therefore, I don't know why the getline get the meaning but can't output it! THX!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    You shouldn't use eof() to control your loop. It will usually cause a loop to run one too many times because eof() doesn't return true until after you try to read past the end of the file.

    So in this case, your loop is running twice. The first time it gets the meaning and outputs it. The second time it doesn't get anything, so you output a blank line (did you wonder why there was that blank line there?). So when you output the meaning to the output.txt file, the meaning string is empty.

    A common way to loop on input is to put the read into the loop control:
    Code:
    while (input.getline(meaning, 200, '.'))

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    67
    Well, thx Daved, I just changed as what you told me but it seems there're still problems.
    This is the new output:

    <category>
    <pattern> WHAT IS THE MEANING OF AIML </pattern>
    <template><random>
    <li> Its meaning is .</li>
    <li> It means .</li>
    <li> It is .</li>
    </random></template>
    </category>
    <category>
    <pattern> WHAT DOES AIML MEAN </pattern>
    <template><srai> WHAT IS THE MEANING OF AIML </srai></template>
    </category>
    You can see it is as same as the previous one. I wonder if there's any problems with my output.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Is the text displayed in the cout the same? Are you calling getline inside the loop also (you shouldn't be)?

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    67
    Yes, the test display in cout is same. I just replace

    Code:
    while (!input.eof())
         {
              input.getline(meaning, 200, '.');
              cout<<meaning<<endl;
         }
    by

    Code:
    while (input.getline(meaning, 200, '.'))
    cout<<meaning<<endl;
    Do you think there is anything wrong with output?

  6. #6
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Code:
    ...
    input>>word;
    cout<<word<<endl;
    input.clear();
    input.close();
    input.open("meaning.txt");
    ...
    Try adding input.clear(); This should fix the problem. This kind of explains it, although, I don't fully understand it myself. I thought when you closed the file everything reset. Perhaps someone else could explain.

    Edit- It's probably better to put the input.clear() after you close the file.
    Last edited by Cpro; 04-16-2009 at 08:56 AM.
    IDE - Visual Studio 2005
    Windows XP Pro

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    67
    Oh, thank you Cpro. I just tried but it still never output the meaning-----I mean the sentence. SO I am just wondering whether I can output a sentence by getline or just do somethng else, like read in the sentence as a string.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Can you repost your whole program again with the changes? Also post the contents of word.txt and meanings.txt.

    There are a lot of little details that might be causing this problem depending on what is in those files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help in outputing different file names in a loop
    By nightfallz in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2009, 09:53 AM
  2. Outputing an array to a textbox ?
    By C of Green in forum C# Programming
    Replies: 2
    Last Post: 10-31-2006, 10:28 AM
  3. Outputing Results to a File
    By SITHDUKE in forum C++ Programming
    Replies: 11
    Last Post: 03-03-2005, 06:33 PM
  4. Help with outputing char in int
    By xephyr in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2004, 11:18 PM
  5. Replies: 2
    Last Post: 01-02-2002, 01:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21