Array, DO WHILE INPUT FILE

Nov 2, 2015 at 12:01am
Hello, I am trying to read the following from an input file into the console using a do while loop. The first column is the quiz number, second is earned points and the third is the max points earned. The 1000 is a Sentinel value.

input file
0 14 27
1 11 14
2 24.5 30
3 31 34
4 7.5 9
5 10 14
6 18 20
7 15 16
1000

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

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    const int MAX = 20;

    const double SENTINEL = 1000;

    int quiz_no[MAX + 1];
    int quiz,
        earned,
        max_earned;

    int i,
        

    float pts_earned[MAX],
          pts_max[MAX];

    ifstream fin;

    fin.open("quizinfo.txt");

    if (!fin)
    {
        cout << "Input file open failure\n";
        return 1;
    }

    do
    {
        cout << "NO.\tEarned\tMax"
             << setprecision(2) << fixed;

        fin >> quiz >> earned >> max_earned;
        quiz_no[i] = quiz;
        cout << endl
             << quiz << "\t" << earned << "\t" << max_earned;
    }while (quiz != SENTINEL);

Last edited on Nov 2, 2015 at 12:05am
Nov 2, 2015 at 1:35am
missing:
#include <fstream>

1
2
3
    int quiz,
        earned,
        max_earned

This will cause an infinite loop of reading the data because int does not support decimal numbers. Change earned and max_earned to type double.

int i,
This is missing a semi-colon to end the line. i is also never defined or incremented making it invalid.

Apart from that the code will actually work. I corrected those mistakes and it works as expected.
Last edited on Nov 2, 2015 at 1:35am
Nov 2, 2015 at 1:45am
Do I increment the code like this?

do
{
cout << "NO.\tEarned\tMax"
<< setprecision(2) << fixed;

fin >> quiz[i] >> earned[i] >> max_earned[i];

cout << endl
<< quiz[i ]<< "\t" << earned[i] << "\t" << max_earned[i];

}while (quiz != SENTINEL);
Last edited on Nov 2, 2015 at 1:47am
Nov 2, 2015 at 2:26am
you're using the value i but never setting a value to it or incrementing it.

e.g
1
2
3
4
5
int i; // i has no value

int my_ints[10];

my_ints[i] = 0; // what is i? it's invalid 
Nov 2, 2015 at 2:48am
So, I set i equal to zero and ran the following. I got this in the build message part
error: invalid types 'int[int] ' for array subscript
error: invalid types 'double[int] ' for array subscript



#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    const int MAX = 20;

    const double SENTINEL = 1000;

    int quiz_no[MAX + 1];
    int quiz;

    double earned,
           max_earned;

    int i = 0;


    double pts_earned[MAX],
          pts_max[MAX];

    ifstream fin;

    fin.open("quizinfo.txt");

    if (!fin)
    {
        cout << "Input file open failure\n";
        return 1;
    }

    do
    {
        cout << "NO.\tEarned\tMax"
             << setprecision(2) << fixed;

        fin >> quiz[i] >> earned[i] >> max_earned[i];
        quiz_no[i]= quiz;
        pts_earned[i] = earned;
        pts_max[i] = max_earned;
        cout << endl
             << quiz << "\t" << earned << "\t" << max_earned;
    }while (quiz != SENTINEL);
Nov 2, 2015 at 2:54am
fin >> quiz[i] >> earned[i] >> max_earned[i];
quiz, earned and max_earned are not part of an array. So you cannot access them like an array.

You're code is over-complicated imo.
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace std;

struct Element {
  int index = 0;
  double earned = 0;
  double max_earned = 0;
};

int main() {
  const double SENTINEL = 1000;

  ifstream fin;
  fin.open("quizinfo.txt");

  if (!fin) {
    cout << "Input file open failure\n";
    return 1;
  }

  vector<Element> elements;
  int quiz_id;

  cout << "NO.\tEarned\tMax" << setprecision(2) << fixed << endl;
  do {
    Element new_entry;
    fin >> quiz_id >> new_entry.earned >> new_entry.max_earned;
    new_entry.index = quiz_id; // kinda useless if data is ordered
    if (quiz_id != SENTINEL)
      elements.push_back(new_entry);

  } while (quiz_id != SENTINEL);

  for (Element element : elements)
    cout << element.index << "\t" << element.earned
      << "\t" << element.max_earned << endl;

  return 0;
}
Nov 2, 2015 at 3:02am
I cant have anything global or use the vector library.
Nov 2, 2015 at 3:06am
I cant have anything global or use the vector library

^^ This is fucking stupid.

As you're brief is to only read from a file and print to console I've dumped all of your array crap.
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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main() {
  const double SENTINEL = 1000;
  int quiz;
  double earned, max_earned;

  ifstream fin;
  fin.open("quizinfo.txt");
  if (!fin) {
    cout << "Input file open failure\n";
    return 1;
  }

  cout << "NO.\tEarned\tMax" << setprecision(2) << fixed << endl;
  do {
    fin >> quiz >> earned >> max_earned;
    if (quiz != SENTINEL)
      cout << quiz << "\t" << earned << "\t" << max_earned << endl;
  } while (quiz != SENTINEL);

  return 0;
}
Nov 2, 2015 at 3:28am
Thank you! I really appreciate it. Can I ask you one more question?
Nov 2, 2015 at 3:29am
Quickly sure
Nov 2, 2015 at 3:34am
I want to drop a certain quiz grade but the average isn't working out. Also, I want the stuff in the input file to keep repeating every time I choose to drop a different quiz grade. Do I include the following in the do while loop as well?



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


    while (count <= MAX && pts_earned >= 0)
    {

        total_earned += pts_earned[count];
		count = count + 1;
		fin >> pts_earned[count];		//Get the next grade
    }

    while (count < MAX && pts_max >= 0)
    {

        total_earned += pts_max[count];
		count = count + 1;
		fin >> pts_max[count];		//Get the next grade
    }

    average = (total_earned-pts_earned[count])/(total_max-pts_max[count])*100;

    if (drop > 0)
    {
            cout << endl << endl;
            cout << "Please select the quiz number you want to drop or any negative"
                 << " number to exit the program: ";
            cin >> drop;
            cout << setprecision(2) << fixed
                 << "If you drop quiz #" << drop << " your average will be " << average;
    }
    else
    {
            cout << "You have chose to exit the program.";
    }

Nov 2, 2015 at 3:46am
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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main() {
  const double SENTINEL = 1000;
  int quiz;
  double earned, max_earned;

  ifstream fin;
  fin.open("quizinfo.txt");
  if (!fin) {
    cout << "Input file open failure\n";
    return 1;
  }

  double grades[20] = {0};
  unsigned loaded_grades = 0;

  cout << "NO.\tEarned\tMax" << setprecision(2) << fixed << endl;
  do {
    fin >> quiz >> earned >> max_earned;
    if (quiz != SENTINEL) {
      cout << quiz << "\t" << earned << "\t" << max_earned << endl;
      grades[loaded_grades] = earned;
      ++loaded_grades;
    }
  } while (quiz != SENTINEL);

  cout << "There were " << loaded_grades << "loaded" << endl;
  cout << "Grades:";
  for (unsigned i = 0; i < loaded_grades; ++i)
    cout << grades[i] << " ";
  cout << endl;

  return 0;
}


This code shows you how to declare, populate and read from an array. It's the basic skillset you need for what you're after.
Nov 2, 2015 at 4:34am
Thank you!
Topic archived. No new replies allowed.