I cant figure out how to fix the errors in this prooram. Any help would be great.

// date: April 25, 2008
// username: dpfaff



#include <cstring>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <iostream>
using namespace std;

const int MAXCHARS = 25;
const int MAXSIZE = 6;
typedef char String [MAXCHARS];
string array[MAXSIZE];

// opens an input file called in.dat
// outputs: if no failure, infile is open
// else prints message and program stops
// usage: openInputFile (infile);
void openInputFile (ifstream& infile);

// sorts a list of words
// inputs: array[0..size-1]
// outputs: array
// usage: selectionSort (nums, MAXSIZE);
void selectionSort (string array[], int size);

// enters a list of words
// inputs: a list of words
// outputs: a list of words in array
// usage:
void enter (ifstream& infile, string array[], int size);

// prints a list of words
// inputs: a list of words in array
// outputs: a list of words separated by commas
// usage:
void print (string array[], int size);

// prints a number of new lines
// inputs: num, the number of new lines
// outputs: num new lines
// usage: printNewLines(3);
void printNewLines (int num);



int main()
{
ifstream infile;
openInputFile (infile);
openInputFile(inlab.dat);
string array [MAXSIZE];
enter (inlab.dat, array, MAXSIZE);
print (array, MAXSIZE);
printNewLines(3);
selectionSort (array, MAXSIZE);
print (array, MAXSIZE);
inlab.dat.close();

return 0;
}

void selectionSort (string array[], int size)
{
int start, minIndex, minValue;

for (start = 0; start < size - 1; start++)
{
minIndex = start;
strcpy(minValue, array[start]);
for (int index = start + 1; index < size; index++)
{
if (strcmp(array[index], minValue) < 0))
{
strcpy(minValue, array[index]);
minIndex = index;
}
}
strcpy(array[minIndex], array[start]);
strcpy(array[start], minValue);
}
}
// opens an input file called in.dat
// outputs: if no failure, infile is open
// else prints message and program stops
// usage: openInputFile (infile);
void openInputFile (ifstream& infile)
{
infile.open("inlab.dat");
if (infile.fail())
{
cout << "Error opening input file" << endl;
exit(1);
}
}



// enters a list of words
// inputs: a list of words
// outputs: a list of words in array
// usage:
void enter (ifstream& infile, string array[], int size)
{
for (int i = 0; i < size; i++)
{
infile >> array[i];
}
}

void print (ifstream& infile, String array[], int size)
{
printNewLines(2);
for (int i = 0; i < size; i++)
cout << array[i] << ", ";
printNewLines(2);
}

// prints a number of new lines
// inputs: num, the number of new lines
// outputs: num new lines
// usage: printNewLines(3);

void printNewLines (int num)
{
for (int k = 0; k < num; k++)
cout << endl;
}

Recommended Answers

All 8 Replies

post some of the errors.

On lines 55 and 57 you are trying to use an unidentified object inlab. Have no clue what that is supposed to be.

inlab is the name of the input file that containes a list of names. here are some of the errors I am getting.

55: inlab was not declared in this scope
In funtion void selectionSort(char (*)[25], int)
74:error: Stropy was not declared in the scope
77:error: Stromp was not declared in the scope
77: expected ';' before ) token

55: should be openInputFile("infile.dat"); (even then it won't work, infile.dat is a string not an ifstream reference) just take this line out and it should be fine.
77: if (strcmp(array[index], minValue) < 0)) you have an extra )

74:error: Stropy was not declared in the scope
77:error: Stromp was not declared in the scope

What? In your code it says strcpy and strcmp , if not change it.

I have tried that but it did not fix the errors on those lines
I also have errors on lines 60 and 61:

cannot convert std string to to char for argument 1 selection sort

line 55: I see now -- just delete that line because line 54 does what you want it to do.

cannot convert std string to to char for argument 1 selection sort

you can not use strcpy() with std::string objects. Just use equality. Since array is an array of std::string's minValue must also be a std::string.

string minValue = array[0];
for (int index = start + 1; index < size; index++)
{
    if (array[index] < minValue) )
    {
        minValue = array[index];
        minIndex = index;
    }
}

I think I understand but I still get errors when I do it that way

1) add include <string> near the top of the program

2)

void selectionSort (string array[], int size)
{
int start = 0, minIndex = 0;
string minValue;
for (start = 0; start < size - 1; start++)
{
minIndex = start;
minValue = array[start];
for (int index = start + 1; index < size; index++)
{
    if (array[index] < minValue)
    {
        minValue = array[index];
        minIndex = index;
    }
}
array[minIndex] = array[start];
array[start] = minValue;
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.