payjack

puttingittogether

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