Basically the program reads the file char by char finds the numbers, finds out which one is the biggest, which the smallest, and the average of the numbers.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
int main(int argc, char* argv[])
{
std::vector<unsignedint> data;
std::ifstream file("yourfile.txt");
if (!file.good())
{
std::cout << "Could not read the file\n";
return 0;
}
while (!file.eof())
{
//store the string in a temporary variable
std::string temp("");
file >> temp;
//extract the value from the temporary string to the value variable
unsignedint value;
std::string temp2("");
for (unsignedint i = 0; i < temp.size(); i++)
{
if (isdigit(temp[i]))
{
temp2 += temp[i] ;
}
}
std::istringstream stream(temp2);
stream >> value;
//insert the value into the container at the end
data.push_back(value);
}
//calculate the average
std::vector<unsignedint>::iterator it_begin = data.begin();
std::vector<unsignedint>::iterator it_end = data.end();
unsignedint sum = 0;
while (it_begin != it_end)
{
sum += *it_begin;
it_begin++;
}
double average = sum / data.size();
std::cout << "\nThe average is " << average;
//sorting the container , so that the min value is at the beginning
//and the highest value is at the end
std::sort(data.begin(), data.end());
std::cout << "\nThe minimum value is " << *data.begin();
std::cout << "\nThe maximum value is " << *(--data.end());
return 0;
}
I think "c = input.get();" should be in "while statement", otherwise "while(!input.eof())" will not terminate, and in the first if statement you should turn nums back to true.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
int main(int argc, char* argv[])
{
unsignedint* data;
std::ifstream file("yourfile.txt");
if (!file.good())
{
std::cout << "Could not read the file\n";
return 0;
}
unsignedint size = 3;
data = newunsignedint[3];
int forward_iterator = 0;
while (!file.eof())
{
//store the string in a temporary variable
std::string temp("");
file >> temp;
//extract the value from the temporary string to the value variable
unsignedint value;
std::string temp2("");
for (unsignedint i = 0; i < temp.size(); i++)
{
if (isdigit(temp[i]))
{
temp2 += temp[i] ;
}
}
std::istringstream stream(temp2);
stream >> value;
//insert the value into the container at the end
data[forward_iterator] = value;
std::cout << "\n " << data[forward_iterator];
forward_iterator++;
}
//calculate the average
unsignedint i = 0;
unsignedint sum = 0;
auto min = *data;
auto max = *data;
while (i < size)
{
sum += data[i];
if (data[i] < min) min = data[i];
if (data[i] > max) max = data[i];
i++;
}
double average = sum / size;
std::cout << "\nThe average is " << average;
std::cout << "\nThe minimum value is " << min;
std::cout << "\nThe maximum value is " << max;
delete[] data;
return 0;
}