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 countr, int number []) //should average + total be passed in? gets mad when I don't declare total
- {
- int total = 0;
- for (int a = 0; a<10; a++)
- {
- total = total + number [a];
- }
- cout<<total<<endl; //this should add the numbers together as it loops
- double answer = double (total)/countr;
- cout<<answer; //average is NOT correct (?)
- return answer;
- }
- int main()
- {
- int i=0;
- int countr=0;
- int total=0;
- char comma = ','; //stops the reading at comma
- cout<<"ok"<<endl;
- ifstream infile;
- char cNum[40]; //40 is size of the array
- int number [40];
- //this is NOT working, something is wrong how I am declaring/calling function and variables
- cout<<"ok"<<endl;
- ofstream outFile;
- cout<<"ok"<<endl;
- 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
- cout<<countr<<endl;
- }
- else
- {
- number[i-1] = atoi(cNum);
- cout<<number[i-1]<<endl;
- //all other numbers are put into the array number
- }
- i++;
- }
- infile.close();
- }
- else
- {
- cout << "Error opening file";
- }
- cout<<"ok"<<endl;
- double average = avg (countr, number);
- cout<<"ok"<<endl;
- outFile.open("stats.txt");
- if (outFile.is_open())
- {
- for (int j = 0; j<10; j++)
- {
- outFile << setw(5) << "The data set is:" << number [j] << endl; //how to get numbers to output in nice way? output at all?
- }
- outFile << "The average = " << average << endl;
- outFile << endl;
- }
- outFile.close();
- return 0;
- }
Add Comment
Please, Sign In to add comment