text file

This is a discussion on text file within the C++ Programming forums, part of the General Programming Boards category; can anyone tell me why there is 18 numbers in the text file instead of 10. thanks Code: #include <fstream> ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    text file

    can anyone tell me why there is 18 numbers in the text file instead of 10. thanks
    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    
    void rand_seed()
    {
    int seed = static_cast<int>(time(0));
    srand(seed);
    }
    
    
    int rand_int(int a, int b)
    {
    	return a + rand() % (b - a + 1);
    }
    
    	
    
    int main()
    {
    	int response;
    	
    	              do
    		
                                   {				
    		cout<<"Enter 1 for a list 2 to exit.";	
    		cin>>response;							
       		if (response == 1)
    		
                                     {
    			
    		          rand_seed();
    	                         ofstream fout;
    	                         fout.open("data.dat", ios::out);
    	
    
    		int arr [10];
    		rand_seed();
    		int i;					
    		for (i = 1; i <= 9; i++)
    		
    		{		
    			int d1 = rand_int(1, 99);
    			arr[i] = d1;
    			fout<<arr[i];
    		}
    	
    		fout.close();
    				
    		}
    		else
    		cout<<"Thank you!!!";				
                                    while (response == 1);
    		
     return 0;
    }
    Last edited by bj31t; 04-28-2004 at 12:52 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > can anyone tell me why there is 18 numbers in the text file instead of 10.
    Because your loop only outputs 9 numbers, not 10
    So I guess you ran it twice without deleting the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    FIRST
    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <cstdlib.h>
    #include <ctime.h>
    SECOND
    Code:
    for(i=0;i<9;i++)
    ....

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Quote Originally Posted by enjoy
    FIRST
    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <cstdlib.h>
    #include <ctime.h>
    ....
    Nope he was absolutely right with the headers he was using. Using fstream.h is not favorable as those headers are officially deprecated.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > #include <fstream.h>
    No, this is old standard C++

    #include <fstream>
    Followed by
    using namespace std;

    Is the new way.

    > for(i=0;i<9;i++)
    Loops just as many times as
    for(i=1;i<=9;i++)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by enjoy
    FIRST
    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <cstdlib.h>
    #include <ctime.h>
    SECOND
    Code:
    for(i=0;i<9;i++)
    ....
    I'm not sure if you meant it, but both your suggestions are wrong.

    The FIRST one says to use old, non-standard or deprecated headers. Not a good suggestion if bj31t is already using the newer headers.

    The SECOND one still says to print out 9 numbers, even though it corrects the problem with starting at 0.


    EDIT - Too Slow. And by the way, if I'm not mistaken, <iostream.h> and <fstream.h> are not deprecated, they are just old and non-standard. <stdlib.h> and <time.h> on the other hand are standard, but have been deprecated and with the newer headers being preferred.
    Last edited by jlou; 04-28-2004 at 03:51 PM.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Hehe...see that enjoy? Post something incorrect and you get 3 rebukes within the space of a minute! That's forums for ya!

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Darn I was only a few minutes too late! I had to go to the bathroom...

    Yea you're so wrong that I almost cried. Use the newer headers.

  9. #9
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    not all the compiler accept <fstrem> ===> so my compiler don't accept

    i advice to you using
    Code:
    *arr[10];
    to economise memory

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You obviously don't know C++ because or else you would not be saying that. *arr[10] makes no sense for this program and won't economize any memory. His array code is perfectly fine and plus we're talking about 10 ints... 20-80 bytes, w00t.

    Giving bad advice is not the way to go.
    Last edited by Speedy5; 04-28-2004 at 04:30 PM.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > not all the compiler accept <fstrem> ===> so my compiler don't accept
    Your compiler is too old then - consider something which isn't TurboC++ for DOS.

    > to economise memory
    Which would be to not have the array in the first place.

    > *arr[10];
    You would have to initialise all those pointers some how, thus using up even more memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Quote Originally Posted by enjoy
    not all the compiler accept <fstrem> ===> so my compiler don't accept

    i advice to you using
    Code:
    *arr[10];
    to economise memory
    PLEASE stop posting until you learn more.
    Do not make direct eye contact with me.

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    omg holy!!!! -bleep- SOMEONE PUT OUT THE FIRE!!!

    Jesus you would think any of this matters..

    He's obviously not asking for optimized code he's asking for a solution for his code..who cares what headers he's using? Even though <iostream> includes <fstream>...
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    thanks for the help!! i guess it would have helped if i had told you i was using c++.net. i dont think you have to use .h

  15. #15
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by Tronic
    omg holy!!!! -bleep- SOMEONE PUT OUT THE FIRE!!!

    Jesus you would think any of this matters..

    He's obviously not asking for optimized code he's asking for a solution for his code..who cares what headers he's using? Even though <iostream> includes <fstream>...
    The point was that one poster gave advice correcting the OP, but that was bad advice in the opinion of several of us. The fire didn't have to be so large, but since all three of us posted at roughly the same time, it came off worse than it was.

    I hope you'd agree that it does matter when one poster incorrectly advises someone to change their code from right to wrong.

    And by the way, <iostream> does not include <fstream>, at least not on most platforms.

    -------

    bj31t,

    Did you figure out your for loop problem?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. checking values in a text file
    By darfader in forum C Programming
    Replies: 2
    Last Post: 09-24-2003, 02:13 AM
  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