Write a C++ program that will read in values and return the sum of different dat
ID: 3589320 • Letter: W
Question
Write a C++ program that will read in values and return the sum of different data types.
Write a function
T accum(vector <T> v)
that forms and returns the "sum" of all items in the vector v passed to it. For example, if T is a numeric type such as int or double, the numeric sum will be returned. If T represents the STL string type, the result of concatenation is returned.
Use input file tempData.txt as input. The first five lines will hold integer values. Sum these values. The next six lines hold string values. "Sum" (concatenate) these string values together. For each type, display the values and their respective sums.
NOTE: for any type T, the expression T( ) yields the value or object created by the default constructor. For example, T() yields the empty string object if T is the string class. If T represents a numeric type such as int, then T() yields 0. You may find this helpful when initializing your accumulator.
Be sure to use good programming methodology and keep your project modular.
the tempData.txt has the following
101
599
245
853
99
Exceptions
are
used
to
signal
errors
Explanation / Answer
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
//Template class declaration
template <class T>
T accum(vector <T> v) //accum is a template function that will return the sum of integer vector or concatenate a string vector
{
T sum = T();
for (int i = 0; i < v.size(); i++) //read each element of the vector
sum += v[i]; //accumulation of the values in the vector
return sum; //returns ths sum of given data-type
}
class VectorFile
{
private:
const int SIZE = 11; //number of lines in tempData.txt file
const int NUM_SIZE = 5; //first 5 lines of tempData.txt
const int STRING_SIZE = 6; //second half of tempData.txt is 6 lines
int count;
int num; //num are the int elements of vector
string word; //word are the string elemtns of vector
vector<int> number; //vector container for num
vector <string> sentence; //vector container for words
void tempData(); //function to read tempData.txt
void displayVector(); //function to display contents of integer and string vectors
public:
void start(); //function to access VectorFile members from Main program
ifstream txt; //hold tempData.txt
};
/**************************************************************
* VectorFile::start *
* This function displays introduction of program, *
* pauses for user input, then calls tempData function. *
**************************************************************/
void VectorFile::start()
{
cout << " +------------------------------------------------------------+";
cout << " | This program sums the contents of a text file. |";
cout << " | Make sure 'tempData.txt' is located in current directory. |";
cout << " | |";
cout << " | Press ENTER to continue . . . |";
cout << " +------------------------------------------------------------+ ";
cin.get();
tempData();
}
/**************************************************************
* VectorFile::tempData *
* This function reads the data of tempData.txt *
* and initalizes two vectors of integer and string data-types. *
* This function also calls the displayVector member. *
**************************************************************/
void VectorFile::tempData()
{
//attempt to open txt file
txt.open("tempData.txt");
if (!txt)
cout << "Error opening data file ";
//initalize the text file into the vectors
else
{
count = 0;
cout << " Reading file ";
//read first half of file into integer vector
while (count < NUM_SIZE && txt >> num) //NUM_SIZE count up to 5
{
number.push_back(num);
count++;
//Display the progress of reading file into vector, I used a counter of 99 million to slow down the reading of numbers.
for (int i = 0; i <= 99999999; i++);
cout << count << ""; // escape character is to backspace previous count and write over with next count
}
//read second half of file into string vector
while (count < SIZE && txt >> word) //This loop continues count where first loop left off, but counts up to 6 instead of 5
{
sentence.push_back(word);
count++;
for (int i = 0; i <= 99999999; i++);
if (count > 10) //create an extra backspace if count is more than 10
{
cout << "";
}
cout << count << "";
}
txt.close(); //close txt file
}
displayVector(); //call function to display the vectors
}
/***********************************************************************
* VectorFile::displayVector *
* This function displays the contents of the Class's vectors *
* and uses a Template function to display the sum of vector's contents. *
************************************************************************/
void VectorFile::displayVector()
{
//Display size of the integer vector
cout << " +------------------------------------------------------------+";
cout << " | Numbers found : " << number.size() << " |";
cout << " | ";
//Display elements of integer vector
for (count = 0; count < NUM_SIZE; count++)
{
cout << number[count] << " + ";
}
//Display integer sum by passing vector to template function accum
cout << " = " << accum(number) << " |";
cout << " +------------------------------------------------------------+ ";
//Display size of the string vector
cout << " +------------------------------------------------------------+";
cout << " | Words found : " << sentence.size() << " |";
cout << " | ";
//Display elements of string vector
for (count = 0; count < STRING_SIZE; count++)
{
cout << sentence[count] << " + ";
}
//Display concatenated string by passing vector to accum function
cout << " = |";
cout << " | " << accum(sentence) << " |";
cout << " +------------------------------------------------------------+ ";
}
/**************************************************************
* *
* Main *
* *
* *
**************************************************************/
//small Main driver program that creates a program object, which starts the VectorFile class's functions
int main()
{
VectorFile program;
program.start();
cout << " ";
system("pause");
return 0;
}
tempData.txt
101
599
245
853
99
Exceptions
are
used
to
signal
errors
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.