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)[2], int& num_amount, ifstream& fin){ int num; //used for temporary storage of a number obtained through the ...

  1. #16
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Code:
    void get_numbers(char (*data_array)[2], 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;   //get first data item
    	
    	while (!fin.eof()){  //while the eof marker has not yet been read
    		*(*(data_array + index)) = num;
    		cout << int(*(*(data_array + index))) << " ";
    		getch();
    		num_amount++;
    		index++;
    		fin.get(comma_discard);   //"eats" the commas after the integer values in the code file
    		fin >> num;               //get next data item
    		
    	}
    	
    	getch();
    	
    } //end void get_numbers
    Works to an extent but then it becomes garbled and I get access violation errors
    Last edited by Nakeerb; 10-14-2002 at 07:44 PM.

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What are you trying to do? You cannot store an int in a char (unless you want to loose a load of data).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    It wants me to use a two-dimensional array of chars and then use pointers to traverse the list.

    I need to be able to read in every number into one row of an array, and the other row must contain the first letter of every word in another file.

  4. #19
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    However, I *could* use two arrays, an int array for the numbers and a char array for the letters, but the teacher had mentioned something about using a two-dimensional, so :/

  5. #20
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    what crazy teacher would assign such a thing requiring such strange datastructures.

    ok so for numbers and characters....

    in C++ i know this:

    if ('A' == 65)

    will evaluate to true

    I *think* you can set
    char bleh = 65;
    and
    cout<<bleh;

    and have it print 'A'

    but I don't remember (summer makes me forget everything )

  6. #21
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    That is true, but I need it the other way around. Putting integers into char arrays

  7. #22
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    doesn't this work?

    char bleh[2];

    bleh[0] = 65;
    bleh[1] = 66;
    cout<<bleh<<endl;


    should print AB no?

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    RE: Putting integers into char arrays...

    You're best served doing it the other way around. Don't put a larget data type into a smaller data type. It is bad design. If your teacher marks you down for it, tell them it's bad form and is error prone.

    Assuming your integer values are never greater than 127, sure, use a char array. Otherwise, just do:

    int array[N][2]; /*or*/
    int array[2][N]; /* it's a matter of personal preference */

    Where N is the number of elements you want in the array.

    Use array[][0] for characters and array[][1] entries for integers (or the other way around).

    Simply do:

    array[SOMETHING][SOMETHING] = 'a';
    or...
    array[SOMETHING][SOMETHING] = 123456;

    Vola.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Diamonds
    doesn't this work?

    char bleh[2];

    bleh[0] = 65;
    bleh[1] = 66;
    cout<<bleh<<endl;


    should print AB no?
    No. It shouldn't. You're treating it like a string. This not a string. It isn't null terminated. If you want it to print correctly you should do:

    cout << bleh[0] << bleh[1] << endl;

    Specify the cells of the array so it prints the characters seperately, or add another element to your array and have it hold the NULL so it is a valid string.

    Quzah.
    Hope is the first step on the road to disappointment.

Page 2 of 2 FirstFirst 12
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, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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