declare SIZE with macro
as in
#define SIZE 50
the way you have declared elfRating will throw error, either you declare the array as pointer or with literal value; probably c++14 has other way of declaring;
also, you can write if else cascade as follow
if (numToys [t] < 200)
{
rating = "-" ;
}
else if (numToys [t] < 300)
{
rating = "*" ;
}
else if (numToys [t] < 500)
{
rating = "***" ;
}
else
{
rating = "*****" ;
}
there is no way, you will get different value
in the first while loop of main
you have
cout << names [10] << endl ;
in the for loop of main function
you have declared
total in the for loop block. total variable does not exists outside the loop. As well as you are setting total back to zero in every loop;
in the elfRating function you have sent rating as std::string array; you are trying to set string in the array; C++ compiler must throw error.
Suggestion is:
1.
change
rating="**** string";
to
rating[t]="**** string";
2.
change elfRating return type to void, remove the return;
3.
in the second while loop, remove the function calling of elfRating
move elfRating above for loop;
you should close ins too;
And I feel like, you haven't learn c++ programming or any other programming very well, you need more study
--edit: add total error fixing and with example--
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#define SIZE 50 // new line
using namespace std ;
void elfRating (int[], string[]) ;
int main ()
{
string names [SIZE] ; int numToys [SIZE] ; string rating [SIZE] ; string line = " " ;
ifstream ins ;
ins.open ("elves.dat") ;
int i = 0 ;
istringstream iss (line) ;
while (getline(ins, line))
{
istringstream iss (line) ;
iss >> names [i] >> numToys [i] ;
i++ ;
}
int total = 0 ;
elfRating (numToys, rating) ; for (int num = 0; num < SIZE; num++)
{
total += numToys[num] ;
}
int cnt = 0 ;
int max = numToys [0] ;
while (cnt < SIZE)
{
if (numToys[cnt] > max)
{
max = numToys [cnt] ;
cnt++ ;
}
else
{
cnt++ ;
}
}
int count = 0 ;
int min = numToys [0] ;
while (count < SIZE)
{
if (numToys[count] < min)
{
min = numToys [count] ;
count++ ;
}
else
{
count++ ;
}
}
int ii = 0 ;
while (ii < 50) {
cout << setw(30) << left << "Elves name:" << names[ii]
<< setw(20) << left << "Number of Toys Produced:" << numToys[ii]
<< setw(10) << left << "Rating:" << rating[ii] ;
ii++ ;
}
cout << "Total number of toys created by all elves: " << total ;
cout << "The greatest number of toys created is: " << max ;
cout << "The lowest number of toys created is: " << min ;
cin.ignore () ;
}
void elfRating (int numToys [SIZE], string rating [SIZE])
{
int t = 0 ;
for (t=0;t<50;t++)
{
if (numToys [t] < 200)
{
rating [t] = "-" ;
}
else if (numToys [t] < 300)
{
rating [t] = "*" ;
}
else if (numToys [t] < 500)
{
rating [t] = "***" ;
}
else
{
rating [t] = "*****" ;
}
}
}