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




const int length = 25;

void insertionSort(int numb, char* anArray[])
{
	char temp[length];
	int pos;

	for(int i = 1; i < numb; i++)
    {
		strncpy(temp, anArray[i], length);
		pos = i;

		//moves "higher" letters to the right
		while(pos > 0 && strnicmp(anArray[pos-1],temp,length) > 0)
        {
			strncpy(anArray[pos],anArray[pos - 1],length);
			pos --;
		}
		strncpy(anArray[pos],temp,length);
	}
}


int main()
{
    ofstream file;
    file.open("Alphabetize.doc");

    

	int num;
	
	cout << "How many names do you want to enter? ";
	cin >> num;
	char *sortArray= new char [num];
    
    file << "Original Word List: " << endl;
	
	for (int i=0; i<num; i++)
	{
        cout << "\tInput word: ";
        cin >> sortArray[i];        
        file << sortArray[i] << endl ;
    insertionSort(num,sortArray[i]);
    }


    file << "\n\nSorted List:\n";
    
	for(int i = 0; i < num; i++)
		file << sortArray[i] << endl;

    return 0;
    
}

ERROR: invalid conversion from `char' to `char**' initializing argument 2 of `void insertionSort(int, char**)'

I have an idea of what the error is, in that when I am calling the function, I am passing the wrong arguments. (I can read). I have tried googling for help (to understand it so I can 1, fix it and 2, not make it again), I have read my c++ book I feel like 1,000 times. I can't seem to figure out how to fix it. :$
I have tried different combinations of * and & to reference and dereference the char, since I couldn't understand what the book was telling me about it. So far, nothing is working! I'm getting VERY frustrated.
Obviously, the goal of the program (and yes, it is a school project) is to alphabetize an input list of names, the length of the list to be determined by the user. I thought string would work better than char, but the assignment says to use "char *names[]", so I'm using that (though with a different name).

I'd appreciate any help in understanding wtf I need to do. I'm tired of this error!
Thanks!!!!

>>char* anArray[]

That parameter is a 2 dimensional array, what you are trying to pass is a single character.

Now, down in main(), you declared sortArray as a single-dimensional array. That means you can not put more than one string into it. Line 50 will only let you enter a single character, not a whole string.


You need to declare sortArray with two stars, not one, in order to make it a 2 dimensional array of strings char **sortArray= new char* [num]; . Next, inside the loop you have to allocate more memory for the string itself. And the call to sort() must be after the end of the loop, not inside it.

for (int i=0; i<num; i++)
	{
        cout << "\tInput word: ";
        string word;
        cin >> word;
        sortArray[i] = strdup(word.c_str());        
        file << sortArray[i] << endl ;
    }
    insertionSort(num,sortArray);

I thought "insertionSort(num,sortArray)" would pass the whole array, i.e. all of the input words? So how is it only sending a single character? Or am I misunderstanding what you've written?

sortArray is NOT an array of strings -- see my previous post. Your program passed sortArray[i] which is just one character. char * sortArray = new char[255]; is very similar to char sortArray[255]; in that both are single-dimentional arrays that can only hold a single string of up to 255 characters. What you want is something like char sortArray[255][20]; which holds up to 255 strings of 20 characters each. When you know neither of those dimentions at compile time then you need to declare it with a double star char **sortArray and allocate each dimension as I previously posted.

I think I understand now. :)
For some reason it keeps crashing, but now that I understand how to code it better I will get it to work.
Thanks so much!!!

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.