reading whole .txt file after entering numbers

This is a discussion on reading whole .txt file after entering numbers within the C++ Programming forums, part of the General Programming Boards category; hello i am trying to make a program that will take numbers entered output them to a text file and ...

  1. #1
    new to c++
    Join Date
    Feb 2009
    Posts
    53

    reading whole .txt file after entering numbers

    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;
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    readnumbers is reading all of the numbers, but each time it executes the line
    Code:
    outfile>>numbers;
    the previous entry is overwritten by the next one so you're not doing anything with any but the last entry. The line
    Code:
    total += numbers:
    should be inside the for loop.

    Also, in your asknumbers function, instead of opening and closing the file on every iteration of the do-while loop, you should open the file just once before the loop, and close it after the loop.

  3. #3
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    Thanks

    I have another question about how i would calculate the standard deviation...
    I know that the standard deviation is calculated as:
    sqrt((n1-avg)^2+(n2-avg)^2+(n3-avg)^2+...)/howmanynumbers) right?
    I guess my question is how would I get my program to get the n1,n2,n3, variables and so on into that equation?
    I have this
    Code:
    void standarddev(ifstream& outfile, double numbers,double average,double& standardd,int howmanynumbers)
    {
    	outfile.open("numbers_file.txt");								//opens file
    	for(int x=howmanynumbers;x>0;x--)
    	{
    		standardd=sqrt(pow((numbers-average),2)/average);
    	}
    	cout<<"The standard deviation of those numbers is: "<<standardd<<endl;
    }
    but I dont think its right, could I get some help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help converting CANoe logfile to EXCEL readable .txt file
    By turbofizzl in forum C Programming
    Replies: 21
    Last Post: 08-16-2009, 08:56 PM
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21