0

I've been trying to read in values from a text file into 2 arrays, however I end up with nothing inside my names and scores arrays. Here's what I have:

const int size = 6;
int names[size] = { 0 };
int scores[size] = { 0 };
int name;
ifstream inputFile;
inputFile.open("input.txt"); //opens up textfile

inputFile >> name;
while (!inputFile.eof()){
    inputFile >> names[x] >> scores[x];
    cout << names[x] << scores[x];
    x++;
}

input.txt

6
Alice 50
Bob 100
Cathryn 75
Don 90
Emily 80
Flora 60
George 95

name is picking up a value of 6, but names and scores are picking up nothing. Any ideas about what's wrong?

9
  • have you initialized x? like x = 0? Commented Aug 5, 2014 at 23:20
  • Show us the declaration for your names and scores array. Commented Aug 5, 2014 at 23:20
  • Try cout << inputFile instead and see if the file is actually being read. Commented Aug 5, 2014 at 23:21
  • and now where is size? :-) Commented Aug 5, 2014 at 23:22
  • Is size a constant? Commented Aug 5, 2014 at 23:22

1 Answer 1

2

You're program doesn't work because you accidentally initialized names as an array type int, instead of type std::string. This breaks the whole line inputFile >> names[x] >> scores[x];.

Silly mistake. Just make a data structure of std::strings called names and put stuff in it.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm so out of it... Thanks gragas!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.