I have a text file to be read "Mary had a little lamb its fleece was white as snow and everywhere that Mary went, the lamb was sure to go!".
The program should do the following:
1. count number of words in file
2. print all words, one per line
3. ask user if they want to change any words. if yes, then needs to ask user what word they want to change and what they want to replace it with. then execute a replacement.
4. output words with the replacement, if applicable.
5. output words by their length, and then output words alphabetically (all words one per line)
This is what I have so far, but there are many errors but I dont know how to fix it. Can anyone help?
#include <iostream> #include <fstream> // for input file #include <string> // for strings using namespace std; char repeat, symbol; string oldword, newword; int ans, name; template <class Type> void SortLengths(Type A[], int size) { int pass = 0, lengthofsmallest; while (pass < size) { lengthofsmallest = pass; int length = strlen(A[indexofsmallest]); for (int i = pass + 1; i < size; i++) { if (strcmp(A[indexofsmallest], A[i]) > 0) indexofsmallest = i; } Type temp = A[pass]; A[pass] = A[indexofsmallest]; A[indexofsmallest] = temp; pass++; } cout << endl; cout << "The words sorted in assending order of their lengths:" << endl; cout << endl; for (int i = 0; i <= wordcount; i++) cout>> A[i]<<endl; } template <class Type> void SortAlphabetical(Type A[], int size) { int pass = 0, indexofsmallest; while (pass < size) { indexofsmallest = pass; for (int i = pass + 1; i < size; i++) { if (A[indexofsmallest] > A[i]) indexofsmallest = i; } Type temp = A[pass]; A[pass] = A[indexofsmallest]; A[indexofsmallest] = temp; pass++; } cout << endl; cout << "The words sorted in assending alphabetical order:" << endl; cout << endl; for (int i = 0; i <= wordcount; i++) cout>> A[i]<<endl; } int main() { ifstream infile1("input7.txt"); ifstream infile2("input7.txt"); if ( infile1.fail() || infile2.fail() ) { cout<<"Input file \"input5.txt\" opening failed"<<endl; exit(1); } cout<<"The records from the file \"input5.txt\" are as follows:"<<endl; cout<<endl; string one_word; int wordcount = 0; while(infile1 >> ws && !infile1.eof()) //reads the file a firt time to get word count { ++wordcount; infile1 >> one_word; } infile1.close(); cout << "The total number of words = "<< wordcount <<endl; string *words_array = new string[wordcount]; int count =0; while (infile2 >> ws && !infile2.eof()) //reads file a second time to fill the string "words_array" with all of the words { infile2 >> one_word; words_array[count++] = one_word; cout<<one_word<<endl; } infile2.clear(); cout<<endl; cout<<endl; cout<<"Do you want to replace any one word in the input text with a new word?"<<endl; cout<<"Type 1 for Yes or 0 for No, and <enter>"<<endl; cin>>ans; cout<<endl; if ( ans == 1) { cout<<"Type the word to be replaced and <enter>: "; cin>>oldword; //the word that they want replaced cout<<"Type the new word and <enter>: "; cin>>newword; //the word to replace the old word with cout<<endl; cout<<"The words with replacement are: "<<endl; int position = words_array.find(oldword, 0); while (position !=string::npos) // this should find the oldword in the string and place it with new word, then keep looking { // through out the file to see if the old word appears again, and then replace it, etc. words_array.replace(position, 1, newword); //until the end, which is regognized by string::npos position = words_array.find(oldword, position + 1); } cout << endl; cout << "The words as they appear in the modified text file: " << end; cout << endl; for (int i = 0; i < wordcount; i++) cout << words_array[i] << endl; SortLengths(words_array,wordcount); //function call to SortLengths to sort the words by their length SortAlphabetical(words_array,wordcount); //function call to SortAlphabetical to sort words alphabetically cout<<"Would you like to run this again?"; //this is just there for my own purposes, to keep output screen open, so please ignore cin>>repeat; cout<<"Good bye!"<<endl; return 0; }