File I/O calculating total help

This is a discussion on File I/O calculating total help within the C++ Programming forums, part of the General Programming Boards category; Hello I am working on a program where I have to input numbers(as many as I want) and then I ...

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

    File I/O calculating total help

    Hello

    I am working on a program where I have to input numbers(as many as I want) and then I want to get the Average and the Standard Deviation from those numbers. I could do this easily if I didn't have to use file i/o. I don't understand how to read the numbers one I have written them to a text file. another thing is that when the program goes to calculate the total it dont do it right. im not sure whats up. if someone could please help me or give me some pointers..
    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 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);
    		
    	
    
    		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)
    {
    	outfile.open("numbers_file.txt");						//opens file
    	outfile>>numbers;										//reads numbers
    	total+=numbers+0;										//calculates total from numbers input
    	cout<<"The total of the numbers is: "<<total<<endl;
    	
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Look inside your readnumbers function. How many times does it read from the file?

  3. #3
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    Once...but that's all I thought it needed to do was read the whole file once to get the numbers. If I output the numbers that it reads it only reads the last number that was entered and not all of the numbers that were entered.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    If there are many numbers in the file, and it only reads one number from the file, then how is that all you need to do? What about all the rest of the numbers in the file? Your code in the readnumbers function only reads in one number, not all the numbers at once.

    What happens if you open the file yourself in notepad or something after running the program? Does it have all the numbers you entered?

  5. #5
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    That's my problem, I don't know how to get my program to read all of the numbers that I entered. When I do run the program and enter multiple numbers, it does have all of the numbers I entered. I would think that if I looped the reading numbers part it would just read the last number I put in.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> I would think that if I looped the reading numbers part it would just read the last number I put in.

    If you looped, it read each number one at a time, and you have to figure out what to do with each number as you read it in. You already have code there that adds to the total, what about using that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM

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