Why do I have an OBOB error?

This is a discussion on Why do I have an OBOB error? within the C++ Programming forums, part of the General Programming Boards category; Code: void get_numbers(char *data_array, int& num_amount, ifstream& fin){ int num; //used for temporary storage of a number obtained through the ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    155

    Why do I have an OBOB error?

    Code:
    void get_numbers(char *data_array, int& num_amount, ifstream& fin){
    	int num; //used for temporary storage of a number obtained through the input stream
    	int index = 1;  //this is to be used to begin storing data into data_array[1] below
    	char comma_discard;  //used to disregard commas in the code file
    	clrscr();
    	
    	while (!fin.eof()){
    		fin >> num;
    		cout << num;
    		//*(data_array + index) = num;
    		//cout << int(*(data_array + index)) << " ";
    		num_amount++;
    		index++;
    		fin.get(comma_discard);
    		cout << comma_discard;
    		
    	}
    	getch();
    	
    } //end void get_numbers
    Contents of what the input stream is connected to:

    Code:
    115,73,24,807,37,52,49,17,31,62,647,22,7,15,140,47,29,107,79,84,56,239,10,26,811,5,196,308,85,52,160,136,59,211,36,9,46,316,554,122,106,95,53,58,2,42,7,35,122,53,31,82,77,250,196,56,96,118,71,40,287,28,353,37,1005,65,147,807,24,3,8,12,47,43,59,807,45,316,101,41,78,154,1005,122,138,191,16,77,49,102,57,72,34,73,85,35,371,59,196,81,92,191,106,273,60,394,620,270,220,106,388,287,63,3,6,191,122,43,234,400,106,290,314,47,48,81,96,26,115,92,158,191,110,77,85,197,46,10,113,140,353,48,120,106,2,607,61,420,811,29,125,14,20,37,105,28,248,16,159,7,35,19,301,125,110,486,287,98,117,511,62,51,220,37,113,140,807,138,540,8,44,287,388,117,18,79,344,34,20,59,511,548,107,603,220,7,66,154,41,20,50,6,575,122,154,248,110,61,52,33,30,5,38,8,14,84,57,540,217,115,71,29,84,63,43,131,29,138,47,73,239,540,52,53,79,118,51,44,63,196,12,239,112,3,49,79,353,105,56,371,557,211,505,125,360,133,143,101,15,284,540,252,14,205,140,344,26,811,138,115,48,73,34,205,316,607,63,220,7,52,150,44,52,16,40,37,158,807,37,121,12,95,10,15,35,12,131,62,115,102,807,49,53,135,138,30,31,62,67,41,85,63,10,106,807,138,8,113,20,32,33,37,353,287,140,47,85,50,37,49,47,64,6,7,71,33,4,43,47,63,1,27,600,208,230,15,191,246,85,94,511,2,270,20,39,7,33,44,22,40,7,10,3,811,106,44,486,230,353,211,200,31,10,38,140,297,61,603,320,302,666,287,2,44,33,32,511,548,10,6,250,557,246,53,37,52,83,47,320,38,33,807,7,44,30,31,250,10,15,35,106,160,113,31,102,406,230,540,320,29,66,33,101,807,138,301,316,353,320,220,37,52,28,540,320,33,8,48,107,50,811,7,2,113,73,16,125,11,110,67,102,807,33,59,81,158,38,43,581,138,19,85,400,38,43,77,14,27,8,47,138,63,140,44,35,22,177,106,250,314,217,2,10,7,1005,4,20,25,44,48,7,26,46,110,230,807,191,34,112,147,44,110,121,125,96,41,51,50,140,56,47,152,540,63,807,28,42,250,138,582,98,643,32,107,140,112,26,85,138,540,53,20,125,371,38,36,10,52,118,136,102,420,150,112,71,14,20,7,24,18,12,807,37,67,110,62,33,21,95,220,511,102,811,30,83,84,305,620,15,2,108,220,106,353,105,106,60,275,72,8,50,205,185,112,125,540,65,106,807,188,96,110,16,73,32,807,150,409,400,50,154,285,96,106,316,270,205,101,811,400,8,44,37,52,40,241,34,205,38,16,46,47,85,24,44,15,64,73,138,807,85,78,110,33,420,505,53,37,38,22,31,10,110,106,101,140,15,38,3,5,44,7,98,287,135,150,96,33,84,125,807,191,96,511,118,440,370,643,466,106,41,107,603,220,275,30,150,105,49,53,287,250,208,134,7,53,12,47,85,63,138,110,21,112,140,485,486,505,14,73,84,575,1005,150,200,16,42,5,4,25,42,8,16,811,125,160,32,205,603,807,81,96,405,41,600,136,14,20,28,26,353,302,246,8,131,160,140,84,440,42,16,811,40,67,101,102,194,138,205,51,63,241,540,122,8,10,63,140,47,48,140,288,
    Problem is, an extra "288," is being read in at the very end and I do not know why. Technically once that last fin.get() eats that last comma shouldn't that be eof?

  2. #2
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    what's the other function do?

    also, try just removing the extra comma
    Last edited by Diamonds; 10-14-2002 at 08:19 PM.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>while (!fin.eof()){
    classic problem, I believe. You be better off checking the return code from the reading function to see if you're at the end of the file. Or at least checking the file status after each read.

    The problem is some systems don't recognise EOF until they've hit it, which in your code is too late.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    It makes no sense... there is no reason why it should be getting that value twice

  5. #5
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    it's becuase it doesn't get read in the second time...and just keeps the value. instead of an error when the program tries to read nothing, it just keeps the same value for the variable, and it's printed twice thusly

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Why won't it read the eof?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Nakeerb
    It makes no sense... there is no reason why it should be getting that value twice
    Code:
    while (!fin.eof()){
    		fin >> num;
    		cout << num;
    Say the stream has just read the last lot of input from the file, enough to satisfy the fin.get(comma_discard); call. It goes to the top of the loop and says am I at EOF? Well, I haven't got there yet, so continue with the loop. fin>>num then attempts a read and finds there's no input left, and this is when EOF is set. Next, you blindly cout<<num for the second time and so on. I've seen this work two different ways on two compilers...

    Try adding a test after the fin>>num line, and if the file stream is at EOF, break out of the loop.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    fix: remove the last comma

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    This is a school assignment and the text must remain untouched

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    To Hammer: I see, so technically it must "read the eof marker" first? Hmmm, perhaps I should reverse my loop a bit and perhaps put the fin >> stuff at the end and put one extra fin >> BEFORE the loop?

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Code:
    void get_numbers(char *data_array, int& num_amount, ifstream& fin){
    	int num; //used for temporary storage of a number obtained through the input stream
    	int index = 1;  //this is to be used to begin storing data into data_array[1] below
    	char comma_discard;  //used to disregard commas in the code file
    	clrscr();
    	
    	fin >> num;
    	
    	while (!fin.eof()){
    		cout << num;
    		//*(data_array + index) = num;
    		//cout << int(*(data_array + index)) << " ";
    		num_amount++;
    		index++;
    		fin.get(comma_discard);
    		cout << comma_discard;
    		fin >> num;
    		
    	}
    	getch();
    	
    } //end void get_numbers
    Thanks Hammer! That explanation really helped me

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Of course, now I get access violation errors when I remove the comments for the *data_array stuff

  13. #13
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    *(data_array + index) = num;
    is bad

    should be
    variable = statement...

    not
    variable + variable = something

    if you want num assigned to data_array + index then...
    num = data_array + index;

  14. #14
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    i think you want

    data_array[index] = num;

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >*(data_array + index) = num;
    is a problem, but not because of the addition. Remember that data_array is a char pointer, so adding index to it is valid. A problem will come about when trying to store the int num variable into a one character element of the array.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

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