-2

edit here's what's on the file

76 89 150 135 200 76 12 100 150 28 178 189 167 200 175 150 87 99 129 149 176 200 87 35 157 189

I want help with something pretty simple. Below is a program that reads a text file with a number of grades on it. The program takes those numbers, stores them in an array, then determines what category they belong in, hence the setCategories function I've written. Once it sets the categories the number of each number in a category is stored in an array and that array is printed to another text file. Or at least it's supposed to - as of right now I don't have the getData and printData functions defined, which are the two I have positioned to do all the leg work/ I need some help writing these functions, for some reason I can't seem to wrap my head around them.

 #include <iostream>
#include <fstream>

using namespace std;



void getData(istream & in, int A[], int & count)
{
    while(!in.eof())
    {
        in >> A[count++];
    }
}

void printData(ostream & out, int ctd[], int count)
{
    for (int i=0; i < count; i++)
    {
        out << ctd[i] << endl;
    }
}

void setCategories(int c[], int g[], int size) {
    int i;
    for (i = 0; i < size; i++) {
        if (g[i] > 174)
            c[7]++;
        else if (g[i] > 149)
            c[6]++;
        else if (g[i] > 124)
            c[5]++;
        else if (g[i] > 99)
            c[4]++;
        else if (g[i] > 74)
            c[3]++;
        else if (g[i] > 49)
            c[2]++;
        else if (g[i] > 24)
            c[1]++;
        else c[0]++;
    }
}

int main() {

    ofstream outFile;
    ifstream inFile;

    int categories[8] = {0, 0, 0, 0, 0, 0, 0, 0};
    int grades[40];
    int count = 0;

    inFile.open("/Users/holdentatlow/Desktop/computer_science_projects/test_scores.txt");
    outFile.open("/Users/holdentatlow/Desktop/computer_science_projects/results.txt");

    outFile << "Matthew Holden Tatlow" << endl;

    getData(inFile, grades, count);
    setCategories(categories, grades, count);
    printData(outFile, categories, count);


    return 0;

}

Right now, my output file is pure nonsense:

  Matthew Holden Tatlow
1
2
0
6
1
3
5
8
262169208
1
0
0
262169416
1
-1196213792
32767
-520085504
32712
-520085410
32712
-520085410
32712
0
0
0
0
  • 4
    [Each `c[i] = c[i]++;` has undefined behavior](https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points). Fix that to `c[i]++` first. – StoryTeller - Unslander Monica Nov 16 '17 at 06:28
  • Can't do much for you without knowing what the input file looks like. Recommend adding a sample of it to your question. Typical solutions are based around [Read file line by line](https://stackoverflow.com/questions/7868936/read-file-line-by-line). – user4581301 Nov 16 '17 at 06:45
  • I made the changes you guys asked for. As for the two functions, I'm not 100% sure what to do. I've read numbers from a file before but not from a function outside of main. Same deal with printing the array. – Holden Tatlow Nov 16 '17 at 13:17
  • `in >> A[40]` reads the first number and tries to place it out of bounds of the array. – omerfarukdogan Nov 16 '17 at 13:30
  • You are passing the wrong number to `printData`. It should be 7 (as there are only 7 categories). – negacao Nov 16 '17 at 13:38

2 Answers2

0

Here's a quick and dirty shot at the two functions:

void getData(istream & in, int A[], int & count)
{
    while(!in.eof())
    {
        in >> A[count++];
    }
}

void printData(ostream & out, int ctd[], int count)
{
    for (int i=0; i < count; i++)
    {
        out << ctd[i] << endl;
    }
}

Note that I've changed the signature of printData to include a count. I am also just guessing at what you want here, so.. ymmv.

negacao
  • 1,244
  • 8
  • 17
  • This seems to work a little better but I still have some nonsense of a file when it finishes. I wish there was an easy way to show it here but basically I get 26 lines of zeroes (I changed the array size to 26 because there's only 26 grades) and the final line is filled with random stuff from memory 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0x7ffeebbfe670 – Holden Tatlow Nov 16 '17 at 13:27
  • `0x7ffeebbfe670 is being caused by `outFile << categories;` in your main. Remove that. :) – negacao Nov 16 '17 at 13:29
  • Here is the output I get `Matthew Holden Tatlow 1 2 0 6 1 3 5 ` though the values are on separate lines. – negacao Nov 16 '17 at 13:30
  • Oh I forgot that, removed it. Now I just have the 0's. 26 of them. – Holden Tatlow Nov 16 '17 at 13:31
  • I had a typo in `printData` - it should be `out << ctd[i] << endl;` – negacao Nov 16 '17 at 13:32
  • How do you have 26 zeros? You are only passing `categories[8]` to `printData`, right? – negacao Nov 16 '17 at 13:33
-1

Here's what worked for me:

#include <iostream>
#include <fstream>

using namespace std;



void getData(istream & in, int A[], int & count)
{
    while(!in.eof())
    {
        in >> A[count++];
    }
}

void printData(ostream & out, int ctd[], int count)
{
    for (int i=0; i < 8; i++)
    {
        out << ctd[i] << endl;
    }
}

void setCategories(int c[], int g[], int size) {
    int i;
    for (i = 0; i < size; i++) {
        if (g[i] > 174)
            c[7]++;
        else if (g[i] > 149)
            c[6]++;
        else if (g[i] > 124)
            c[5]++;
        else if (g[i] > 99)
            c[4]++;
        else if (g[i] > 74)
            c[3]++;
        else if (g[i] > 49)
            c[2]++;
        else if (g[i] > 24)
            c[1]++;
        else c[0]++;
    }
}

int main() {

    ofstream outFile;
    ifstream inFile;

    int categories[8] = {0, 0, 0, 0, 0, 0, 0, 0};
    int grades[26];
    int count = 0;

    inFile.open("/Users/holdentatlow/Desktop/computer_science_projects/test_scores.txt");
    outFile.open("/Users/holdentatlow/Desktop/computer_science_projects/results.txt");

    outFile << "Matthew Holden Tatlow" << endl;

    getData(inFile, grades, count);
    setCategories(categories, grades, count);
    printData(outFile, categories, count);


    return 0;

}