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;
}

The easiest to solve ones:

Line 18: "indexofsmallest" undeclared.
Where should this variable come from? What does it do?

Lines 35 and 61: "wordcount" undeclared.
You will need to count the words first and send this to the function in the arguments.

Line 145: Expected "}" at end of input
You are missing one close brace. Tac this on to the end. You will need to properly indent the couple of lines above it that are currently out of line.

Edited 5 Years Ago by SgtMe: n/a

i see what you mean at line 35 & 61, i meant to use "size" there; at 145. there should be a "}" at line 119 to end that if statement. thanks.
however, indexofsmallest is declared on lines 14 &42 as integers, and set equal to pass on 17 & 45. it is a method/function procedure i learned for sorting

You are doing too much at one time. Take the program a setp at a time so you can understand completely what you are doing.

First, read the file and output what is read. No more. Then

1. count number of words in file

Get that running completely (and properly) before moving on. When finished, add

2. print all words, one per line

Get that running completely (and properly) before moving on.

What you are doing is trying to get everything done at once which makes it hard to really understand the steps -- the forest/trees problem. It also makes debugging and error checking much more manageable. And with only one step at a time, you won't have the problem that

there are many errors but I dont know how to fix it.

This article has been dead for over six months. Start a new discussion instead.