daily pastebin goal
66%
SHARE
TWEET

weakkkkkkkkk

payjack Nov 20th, 2017 (edited) 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <stdlib.h>
  5. #include <iomanip>
  6. #include <math.h>
  7. using namespace std;
  8.  
  9. double avg (int total, int countr, int number []) //should average + total be passed in? gets mad when I don't declare total
  10.         {
  11.             for (int i = 1; i > 0; i++)
  12.                 total = total + number [i]; //this should add the numbers together as it loops
  13.                 double average = total/countr; //average is NOT correct (?)
  14.                 return average;
  15.         }
  16.  
  17. int main()
  18. {
  19. int i=0;
  20. int countr=0;
  21. char comma = ','; //stops the reading at comma
  22.  
  23. ifstream infile;
  24.  
  25. char cNum[40]; //40 is size of the array
  26. int number [40];
  27. int total;
  28. double average = avg (total, countr, number); //this is NOT working, something is wrong how I am declaring/calling function and variables
  29.  
  30. infile.open ("listOfIntegers.txt", ifstream::in);
  31. if (infile.is_open())
  32.     {
  33.     while (infile.good())
  34.         {
  35.             infile.getline(cNum, 256, ',');
  36.             if (i==0) //first number is the amount of numbers = 0 is the first in an array
  37.             {
  38.                 countr = atoi(cNum); //gets it to be an integer
  39.                 i++;
  40.             }
  41.             else
  42.               {
  43.                   number[i-1] = atoi(cNum); //all other numbers are put into the array number
  44.               }
  45.         }
  46.         infile.close();
  47.     }
  48.     else
  49.     {
  50.         cout << "Error opening file";
  51.     }
  52.  
  53.     ofstream outFile;
  54.     outFile.open("stats.txt");
  55.     if (outFile.is_open())
  56.     {
  57.         for (int j = 0; j<40; j++)
  58.         {
  59.             outFile << setw(5) << "The data set is:" << number << endl; //how to get numbers to output in nice way? output at all?
  60.         }
  61.         for (int j = 0; j<=40; j++)
  62.         {
  63.             outFile << "The average = " << average << endl;
  64.         }
  65.         outFile << endl;
  66.     }
  67.     outFile.close();
  68. return 0;
  69. }
RAW Paste Data
Top