Reading a .txt into an array

I am new at C++ (of course) and am having problems reading a .txt file into an array. I really don't have a full grasp of arrays.

My .txt file looks like this.

1012 C
25 E
34567 R
987789 D
123 D
45320 D
555 R
123456 E
40506 R
111 E
13 R
12 C
1213 D
44444 R
19191 C

One column is for song ID's and the other is for genre.

My code so far goes...
***********************************************

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int count;
const int NUM_SONGS = 15;
int SONGS[NUM_SONGS];
ifstream inputFile;

inputFile.open ("songData.txt"); 	

for (count = 0; count < NUM_SONGS; count++)
inputFile >> SONGS[count];
inputFile.close();

cout << "The songs are " << endl;
for (count = 0; count < NUM_SONGS; count++)
cout << SONGS[count] << endl;

******************************************

now I know I am way off. I need to read the two columns into two different arrays.

When i run this, I get "-858993460" 15 times. Can anyone help me out?
Well, you are reading in integer types in a text format. You usually want to do that in binary.
Can you change your song array to an array of characters?

I would like to read them as int's because I don't know anything about binary. I need to make two parallel arrays. One being an int, the other being a char. I just don't know how to read in the file into two arrays the way it is formatted. and I'm not aloud to change the file (its for a class)
Hello,
you must convert the characters (the numbers in the file aren't numbers, they are characters) to ints. atoi helps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int count;
const int NUM_SONGS = 15;
int SONGS[NUM_SONGS];
int GENRE[NUM_SONGS];
ifstream inputFile;

inputFile.open("songData.txt");

for (count = 0; count < NUM_SONGS; count++)
{
    char cDigits[16] = {0}; // memory for numbers with max 15 digits
    cDigits[count] << inputFile;
    SONGS[count] = atoi(cDigits);
    GENRE[count] << inputFile; // don't know if fstream ignores the whitespace
}
inputFile.close();
// do something with the arrays

mfg goti
thanks for helping!!!!!

i don't understand what the cDigits is. That's converting my char to ints? and I have never seen atoi before.
ok i tried something new but it doesn't work but I think I'm closer.

GLOBAL VARIABLES
1
2
3
4
5
6
7
const int NUM_SONGS = 15;

struct SONGS
{
int SONG_ID;
char GENRE;
};



in Main

1
2
3
4
5
6
7
8
9
10
11
12
13
int count;
ifstream inputFile;

inputFile.open("songData.txt");

for (count = 0; count < NUM_SONGS; count++)
{
	inputFile >> SONGS.SONG_ID[count];
	inputFile >> SONGS.GENRE[count];
}

for ( count = 0;  count < NUM_SONGS; count++ )
		cout << SONGS.SONG_ID[count] << " " << SONGS.GENRE[count] << endl;


I think I'm close but I get an error that says I can't use "token '.' "
Last edited on
Greetings fellow newb,

Ok here is the thing, you declared the struct 'SONGS' very well, but you made a simply CRITICAL mistake. When you created SONGS think of it like an int or a char because thats what it is, A CUSTOM, USER -DEFINED, TYPE!! What you did was refering to thats type without defining a variable, heres what you need to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<fstream>

using namespace std;
const int MAX_ELEMENTS = 15;
class SONG {
       public:
              int songID[MAX_ELEMENTS];
              char songGenre[MAX_ELEMENTS];
              void show(){
                   for(int k = 0; k < MAX_ELEMENTS; ++k)
                      cout << songID[k] << ' ' << songGenre[k] << endl;
                   }
       };
int main(){
    
    ->>SONG mySong;<<-
    ifstream inputFile("songData.txt");
    
    for(int i = 0; i < MAX_ELEMENTS; ++i){
            inputFile >> mySong.songID[i] >> mySong.songGenre[i];
            }
    
    mySong.show();
    
            
    system("Pause");
}


Hope it helped!
Post again if you require further help! :D
You were really close with your original code. The problem is that your input file reads a number then a character. So to make it work, you need to read the number into an array of integers (yes you can do this from a txt file) and then you need to read the character into an array of characters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>

using namespace std;

int main()
{

	int count;
	const int NUM_SONGS = 15;
	int SONGS[NUM_SONGS];
	char genre[NUM_SONGS];
	ifstream inputFile;

	inputFile.open ("songData.txt"); 	

	for (count = 0; count < NUM_SONGS; count++)
	{
		inputFile >> SONGS[count];
		inputFile >> genre[count];
	}

	inputFile.close();

	cout << "The songs are " << endl;

	for (count = 0; count < NUM_SONGS; count++)
		cout << SONGS[count] << endl;

	system("pause");

	return 0;
}
Topic archived. No new replies allowed.