My program is not printing what i want it to print.
#include<cstdlib>
#include<cmath>
#include<fstream>
#include<sstream>
#include<iomanip>
#include<iostream>
#include<string>
#include<cstring>
#include<cassert>
#include<ctime>
#include<cctype>
#include<algorithm>
#include<locale.h>
#include<stdio.h>
#include<functional>
#include<math.h>
using namespace std;
int main(int argc, char**argv)
{
int r = 0;
int p = 0;
int c = 0;
string names[20];
double scores[20][10];
ifstream infile;
infile.open("C:\\Users\\Colin\\Documents\\NetBeansProjects\\Bowlerspart2\\data\\bowlers.txt");
while(!infile)
{
cout << "can not find file" << endl;
return 1;
}
for(r = 1; r <= 10; r++)
{
getline(infile, names[r]);
for(c = 1; c <= 3; c++)
{
infile >> scores[r][c];
}
}
infile.close();
for(r = 1; r <= 10; r++)
{
cout << names[r] << endl;
cout << fixed << setprecision(2) << endl;
cout << scores[r][c] << endl;
}
return 0;
}
It only prints one of the names and prints 0.00 for all the scores. I believe I'm probably reading the file wrong, but not sure how.
Here is the text file:
Linus too good
100
23
210
Charlie brown
1
2
12
Snoopy
300
300
100
Peperment Patty
223
300
221
Pig Pen
234
123
212
Red Headed Girl
123
222
111
Marcey
1
2
3
Keith hallmark
300
300
250
Anna hallmark
222
111
211
Roxie hallmark
100
100
2
this is the output I get with my code:
Linus too good
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
The output is followed by multiple blank lines if I comment out the printing of the scores array. I manipulated the parameters of the for loops and nothing seems to work right. Could someone point me in the right direction?
while(!infile)toif (!infile). The loop only performs one iteration because of thereturnstatement.<iostream>,<string>,<fstream>and<iomanip>.