1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Asking input file condition statement

Discussion in 'C++' started by kslpeter87, Sep 10, 2007.

  1. kslpeter87

    kslpeter87 New Member

    Joined:
    Jul 20, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Question: Calculate the number of words in a paragraph entered

    algorithm :
    1. store the paragraph entered into a .txt file
    2. read the words from file, which separated by spaces
    3. loop step 2 until no words left to read
    4. display the number of loops repeated.


    How to excape the loop with condition there's no more words in the txt file ?
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    I could not get whats your query or do you want us to write the program for you? If that is the case this is a forum where people help each other and not a free home work help store.
     
  3. kslpeter87

    kslpeter87 New Member

    Joined:
    Jul 20, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Shabbir i've edited my post. Hope you could get what i mean.
     
  4. kslpeter87

    kslpeter87 New Member

    Joined:
    Jul 20, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    This is my code

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    
    int main()
    {
    ofstream outputfile;
    ifstream inputfile;
    char X[500],a[50];
    
    outputfile.open("abc.txt");
    
    cout << "Type the paragraph and will count the words you typed";
    cin.get(X,500);
    
    outputfile << X;
    outputfile.close();
    
    inputfile.open("abc.txt");
    
    for (int count=0 ; [COLOR="Red"][SIZE="4"][      ][/SIZE][/COLOR] ; count++)
        inputfile >> a;
    
    getch();
    
    return 0;
    
    }
    The [ ] is the part i dunno how to write. hope somebody can help.
     
  5. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    You can put inputfile >> a; because it will return null when the EOF is reached. Just check the doc to make it 100% sure
     
  6. kslpeter87

    kslpeter87 New Member

    Joined:
    Jul 20, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Thank you Shabbir. I use while loop instead of for loop. Thank you very much.
     

Share This Page