CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2012
    Posts
    4

    Need help with this program

    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    int main ()
    {
    ifstream inputFile;
    inputFile.open ("proud_mary.txt");
    ofstream outputFile;
    outputFile.open ("ordered_mary.txt");
    int counter=0;
    int count=0;
    int andC=0;
    string word;
    string target;
    const int num=10;

    if(!inputFile)
    cout<<"file not found"<<endl;
    else
    {
    while (inputFile >> word)
    {
    if (word=="and")
    {
    andC++;
    word="XXX";
    }
    if (word >= target)
    {
    counter++;
    cout<<word<<" ";
    outputFile<< word<<" ";
    }
    if (counter % num== 0)
    {
    cout <<endl;
    outputFile <<endl;
    count++;
    }
    }
    if (counter % num!=1);
    {
    count++;
    }
    }

    cout<<endl;
    inputFile.close();
    cout <<endl;
    cout << "Total number of words: "<< counter <<endl;
    outputFile << "The number of words: "<< counter <<endl;
    cout << "Total number of lines: " << count << endl;
    outputFile << "Total number of lines: " << count << endl;
    outputFile.close();


    return 0;
    }





    Question to you all is, Comparing the two above strings, word and target .. Why would those two strings be compared? If you can tell me this in a way that makes any sense at all .. It's from opening a file with a whole bunch of words .. counting the words .. and the lines .. What would the function if (word >= target) do?

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

    Re: Need help with this program

    Quote Originally Posted by persianmess View Post
    Question to you all is, Comparing the two above strings, word and target .. Why would those two strings be compared?
    Why do you ask it apllying to your own code?

    As for me I don't see any sense in comparing one string (word) with another one (target) when this target is always empty!
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2012
    Posts
    4

    Re: Need help with this program

    Oh I'm sorry .. this isn't my program .. we are to analyze someone else's program and explain ..

    and I agree that those two strings shouldn't be compared .. how would you go about fixing that expression?

  4. #4
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Need help with this program

    Quote Originally Posted by persianmess View Post
    how would you go about fixing that expression?
    remove it. It always returns true.

    But I could imagine that the original intent was to check for non empty words.
    Code:
     if ( word >  "" )
    would be the same as
    Code:
     if ( !word.empty() )
    Kurt

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