0

My program can't read all of the data from my MarvelIn.txt file. It reads about 29 spaces in, and MarvelIn.txt contains only 9 entries, then I get a runtime error.

I think I have all the syntax right, however this is the only error I have. It will not output to the output file "MarvelOut.txt".

Here is the code:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
using namespace std;

struct sstruct
{
    string first, last;
    string department;
    int salary;
};

void init2(sstruct s[50])
{
    int maxarray = 50;
    sstruct init = { "Darth", "Vader", "None", 0 };

    for (int i = 0; i < maxarray; i++) {
        s[i] = init;
        cout << "init: " <<s[i].first<<endl;
    }
}

void read(sstruct s[50], int &nums)
{
    int maxarray = 50;
    ifstream inf("MarvelIn.txt");
    int i = 0;
    while (!inf.eof())
    {
        inf >> s[i].first >> s[i].last >> s[i].department >> s[i].salary;
        cout << "read: "<<s[i].first<<s[i].last << s[i].department << 
        s[i].salary << endl;
        i++;
    }
    nums = i;
}

void avg(sstruct s[50], int &nums, double &average2)
{
   int maxarray = 50;
   int i;
   for (i = 0; i < nums; i++)
       average2 += s[i].salary;
   average2 /= nums;
}

void print(sstruct s[50], int nums, double &average2)
{
    int maxarray = 50;
    ofstream outf("MarvelOut.txt");
    int i = 0;
    string temp;
    outf << "the number of professors is: " << nums << endl;
    cout << "the number of professors is: " << nums << endl;
    outf << endl << "The average salary of the professors is: " << average2 << endl;
    outf << "Advisor                    " << "Major     " << " Department     " << "Salary     " << endl;
    for (i = 0; i < nums; i++)
    {
        temp= s[i].last + "," + s[i].first;
        cout << "last, first " << temp << endl;
        outf << left << setw(20) << temp << right << setw(5)<< s[i].department << setw(5) << s[i].salary << setw(8) << endl;
    }
    outf << endl << endl;
}

void swap(sstruct &a, sstruct &b)
{
   sstruct temp;
   temp=a;
   a=b;
   b=temp;
}

void bubbleSort(sstruct s[50], int &nums)
{
    int maxarray = 50;
    int i, j;
    bool swapped;
    for (i = 0; i < nums - 1; i++)
    {
        swapped = false;
        for (j = 0; j < nums - i - 1; j++)
        {
            if (s[j].department > s[j + 1].department)
            {
                swap(s[j], s[j+1]);
                swapped = true;
            }
        }

        // IF no two elements were swapped by inner loop, then break
        if (swapped == false)
            break;
    }
}

int main() {
    int nums=0;
    double average3=0.0;
    const int maxarray = 50;
    sstruct s[maxarray];
    init2(s);
    print(s, nums, average3);
    read(s, nums);
    cout << "numsfirst: " << nums << endl;
    avg(s, nums, average3);
    cout << "nums" << nums << endl;
    bubbleSort(s,nums);
    print(s, nums, average3);
    system("pause");
    return 0;
}
Robert
  • 4,805
  • 16
  • 34
  • 46
Dev178
  • 39
  • 1
  • 1
  • 2
  • 1
    Can you share the runtime error you are encountering? – tshimkus Feb 05 '19 at 01:23
  • You may want to read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – n. 'pronouns' m. Feb 05 '19 at 14:35

0 Answers0