C++ coding help...create a program that opens a file and find and 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,tree,Pill,f… I know I have to use the loop to count the commas but I don't know how. Can anyone help me with this? #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.dat"); 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.eof()) { for(count=0; count <= SIZE; count++) count << name[count]; if(count= ',') count = } } cout << "The word or words with the most letters is " << count <<endl; return 0; }
-
Answer:
Inside your while loop for going through the file you need another loop either a while or for loop to count characters between commas. Have a variable to contain the largest count and a variable to contain the current count (between commas). When a comma is encountered compare the current to the largest (init the largest to zero) and put the characters in a variable so you can keep track of the word.
sum_one at Yahoo! Answers Visit the source
Related Q & A:
- How to delete a particular line from a file?Best solution by Stack Overflow
- How to Create a file in "my documents" folder?Best solution by Stack Overflow
- How to create a virtual file on mac OS X?Best solution by Super User
- How to create a simple counter program?Best solution by Stack Overflow
- Can I record a program/movie from a cable dvr to a DVD recorder?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.