-1

I know that if I don't use up all the space in an array that the computer will just fill it with garbage values, but how do I stop them from showing up in the output? The user cannot specify the size of the array either (professor made it that way for this lab). I am reading from a text file if that helps.

const int STUDENTS = 50; //size of array

struct StudentInfo 
{
    string name;
    char char_name[SIZE];
    int idnumber;
    int num1;
    int num2;
    int num3;
    int num4;
    int num5;
    int num6;
    int num7;
};

int main()
{
 StudentInfo student[STUDENTS];
 ifstream inFile;         

int i = 0; 
inFile.open("guesses.txt"); 

if (!inFile)
    cout << "\n\n**** ERROR OPENING FILE. ******\n" << endl;
else
{
    while (!inFile.eof())
    {
        inFile >> student[i].idnumber; 
        inFile.getline(student[i].char_name, SIZE, '\n');
        student[i].name = student[i].char_name;
        inFile >> student[i].num1;
        inFile >> student[i].num2;
        inFile >> student[i].num3;
        inFile >> student[i].num4;
        inFile >> student[i].num5;
        inFile >> student[i].num6;
        inFile >> student[i].num7;
        i++;
        if (inFile.eof()) 
            break;
    }

 for (int i = 0; i < STUDENTS; i++) //loop i use for output
    {
        cout << left;
        cout << setw(5) << student[i].idnumber;
        cout << setw(15) << student[i].name;
        cout << right;
        cout << setw(6) << student[i].num1;
        cout << setw(6) << student[i].num2;
        cout << setw(6) << student[i].num3;
        cout << setw(6) << student[i].num4;
        cout << setw(6) << student[i].num5;
        cout << setw(6) << student[i].num6;
        cout << setw(6) << student[i].num7;
        cout << endl;
    }
    cout << endl << endl << endl;

 inFile.close();

 system("pause");
}
Billy
  • 3
  • 2
  • Could you add more context, please? What is `student`? – PaSTE Dec 05 '18 at 00:47
  • 2
    A [mcve] please. – PaulMcKenzie Dec 05 '18 at 00:47
  • 2
    Best guess based on the code given would be to not print out more students than were recorded. If you read in five students, there's not too much point to printing 50. Add an extra variable to track how many students are stored and use that as the upper bound for the `for` loop. – user4581301 Dec 05 '18 at 00:49
  • 2
    You have to keep count of the number of valid entries somewhere that would control the loop. Otherwise the code you posted is working exactly as designed. – PaulMcKenzie Dec 05 '18 at 00:50
  • added more code for it to make more sense – Billy Dec 05 '18 at 00:51
  • Still doesn't make much sense I'm afraid. Where do you read in students? Are you storing them correctly? – user4581301 Dec 05 '18 at 00:52
  • @Billy No it still is not a [mcve]. You're showing exactly what is to be expected from your description. Where is your variable that keeps track of the number of valid students? – PaulMcKenzie Dec 05 '18 at 00:52
  • @Billy -- Start naming your variables with descriptive names, not `i`. That right there would have corrected the issue. Name the variable `student_count` or something like that, and then use that as the looping constraint in the loop you're using to print the values. – PaulMcKenzie Dec 05 '18 at 00:55
  • full code is now there, and for the record I know the code is working, I just need to know if there is a way to get the garbage values the computer puts in the array out of my output. – Billy Dec 05 '18 at 00:56
  • @Billy -- So you are keeping count, but you didn't realize it because you named the variable `i`. This is a perfect example of why you should choose variable names that make sense and are descriptive. – PaulMcKenzie Dec 05 '18 at 00:57
  • Code only sort of works. `inFile.eof()` only checks for end of file. If anything else goes wrong you get an infinite loop because the program will never get to the end of the file. Plus even if you hit the end of the file while reading in the input, the code still increments `i`, giving one garbage output at the end. Related problem here: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Dec 05 '18 at 00:58

1 Answers1

1

This sounds like homework, and you don’t give enough information to answer the question. However, it sounds as if you’re reading data on a certain number of students.

If you know how many students there are at the beginning of the loop, let’s say because the input file contains the number before the list, you can replace the constant STUDENTS with that variable in the loop condition.

If you need to read in an unknown number of students, you would make the loop terminate after you’ve read in the last one. Either you output each student’s data within the body of the loop, or you initialize the loop counter to 0 before the loop, declaring it outside the loop so it will remain in scope, increment it on each iteration, and then when the loop terminates, the counter will have been set to the number of elements read in.

In your MCVE, you already declare int i = 0; before the loop, so you’d have to make only minimal changes to implement the second approach. Some commenters are suggesting that, instead of a short variable name like i, you might make a more descriptive one, such as, perhaps, students_in_array.

Davislor
  • 14,674
  • 2
  • 34
  • 49
  • 1
    @user4581301 What would you suggest? I’d honestly use `n` and `i` in a simple function with one loop; they’re standard names that will be readily understood. – Davislor Dec 05 '18 at 00:58
  • 1
    @Davislor -- Maybe the `i` name is what got the OP in trouble in the first place? If they came up with a descriptive name, maybe it would automatically click on the light bulb in the head as to what to use? – PaulMcKenzie Dec 05 '18 at 01:00
  • @user4581301 Thanks for the great suggestions! – Davislor Dec 05 '18 at 01:27