Hello Dan,
Let me ask you this: Are you intending to use a 2D array to store the ISBN code under 1 column and the book name under the other?
Well that won't work, because an array must have a single type. Now you can declare a 1D array of
pair<int, char *>
or
pair<int, string>
but that would be overdoing it.
My solution for you is continue storing code and name in 2 different arrays but at the same index, say:
isbn[0]
would give you 9780743486224 and
title[0]
would give you Angels and Demons. (Here title is assumed to be of type string)
To do that, read file as string or lines using the
getline
function in C (not C++ though the latter would do it too). This will allow you to read the entire line as one sentence, then use the
sscanf
function to extract the integer into
isbn
and the string into
title
. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
int main()
{
char * text[] = {"9780743486224 Angels_and_Demons", "9780684835501 The_Case_for_Mars"};
char title[2][32];
char isbn[2][32];
for (int i = 0; i < 2; i++)
{
char ttitle[32];
char tisbn[32];
sscanf(text[i], "%s %s", tisbn, ttitle);
strcpy(title[i], ttitle);
strcpy(isbn[i], tisbn);
}
/* display results */
for (int i = 0; i < 2; i++)
{
std::cout << isbn[i] << ": " << title[i] << std::endl;
}
return 0;
}
|
I would use underscores for names, as it is much easier to read them. You can freely extract them later. Plus, no such ISBN numbers fit in
int
, not even in an
unsigned long
. So it's better storing them as C strings. You also need to be careful when dealing with arrays of C strings as it appears to be dealign with 2D arrays (a single word/C string is a 1D array).