hello
i am trying to make a program that will take numbers entered output them to a text file and compute the average and the standard deviation from the numbers in the file. I can enter the numbers fine and the program will write them to the file fine. I dont know how to make the program read all of the numbers that i have entered. the program will read the last number that i entered but nothing else. so like if i entered 3 numbers it would read only the last number i entered.
Code:
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
void asknumbers(ofstream& myfile,int& howmanynumbers,double& numbers);
void readnumbers(ifstream& outfile, double numbers, double& total,int howmanynumbers);
int main()
{
ofstream myfile;
ifstream outfile;
int howmanynumbers;
double numbers,total;
char repeat;
do
{
myfile.open("numbers_file.txt",ios::trunc); //opens and deletes everything from before
myfile.close(); //closes file
asknumbers(myfile,howmanynumbers,numbers); //function for asking numbers
readnumbers(outfile,numbers,total,howmanynumbers);
cout<<"Do you want to repeat: ";
cin>>repeat;
system("cls");
}while(repeat=='y'||repeat=='Y'); //end first do loop
system("pause");
return 0;
}
void asknumbers(ofstream& myfile,int& howmanynumbers,double& numbers)
{
cout<<"how many numbers do you want to input:"; //asks how many number i want to put in
cin>>howmanynumbers;
int numbercounter;
numbercounter=howmanynumbers;
do //asks for the numbers
{
cout<<"enter the numbers:";
cin>>numbers;
myfile.open ("numbers_file.txt",ios::app); //writes to file called numbers_file.txt
myfile<<numbers<<endl; //writes numbers to the file
myfile.close(); //closes file
numbercounter--; //counts down how many numbers put in
}while(numbercounter>=1); //ends asking for numbers
}
void readnumbers(ifstream& outfile,double numbers, double& total,int howmanynumbers)
{
total=0;
outfile.open("numbers_file.txt"); //opens file
for(int x=howmanynumbers;x>0;x--)
{
outfile>>numbers; //reads numbers
}
total+=numbers; //calculates total from numbers input
cout<<"The total of the numbers is: "<<total<<endl;
}