Show the words that have the most letters...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sumone14
    New Member
    • Oct 2008
    • 1

    Show the words that have the most letters...

    I have to create a program that opens a file and I have to find and show the words that have the most letters. I got the file to open but I can't figure out how to count the letters. I think I have to have a for loop in order to move through the contents of the file but I can't get it to work properly.This is what I have done but I am not sure about the what to put in the loop. Also, the contents of the file look like this:
    12,Apple,fruit, dog,cat,car,tre e,Pill
    I know I have to use the loop to count the commas but I don't know how. Can anyone help me with this or even better, am I on the right track?



    #include <iostream>
    #include <fstream>
    using namespace std;

    int main()
    {
    const int SIZE = 60000;
    char name[SIZE];
    char *info ;
    string word;
    int count, i_word, i_count;
    count = 0;

    ifstream inFile;
    inFile.open ("lab04Small.da t");
    if (!inFile)
    {
    cout<< " Shame on you, you broke the file. Actually, the file cannnot be found";
    return -1;
    }

    inFile.getline( name, SIZE);
    info = new char[SIZE] ;

    int cWords( char *letters );
    {

    char cWords( *info);
    cout<< name <<endl;
    inFile.getline( name, SIZE, '\n');

    //inFile>>word;
    while(!inFile.e of())
    {
    for(count=0; count <= SIZE; count++)
    count << name[count];
    if(count= ',')

    }

    }
    cout << "The word or words with the most letters is " << count <<endl;


    return 0;
    }
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    How about storing the names in a vector or array of multiple strings? Make a loop that reads characters into the first string until it finds a comma, then starts reading into the next string, and so on. You could use the getline function to extract characters until a comma is found. Something like
    inFile.getline( stringList[i], MAXNAMESIZE, ',');
    might work.
    Hope this helps.

    Comment

    Working...