I've declared a function sort
that should sort my arrays of strings and numbers, but when I call it, I get an invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string*'.
I also need to get it to keep the array numoftoys in sync with this one as it sorts.
#include <iostream> //Basic input/output
#include <iomanip> //Manipulators
#include <string> //String stuff
#include <fstream> //File input/output
using namespace std;
void instruct (); //Function Declaration for printing instructions
void input (ifstream &infile, string &names, int &numoftoys); //Function declaration for getting data from file
void sort (string &names, int &numoftoys, int i);
void rating (string names, int numoftoys, string &raiting);
void headers (); //Prints headers
int main()
{
string names [50]; //Array for storing names
int numoftoys [50]; //Array for storing the number of toys made
string raiting [50]; //Array for raiting
int i = 0;
int p = 0;
ifstream infile("elves.dat"); //Opens input file "elves.dat"
instruct(); //Function call to print instructions
while ((infile >> names[i]) && (infile >> numoftoys[i]))
{
++i;
}
sort (names, numoftoys, i);
for (int p = 0; p<i; p++)
{
cout << names[p] << " " << numoftoys[p] << " " << raiting[p] << "\n";
}
return 0;
}
/***************************************************/
/* Name: instruct */
/* Description: Prints instructions to user */
/* Parameters: N/A */
/* Return Value: N/A */
/***************************************************/
void instruct ()
{
cout << "\n" << "This program will calculate the toys made by santas elfs and assign" << "\n";
cout << "a rating to each elf. It will also sort them and print average, min and max." << "\n";
cout << "\n" << "Make sure you have a file named elves.dat in the same directory as";
cout << " this porgram or you will recieve errors.";
cout << "\n" << "\n";
return;
}
/***************************************************/
/* Name: input */
/* Description: Reads from file */
/* Parameters: N/A */
/* Return Value: N/A */
/***************************************************/
void input (ifstream &infile, string &names, int &numoftoys)
{
infile >> names;
infile >> numoftoys;
return;
}
/***************************************************/
/* Name: sort */
/* Description: Sorts Data */
/* Parameters: N/A */
/* Return Value: N/A */
/***************************************************/
void sort (string &names, int &numoftoys, int i)
{
bool swapped = true;
int j = 0;
int tmp;
int t = 0;
while (swapped) {
swapped = false;
j++;
for (int t = 0; t<i-j; t++);{
if (names[t] > names[t + 1]){
tmp = names[t];
names[t] = names[t + 1];
names[t+1] = tmp;
swapped = true;
}
}
}
}