input multiple data types from file to array

Hi all, so I've been trying to put 3 different data types into 3 different arrays from an input file. However after the first 3 items it can't seem to read in the rest of the values properly. I can't seem to find the bug, if anyone can help me it'l be much appreciated. Thanks!

my input file looks like this:

Alpha Sigma
83 65.48
Beta Sigma
97 2.20
Normandy Falling
899 369.44

and this is my code:
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
  	#include <iostream>
#include <fstream>
using namespace std;

int main()
{

	ifstream inputFile;
	string namesAr[3];
	int radAr[3];
	float cadAr[3];
	int index;

	inputFile.open("infile.txt");

	for(index = 0; index <= 3; ++index)
	{
		getline(inputFile, namesAr[index]);
		inputFile >> radAr[index];
		inputFile >> cadAr[index];
	}

	inputFile.close();

	for(index = 0; index <= 3; ++index)
	{
		cout << namesAr[index] << " " << radAr[index] << " " <<  cadAr[index] << endl;
	}

	return 0;
}
http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction
Solution: add std::cin >> std::ws; after line 20. DO not forget fo #include <iomanip>

Also you should not use parallel arrays. For example create a struct with 3 fields and uuse single aray of structs.
so i tried that, but it still didn't work. After putting the cin.ignore i get no output, I'm also on linux(if that makes a difference). I get what your trying to say, but I don't think thats the problem :/
Prefer to use >> std::ws. Also show resulting code.

What do you actually mean by 3 items? Is it 3 values (Alpha Sigma 83 65.48) or 3 sets of values (all your example input)?
3 values and the result code is nothing, my output console is just empty(sorry not sure how to show that)
Show source code that you got now after modifications.
oh my bad
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>
#include <iomanip>
using namespace std;

int main()
{

	ifstream inputFile;
	string namesAr[3];
	int radAr[3];
	float cadAr[3];
	int index;

	inputFile.open("infile.txt");

	for(index = 0; index <= 3; ++index)
	{
		getline(inputFile, namesAr[index]);
		inputFile >> radAr[index];
		inputFile >> cadAr[index];
		cin.ignore();
	}

	inputFile.close();

	for(index = 0; index <= 3; ++index)
	{
		cout << namesAr[index] << " " << radAr[index] << " " << cadAr[index] << endl;
	}

	return 0;
}

did i do it right?
Oh. std::cin was an example. You should apply operation to whichever stream you are operating on. In your case it is inputFile. inputFile.ignore();. And I still suggest to use >> ws. It ever says so in article I linked.

Also I want to say that correct condition for loops is index < 3. In current state it will try to access namesAr[3] eventually which can lead to crash.
Last edited on
ah thanks man, i think i get it now
Last edited on
To quote article I linked:
What's the difference?

The difference is that ignore(std::streamsize count = 1, int_type delim = Traits::eof())3 indiscriminately discards characters until it either discards count characters, finds the delimiter (specified by the second argument delim) or hits the end of the stream. std::ws is only used for discarding whitespace characters from the beginning of the stream.

If you are mixing formatted input with unformatted input and you need to discard residual whitespace, use std::ws. Otherwise, if you need to clear out invalid input regardless of what it is, use ignore(). In our example, we only need to clear whitespace since the stream consumed your input of "John" for the name variable. All that was left was the newline character.
Thanks man, you were a big help
Topic archived. No new replies allowed.