Sum of external file

This is a discussion on Sum of external file within the C++ Programming forums, part of the General Programming Boards category; How do I make a loop that will take numbers from an external file and then list the numbers on ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    17

    Sum of external file

    How do I make a loop that will take numbers from an external file and then list the numbers on the screen, then calculate the sum of those numbers and show it?
    I'm desperate
    Thanks

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Have you got any code to start with?
    Have you read the FAQ? If this is home work nobody will write your program for you.
    Make a start, post your code, and we'll help you get it right.

  3. #3
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499

    I am bored

    Code:
    #include <fstream> 
    #include <iostream> 
    using namespace std; 
    const size_t MAX = 10;
    
    int main() 
    { 
    	{ 
    		int i = 0,
    			numbers[MAX],
    			number = 0,
    			sum_of_num = 0; 
    		
    		//LOAD THE DATA INTO AN ARRAY 
    		for(i = 0;i < MAX;i++)
    		{ 
    			cout<<"Enter a number:";cin>>number; 
    			numbers[i] = number; 
    		} 
    		
    		//CREATE AN OUTPUT FILE AND WRITE THE DATA TO IT
    		fstream file; 
    		file.open("MyNumbers.txt",ios::out|ios::binary);
    		file.write((char *)numbers,MAX * sizeof(int)); 
    		file.close(); 
    		
    		//RE-OPEN THE FILE AS INPUT AND READ FROM IT
    		file.open("MyNumbers.txt",ios::in|ios::binary); 
    		file.read((char *)numbers, MAX*sizeof(int)); 
    		
    		//PRINT AND SUM WHILE TRAVERSING THE ARRAY
    		for(i = 0;i < MAX;i++)
    		{ 
    			cout<<numbers[i]<<endl;
    			sum_of_num +=numbers[i]; 
    		} 
    		
    		//DISPLAY THE TRUTH AND THE SUM
    		cout<<"This is your early christmas present"
    			<<endl
    			<<"The sum is "
    			<<sum_of_num; 
    	}
    	return 0; 
    }
    Last edited by DISGUISED; 05-08-2002 at 08:23 PM.

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Of course, I've been wrong before. There's always somebody willing to show they can open a file and add a couple of variables. I now stand humbled and corrected, and suggest that we re-write the FAQ to read.... "....Don't bother attempting anything for yourself, just post your issue and someone will come forth with the code"

    That having been said, the code looked quite tidy I must say.

  5. #5
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    Originally posted by Azuth
    Of course, I've been wrong before. There's always somebody willing to show they can open a file and add a couple of variables. I now stand humbled and corrected, and suggest that we re-write the FAQ to read.... "....Don't bother attempting anything for yourself, just post your issue and someone will come forth with the code"

    That having been said, the code looked quite tidy I must say.
    The guy was close. I guess you didn't see this thread did ya?
    http://www.cprogramming.com/cboard/s...threadid=17312

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    17
    How do I add a loop that adds the sum of the scores?
    Everytime I do it, something goes wrong?
    Is it even possible or do I need to use an array?

  7. #7
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    Originally posted by robjules
    How do I add a loop that adds the sum of the scores?
    Do you mean add the scores to form a sum? If that's the case look at the second half of the program above. The easiest way to do this is to just open your file, read the file into an array, and then just process the array. Show me what you got.

    -Disguised

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    17
    I'll try that and let you know
    You been helpful, thanks.
    I may not get back here tonight but be sure to check back tomorrow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 02:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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