Input from txt file Problem

Im not allowed to change main and i feel like ive gone wrong with drawing the inputs from the text file, and used a variable i havent set properly hence the insane numbers.
The text file is setup as:
lastName firstName 60 70 80 90 50 60 90 90 100 90
I have to output this info as:

Enter the name of the file that contains the records:
Price Betty 40 50 60 70 60 50 40 30 60 90 55.00

where '55.00' is the average score.
Thanks


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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
  int main(){
ifstream in;
char fileName[30];
cout << "Enter the name of the file that contains the records: " << endl;
open_file(in, fileName);
calcScore(in);
in.close();
}
void open_file(ifstream& in, char fileName[]){
 
    cin>>fileName;
    in.open(fileName);

    if (in.is_open())
    {
        
    }
    else
    {
        cout<<"I/O failure opening file "<<fileName<<".";
        exit(1);
    }
}


void calcScore(ifstream& in){
    char c;
    int l=0;
    while(in.get(c)){
        if(c=='\n'){
            ++l;
        }}
    in.clear();
    
    
    int ROWS=l+1;
    
    char name1[10],name2[10];
    int s[ROWS][10];
    double avg[ROWS];
    int i=0;
    int count=0;
    


    while(!in.eof()){
        in>>name1[i]>>name2[i];
        int sum=0;
        for(int j=0;j<10;j++){
            in>>s[i][j];
            sum+=s[i][j];
        }
        avg[i]=sum/10;
        
        i++;
        count++;
    }
    
    //PRINT
    for(int i=0;i<count;i++){
        cout<<name1[i]<<setw(10)<<name2[i]<<setw(20);
        
        for(int j=0;j<10;j++){
            
            cout<<s[i][j]<<setw(2);
            
        }
        cout<<avg[i]<<endl;
    }
}


The output that was given was:
1
2
Enter the name of the file that contains the records:
\xe5         X                   0 0-495288624327644198352 0-39523881632615-49528891332764-1.38152e+08


instead of the desired:
1
2
3
4
5
Enter the name of the file that contains the records:
Price     Betty      40  50  60  70  60  50  40  30  60  90 55.00
Good      John       60  70  80  90  50  60  90  90 100  90 78.00
Smith     Charles    70  80  90  60  70  60  80  90  90  90 78.00
Georgina  Ward       70  70  80  90  70  80  90  80  70  60 76.00
Last edited on
That's not a complete program. I can't tell if you have function prototypes or what header files you have included.

Lines 27-33: Why are you counting lines character by character?

Line 39-40: That's not valid C++. In standards compliant C++, an array dimension must be a compile time constant. Some compilers do allow this as a non-standard extension.

Line 45: You never reset the stream to the beginning. You're going to be at eof. clear resets the eof bit, but does not reset the position to the beginning.

Line 46: Do not loop on !eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (cin >> var) 
  {  //  Good cin or istream operation
  }


Line 47: This reads only a single character into each name (position varies).

Line 61: This prints only a single character (position varies) from each name.

Last edited on
okay thanks alot for the feedback ill try fix it up :)
Here is an example how to read the file.
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>
#include <string>

int main()
{
  const char *filename = "students.txt";

  std::ifstream src(filename);
  if (!src)
  {
    perror("File error: ");
    return -1;
  }
  std::string firstname, lastname;
  int score[10] = {0};
  while(src >> lastname >> firstname)
  {
    for (int i =0; i < 10; i++) /*Since this is an exercise we can assume the are always 10 scores */
      src >> score[i];

    // display the data we just read to be sure we read correctly
    std::cout << lastname << '\t' << firstname << '\t';
    for (int i =0; i < 10; i++)
    {
      std::cout << score[i] << ' ';
    }
    std::cout << '\n';
  }
  return 0;
}
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

void open_file(ifstream& in, char fileName[]);
void calcScore(ifstream& in);

int main()
{   ifstream in;
    char fileName[30];
    cout << "Enter the name of the file that contains the records: " << endl;
    open_file(in, fileName);
    calcScore(in);
    in.close();
}
void open_file(ifstream& in, char fileName[])
{   cin>>fileName;
    in.open(fileName);
    if (! in.is_open())
    {   cout<<"I/O failure opening file "<<fileName<<".";
        system ("pause");
        exit(1);
    }
}

void calcScore(ifstream& in)
{   const int ROWS=10;    
    char name1[ROWS][10];
    char name2[ROWS][10];
    int s[ROWS][10];
    double avg[ROWS];
    int i=0;
    int count=0;

    while(in >> name1[i] >> name2[i]) 
    {   int sum=0;
        for(int j=0;j<10;j++)
        {   in >> s[i][j];
            sum += s[i][j];
        }
        avg[i]=sum/10.0;       
        i++;
        count++;
    }
    
    //PRINT
    for(int i=0; i<count; i++)
    {   cout << setw(10) << left << name1[i] << " " << setw(10) << name2[i] << " ";
        for(int j=0;j<10;j++)
        {   cout << setw(3) << s[i][j] << " ";
        }
        cout << fixed << setprecision(2) << avg[i] << endl;
    }
    system ("pause");
}
Enter the name of the file that contains the records:
data.txt
Price      Betty      40  50  60  70  60  50  40  30  60  90  55.00
Good       John       60  70  80  90  50  60  90  90  100 90  78.00
Smith      Charles    70  80  90  60  70  60  80  90  90  90  78.00
Georgina   Ward       70  70  80  90  70  80  90  80  70  60  76.00
Press any key to continue . . .
Thanks youre my saviors.
all i have to do now is tinker around with setw
tyvm
Topic archived. No new replies allowed.