- #1
- 4
- 0
I am having trouble trying to get my code to run for the following:
// Use substitution strings to replace all the occurrences
// of multiple tags in the lines of text in a form.
// The user enters the filename.
// The tags and their substitution strings are found in another file.
// The user enters the filename.
// Output the result to the file letter.txt.
// The counts of substitutions for each tag will be output to the screen
// sorted alphabetically by tag names.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std; //introduces namespace std
// Maximum nunber of tagged fields
const int MAXFIELDS = 20;
// Structure to hold the tag and substitution string.
// It is also able to maintain a count of the occurrences of that tag.
struct Field
{
string tag;
string sub;
int count;
};
// Function declarations
// Add additional declarations as needed
int readFields(Field fields[]);
void trim(string& s);
string fillForm(const string& form, Field fields[], int nFields);
string replaceTag(const string& form, Field& field);
int main( void )
{
// Declare string for the form letter
// Call Function 1 to read in the form letter to the string
// Declare an array of Fields named fields
Field fieldsArray[MAXFIELDS];
// Declare variable for the field count
// Call Function 2 to read the fields and return the count.
// Declare string for the finished letter.
// Call Function 5 that substitutes all the sub strings for the tags.
// It returns the finished letter.
// Open the file for the finished letter and test the file stream
// Write the finished letter to the file
// Close the file stream
// Call Function 7 to output the field statistics
return 0;
}
// Function #1
// This is a function to read a form into a string reference parameter.
// Write it. Use string concatenation to merge the lines that you read into
// a single string. Don't forget to insert newline characters.
// This is nearly the same as the corresponding function in Assignment 6.
// However, you will now prompt the user to enter the name of the file to open.
// Function #2
// This function populates the Fields in an array and returns the count.
// It is nearly the same as readTagSub() from Assignment 6.
// Prompt the user for a filename to open.
// It calls the function to convert the tags to lowercase,
// and calls the trim function to remove leading whitespace from the
// substitution string.
// Observe the use of the subscript and dot operators to access data
// members of structs in an array.
int readFields(Field fields[])
{
// Declare and initialize count of the fields read
int n=0;
// Prompt and read filename.
// Open the input file and test it.
// Loop as long as we have not reached the EOF or exceeded the array limit
{
// Input the tag
// Break if there was no tag
// Call Function 3 to make the tag lowercase
// Read the rest of the line into the substitution string
// Call the Function 4 to strip off the leading whitespace characters
// Set the occurrence count to 0
// Increment the field count
}
// Close the stream
// Return the field count
return n;
}
// Function #3
// This function converts the const string reference parameter to
// all lowercase and returns it.
// This is exactly the same as the corresponding function in Assignment 6.
// Function #4
// This function trims off leading whitespace from the string reference parameter.
// This is exactly the same as the corresponding function in Assignment 6.
// Find the position of the first non-whitespace character and use the substr function.
void trim(string& s)
{
// Loop over each leading character until you find one that is not a whitespace.
// Use substr to assign a new value to s.
}
// Function #5
// This function takes an entire form and returns the string
// that has all tags replaced with their substitution string.
string fillForm(const string& form, Field fields[], int nFields)
{
// Declare a string variable initialized with the form parameter
// Loop over all the fields
{
// Call Function 6 using the local variable and a single Field.
// Use the result to update the local string variable.
}
// Return the filled form (not "")
return "";
}
// Function #6
// This function replaces all the occurrences of a single tag
// with its substitution string. Tags are case-insensitive.
// This is nearly the same as the corresponding function in Assignment 6.
// However, it now counts the number of replacements of the tag.
// Use the dot operator to access the data members of the Field.
// It returns the new string with this tag's replacements.
string replaceTag(const string& form, Field& field)
{
// Declare a string for the new string
string newStr;
// Make a lowercase version of the form to search for the tag
// Declare instances of size_t for the tag length,
// start position for the searching, and position of the next tag.
// Initialize them appropriately.
// Loop as long as you haven't reached the end of the form
{
// Find the position of the next tag in the lowercase version of the form.
// Use the version of the find function that takes the start position of
// the search as the second argument.
// If a tag was found (use string::npos)
{
// Concatenate onto the revision the substring from the original form
// beginning at the start position up to the position of the next tag.
// Concatenate onto the revision the substitution string.
// Increment the occurrence count for the tag.
// Update the start position to just past the end of the tag.
}
// Otherwise (tag was not found)
{
// Concatenate onto the revision the substring from the original form
// beginning at the start position up to the end of the form.
// Update the start position to the length of the form.
}
}
// Return the revision string
return newStr;
}
// Function #7
// This function outputs the field statistics to cout.
// Call Function 8 to sort the fields so that the output
// appears alphabetically by tag name.
// Write the function definition.
// Function #8
// Use either bubble sort or selection sort to sort the
// array of Field structs alphabetically by tag.
// There is a similar example of using bubble sort to sort
// an array of Student structs in the lecture notes.
// Function #9
// Swap function for Fields
I tried many combination of functions and arrays but they won't work. What code goes ater the //
// Use substitution strings to replace all the occurrences
// of multiple tags in the lines of text in a form.
// The user enters the filename.
// The tags and their substitution strings are found in another file.
// The user enters the filename.
// Output the result to the file letter.txt.
// The counts of substitutions for each tag will be output to the screen
// sorted alphabetically by tag names.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std; //introduces namespace std
// Maximum nunber of tagged fields
const int MAXFIELDS = 20;
// Structure to hold the tag and substitution string.
// It is also able to maintain a count of the occurrences of that tag.
struct Field
{
string tag;
string sub;
int count;
};
// Function declarations
// Add additional declarations as needed
int readFields(Field fields[]);
void trim(string& s);
string fillForm(const string& form, Field fields[], int nFields);
string replaceTag(const string& form, Field& field);
int main( void )
{
// Declare string for the form letter
// Call Function 1 to read in the form letter to the string
// Declare an array of Fields named fields
Field fieldsArray[MAXFIELDS];
// Declare variable for the field count
// Call Function 2 to read the fields and return the count.
// Declare string for the finished letter.
// Call Function 5 that substitutes all the sub strings for the tags.
// It returns the finished letter.
// Open the file for the finished letter and test the file stream
// Write the finished letter to the file
// Close the file stream
// Call Function 7 to output the field statistics
return 0;
}
// Function #1
// This is a function to read a form into a string reference parameter.
// Write it. Use string concatenation to merge the lines that you read into
// a single string. Don't forget to insert newline characters.
// This is nearly the same as the corresponding function in Assignment 6.
// However, you will now prompt the user to enter the name of the file to open.
// Function #2
// This function populates the Fields in an array and returns the count.
// It is nearly the same as readTagSub() from Assignment 6.
// Prompt the user for a filename to open.
// It calls the function to convert the tags to lowercase,
// and calls the trim function to remove leading whitespace from the
// substitution string.
// Observe the use of the subscript and dot operators to access data
// members of structs in an array.
int readFields(Field fields[])
{
// Declare and initialize count of the fields read
int n=0;
// Prompt and read filename.
// Open the input file and test it.
// Loop as long as we have not reached the EOF or exceeded the array limit
{
// Input the tag
// Break if there was no tag
// Call Function 3 to make the tag lowercase
// Read the rest of the line into the substitution string
// Call the Function 4 to strip off the leading whitespace characters
// Set the occurrence count to 0
// Increment the field count
}
// Close the stream
// Return the field count
return n;
}
// Function #3
// This function converts the const string reference parameter to
// all lowercase and returns it.
// This is exactly the same as the corresponding function in Assignment 6.
// Function #4
// This function trims off leading whitespace from the string reference parameter.
// This is exactly the same as the corresponding function in Assignment 6.
// Find the position of the first non-whitespace character and use the substr function.
void trim(string& s)
{
// Loop over each leading character until you find one that is not a whitespace.
// Use substr to assign a new value to s.
}
// Function #5
// This function takes an entire form and returns the string
// that has all tags replaced with their substitution string.
string fillForm(const string& form, Field fields[], int nFields)
{
// Declare a string variable initialized with the form parameter
// Loop over all the fields
{
// Call Function 6 using the local variable and a single Field.
// Use the result to update the local string variable.
}
// Return the filled form (not "")
return "";
}
// Function #6
// This function replaces all the occurrences of a single tag
// with its substitution string. Tags are case-insensitive.
// This is nearly the same as the corresponding function in Assignment 6.
// However, it now counts the number of replacements of the tag.
// Use the dot operator to access the data members of the Field.
// It returns the new string with this tag's replacements.
string replaceTag(const string& form, Field& field)
{
// Declare a string for the new string
string newStr;
// Make a lowercase version of the form to search for the tag
// Declare instances of size_t for the tag length,
// start position for the searching, and position of the next tag.
// Initialize them appropriately.
// Loop as long as you haven't reached the end of the form
{
// Find the position of the next tag in the lowercase version of the form.
// Use the version of the find function that takes the start position of
// the search as the second argument.
// If a tag was found (use string::npos)
{
// Concatenate onto the revision the substring from the original form
// beginning at the start position up to the position of the next tag.
// Concatenate onto the revision the substitution string.
// Increment the occurrence count for the tag.
// Update the start position to just past the end of the tag.
}
// Otherwise (tag was not found)
{
// Concatenate onto the revision the substring from the original form
// beginning at the start position up to the end of the form.
// Update the start position to the length of the form.
}
}
// Return the revision string
return newStr;
}
// Function #7
// This function outputs the field statistics to cout.
// Call Function 8 to sort the fields so that the output
// appears alphabetically by tag name.
// Write the function definition.
// Function #8
// Use either bubble sort or selection sort to sort the
// array of Field structs alphabetically by tag.
// There is a similar example of using bubble sort to sort
// an array of Student structs in the lecture notes.
// Function #9
// Swap function for Fields
I tried many combination of functions and arrays but they won't work. What code goes ater the //