In: Computer Science
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that.
// File: main.cpp
/*---------- BEGIN - DO NOT EDIT CODE ----------*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
using index_t = int;
using num_count_t = int;
using isConnected_t = bool;
using sum_t = int;
const int MAX_SIZE = 100;
// Global variable to be used to count the recursive
calls.
int recursiveCount = 0;
// Function prototypes.
isConnected_t isFileOpenForInput(ifstream& ifs, const
string& filename);
num_count_t loadNumbersFromFile(ifstream& ifs, int
numbers[]);
void displayNumbers(const int numbers[], const int length);
sum_t sumOfNumbers(const int numbers[], const int length);
int main() {
ifstream ifs;
int numbers[MAX_SIZE];
if ( !isFileOpenForInput(ifs, "numbers.txt") ) {
cerr << "Error opening file for reading." <<
endl;
exit(1);
}
cout << setw(26) << "Numbers: ";
int length = loadNumbersFromFile(ifs, numbers);
displayNumbers(numbers, length);
cout << endl
<< setw(26) << "The sum of all values is: " <<
sumOfNumbers(numbers, length);
cout << endl;
return 0;
}// end main()
/*---------- END - DO NOT EDIT CODE ----------*/
/* TODO (1):
* Implement the isFileOpenForInput() function.
*
* Open the connection to the filename and return the
connection
* status.
*/
isConnected_t isFileOpenForInput(ifstream &ifs, const string
&filename)
{
ifs.open(filename);
return ifs.is_open();
}
/* TODO (2):
* Implement the loadNumbersFromFile() function.
*
* Read each number from the given stream and store
* it into the array.
*
* Return a count of the numbers read.
*/
num_count_t loadNumbersFromFile(ifstream &ifs, int
numbers[])
{
int count = 0;
while (!ifs.eof())
{
ifs >> numbers[count++];
}
return count;
}
void displayNumbers(const int numbers[], const int length)
{
ostringstream oss;
oss << "[ ";
for (int i = 0; i <= length - 1; i++) {
oss << numbers[i] << ", ";
}
oss.seekp(-2, ios::end);
oss << " ]";
cout << oss.str();
}// end displayEmployees()
/* TODO (3):
* Implement the recursive sumOfNumbers() function.
*
* The function recursively adds all the elements of
* the array together.
*
* Upon entry increment the recursiveCount by 1. This
* will keep track of the number of times the function
* is called.
*
* Return the sum of all elements in the array.
*/
sum_t sumOfNumbers(const int numbers[], const int length)
{
if (recursiveCount == length)
return 0;
recursiveCount++;
return numbers[recursiveCount - 1] + sumOfNumbers(numbers,
length);
}
Code:- There is not much to change the output is correct. Go through the code below and ask if you have any doubts
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
using index_t = int;
using num_count_t = int;
using isConnected_t = bool;
using sum_t = int;
const int MAX_SIZE = 100;
// Global variable to be used to count the recursive
calls.
int recursiveCount = 0;
// Function prototypes.
isConnected_t isFileOpenForInput(ifstream& ifs, const
string& filename);
num_count_t loadNumbersFromFile(ifstream& ifs, int
numbers[]);
void displayNumbers(const int numbers[], const int length);
sum_t sumOfNumbers(const int numbers[], const int length);
int main() {
ifstream ifs;
int numbers[MAX_SIZE];
if ( !isFileOpenForInput(ifs, "numbers.txt") ) {
cerr << "Error opening file for reading." <<
endl;
exit(1);
}
cout << "Numbers: ";
int length = loadNumbersFromFile(ifs, numbers);
displayNumbers(numbers, length);
cout << endl
<< setw(26) << "The sum of all values is: " <<
sumOfNumbers(numbers, length);
cout << endl;
return 0;
}// end main()
/*---------- END - DO NOT EDIT CODE ----------*/
/* TODO (1):
* Implement the isFileOpenForInput() function.
*
* Open the connection to the filename and return the
connection
* status.
*/
isConnected_t isFileOpenForInput(ifstream &ifs, const string
&filename)
{
ifs.open(filename);
return ifs.is_open();
}
/* TODO (2):
* Implement the loadNumbersFromFile() function.
*
* Read each number from the given stream and store
* it into the array.
*
* Return a count of the numbers read.
*/
num_count_t loadNumbersFromFile(ifstream &ifs, int
numbers[])
{
int count = 0;
while (!ifs.eof())
{
ifs >> numbers[count++];
}
return count;
}
void displayNumbers(const int numbers[], const int length)
{
ostringstream oss;
oss << "[ ";
for (int i = 0; i <= length - 1; i++) {
oss << numbers[i] << ", ";
}
oss.seekp(-2, ios::end);
oss << " ]";
cout << oss.str();
}// end displayEmployees()
/* TODO (3):
* Implement the recursive sumOfNumbers() function.
*
* The function recursively adds all the elements of
* the array together.
*
* Upon entry increment the recursiveCount by 1. This
* will keep track of the number of times the function
* is called.
*
* Return the sum of all elements in the array.
*/
sum_t sumOfNumbers(const int numbers[], const int length)
{
if (recursiveCount == length)
return 0;
recursiveCount++;
return numbers[recursiveCount - 1] + sumOfNumbers(numbers,
length);
}
Screenshots
Input\output