Reading .txt file into array. Bowling Game!

Hi guys, I'm working on a school project. Basically, I have a .txt file with integers in it. They are the scores of a bowling game.
9 0
3 5
6 1
3 6
8 1
5 3
2 5
8 0
7 1
8 1 0

This is what the program should look like.

Please enter the name of a score file: example1.txt
9-0 3-5 6-1 3-6 8-1 5-3 2-5 8-0 7-1 8-1-0

I feel like I have tried everything to get my code to work. So far this is what I have.


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
34
35
36
37
38
39
40
#include<iostream>
#include<string>
#include<cstdlib>
#include<cmath>
#include<fstream>
#include <iomanip>
using namespace std;

int scoreArray[21];



int main(){
	
	
	string filename;
	ifstream inputFile;
	cout<<"Please enter the name of a score file: ";
	cin>>filename;
	
	inputFile.open(filename.c_str());
		if (inputFile.fail()){//checks to see if opening failed
		cout<<filename<<"no such file or directory";
		exit(1);}
	int i;
	
	
	for( int i=0;i<9;++i){
		
		inputFile>>scoreArray[i];
		//cout<<scoreArray[i];
		cout<<scoreArray[2*i]<<" - "<<scoreArray[2*i+1]<<"\t"    <<endl;//print rolls[2*i] and rolls[2*i+1]
		
	} 
	inputFile.close();
return 0;	



}



The output I get is this:
9 - 0 0 - 0 0 - 0 0 - 0 0 - 0 0 - 0 0 - 0 0 - 0 0 - 0

I'm sorry I don't have a specific question, I just feel like I have tried all the different possibilities out there. Anyone that even helps a little bit, Thank you so much!
Last edited on
First thing I notice is there are total 21 numbers you need to read in, so your for loop need to be change. The second thing is you trying to read out something that have nothing in to.
On line 32:
cout<<scoreArray[2*i]<<" - "<<scoreArray[2*i+1]<<"\t" <<endl;//print rolls[2*i] and rolls[2*i+1]

The array is not completely fill yet so you will see 0. Try to fill the array first then using another loop to see out the content.
Thank You for replying!!! How would I fill the array? I tried doing a while loop before the for loop that was something like...

while(inputFile){
inputFile>>scoreArray[21];
}

And also, my for loop goes to 9 because I am supposed to output the numbers by twos. As you can see I'm trying to output two numbers at the same time. Or at least thats what my TA said...
What you could do is take out the cout statement in the for loop so that the loop would only be use for filling the array, then use another for loop to display the contents.

Even though you would be displaying the numbers by twos there would still be 21 elements within the array there fore you would need the loop to be 21 to see all the content of the array for example:
1
2
3
4
for( int i=0;i<21;++i){
		cout<<scoreArray[i]<<" - "<<scoreArray[i+1]<<"\t"  <<endl;
		i+=1;
	}

As you can see after displayed the numbers by twos I added one to i so that the loop would skip the already displayed numbers. For example if i = 0 then cout << cout<<scoreArray[i] would display 9 and cout<<scoreArray[i+1] would display the 0. Then when I added one to i i would become 1 and when the loop come around again it the loop would cause i to become 2. This would continue until i is greater than 21.
Last edited on
Topic archived. No new replies allowed.