Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- #include <iomanip>
- #include <math.h>
- using namespace std;
- double avg (int total, int countr, int number []) //should average + total be passed in? gets mad when I don't declare total
- {
- for (int i = 1; i > 0; i++)
- total = total + number [i]; //this should add the numbers together as it loops
- double average = total/countr; //average is NOT correct (?)
- return average;
- }
- int main()
- {
- int i=0;
- int countr=0;
- char comma = ','; //stops the reading at comma
- ifstream infile;
- char cNum[40]; //40 is size of the array
- int number [40];
- int total;
- double average = avg (total, countr, number); //this is NOT working, something is wrong how I am declaring/calling function and variables
- infile.open ("listOfIntegers.txt", ifstream::in);
- if (infile.is_open())
- {
- while (infile.good())
- {
- infile.getline(cNum, 256, ',');
- if (i==0) //first number is the amount of numbers = 0 is the first in an array
- {
- countr = atoi(cNum); //gets it to be an integer
- i++;
- }
- else
- {
- number[i-1] = atoi(cNum); //all other numbers are put into the array number
- }
- }
- infile.close();
- }
- else
- {
- cout << "Error opening file";
- }
- ofstream outFile;
- outFile.open("stats.txt");
- if (outFile.is_open())
- {
- for (int j = 0; j<40; j++)
- {
- outFile << setw(5) << "The data set is:" << number << endl; //how to get numbers to output in nice way? output at all?
- }
- for (int j = 0; j<=40; j++)
- {
- outFile << "The average = " << average << endl;
- }
- outFile << endl;
- }
- outFile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement