0 Members and 1 Guest are viewing this topic.
#include <iostream>#include <fstream>#include <string>#include <cstdlib>using namespace std;struct wordsToFind //keeps track of the keywords and how many of each their are{ string word; int count;};int main (){ int numberOfWords; wordsToFind *keyWords; bool goOn=true; char answer; ifstream inStream; string input; string fileName; while (goOn==true) { cout << "What is the name of the file to read from?" << endl; cin >> fileName; fileName += ".txt"; inStream.open(fileName.c_str()); cout << "How many words would you like to count? " << endl; cin >> numberOfWords; cin.ignore(); keyWords= new wordsToFind[numberOfWords]; string templine; for (int i=0; i<numberOfWords; i++) //assigns keywords and saves memory for them { cout << "Enter word number " << i+1 << endl; getline(cin, templine); keyWords[i].word = templine; keyWords[i].count=0; } for (int i=0; i<numberOfWords; i++) { cout << keyWords[i].word << endl; } if (inStream.is_open()) //checks to see if you can open the file (does it exist?) { while ( inStream.good() ) //keep reading until you are at the end of the file { inStream >> input; for (int i=0; i<numberOfWords; i++) { if (input==keyWords[i].word) //checks the words in the file against the keywords you have assigned in your array { keyWords[i].count++; } } } inStream.close(); } else cout << "Unable to open file"; for (int i=0; i<numberOfWords; i++) { cout << keyWords[i].word << ": "; cout << keyWords[i].count << endl; } cout << "Would you like to use this program on another file? [Y]es or [N]o" << endl; cin >> answer; if (answer=='Y' || answer=='y') { goOn=true; } if (answer=='N' || answer=='n') { goOn=false; } } cout << "\nThank you for using the word count program. Goodbye." << endl; delete [] keyWords; return 0;}
if (answer=='Y' || answer=='y')
Jumbled how? I thought i spaced it out really well compared to other snippets of C++ i've seen from my course last semester. Am I missing some conventions?And thanks for the tips guys, i'll look into it. I know I'm not an excellent programmer yet. But I'm working on it!*edit, i looked up recursive. thanks techb