What's Wrong With my code? ARRAYS

**Every time i run the program nothing happens.** " You will be using a data file called elves.dat. There will be one line for each elf. That line will contain the name of the elf and the number of toys the elf made. You are to read from the file and place the values in parallel arrays. You do not know how many elves there are so you must read until the end of file and count. You will need a third parallel array of strings to record a rating for each elf. You should declare arrays with the capacity for 50 components."

I compile the program with the elves.dat file but it doesn't run the program. Am i doing something wrong? Is there something wrong with the code. Should I change the .dat file to a .txt file? Do I have to have an input and output 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  #include <iostream>
#include <fstream>
#include <string.h>
#include <climits>

using namespace std;


int fTotalToys(int toys[50], int counter)
{
    int result=0;
    for (int i=0; i<counter-1; i++)
    {
        result+=toys[i];
    }
    
    return result;
}

int fOverFive(int toys[50], int counter)
{
    int result=0;
    for (int i=0; i<counter-1; i++)
    {
        if(toys[i]>=500)
        {
            result++;
        }
    }
    return result;
}

int fGreatElf(int toys[50], int counter)
{
    int result=0;
    int maxim=0;
    for (int i=0; i<counter-1; i++)
    {
        if(toys[i]>=maxim)
        {
            maxim = toys[i];
            result = i;
        }
    }
    
    return result;
}

int fLeastElf(int toys[50], int counter)
{
    int result=0;
    
    int minimum=INT_MAX;
    for (int i=0; i<counter-1; i++)
    {
        if(toys[i]<=minimum)
        {
            minimum = toys[i];
            result = i;
        }
    }
    
    return result;
}

void printResults(string elf[50],int toys[50], int counter)
{
    ofstream outf("elves.out");
    
    string rating;
    
    outf << "Name           Number of toys    Rating\n";
    for (int i=0; i<counter-1; i++)
    {
        outf << elf[i] << "\t" ;
        if(elf[i].length()<=7)
            outf<<"\t";
        if(elf[i].length()<12)
            outf<<"\t";
        outf<< toys[i] << "\t\t\t";
        if(toys[i]<1000)
            outf<<"\t";
        if (toys[i]>=500)
        {
            rating = "*****";
            
        }
        else if ((toys[i]>=300) && (toys[i]<=499))
        {
            rating = "***";
        }
        else if ((toys[i]>=200) && (toys[i]<=299))
        {
            rating = "*";
        }
        else if (toys[i]<200)
        {
            rating = "-";
        }
        outf << rating << "\n";
        
    }
    
    
    outf << "Total number of toys is :"<< fTotalToys(toys, counter)<<"\n";
    outf << "The number of elves who made more than 500 toys is :"<< fOverFive(toys, counter)<<"\n";
    outf << "The elf who made the most toys is :"<< elf[fGreatElf(toys, counter)] << "\n";
    outf << "The elf who made the least toys is :"<< elf[fLeastElf(toys, counter)] << "\n";
    outf.close();
}



int main()
{
    int toys[50];
    string elf[50];
    string cElf, greatElf, leastElf;
    
    ifstream inf("elves.dat");
    
    for (int i=0; i<50; i++)
    {
        toys[i]=0;
        elf[i]=" ";
    }
    
    int ceToys=0, counter=0;
    
    while (!inf.eof())
    {
        inf >> cElf;
        elf[counter]=cElf;
        inf >> ceToys;
        toys[counter]=ceToys;
        counter++;
        
    }
    inf.close();
    
    printResults(elf, toys, counter);
    
    return 0;
}
Last edited on
Given your code as it is, if you have elves.dat in the same folder and it's not empty, the program should run successfully.

Depending on the contents of elves.dat, this is a program that will probably run in the blink of an eye, without screen output, so maybe it is running and you're missing it.

You are sending your output to elves.out, so check to see if this file was created by your program and what it's contents are.

Alternatively, if you must see the output on the screen, then replace all references to outf with cout in printResults()

Also, line 130 is not a good test, consider testing the result of the input operation instead, for example
1
2
3
4
	while (inf >> elf[counter] && inf >> toys[counter])
	{
		counter++;
	}

Topic archived. No new replies allowed.