Trying to take input from user, put it in array and do the sorting

The code is:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

class Sort
{
	static int array[5];
	static int index;
	static int i;

public:


	void insert(int num)
	{
		array[index] = num;
		index++;
	}

	int sortarr(const void *x, const void *y) 
	{
		return (*(int*)x - *(int*)y);
	}

	void doSort()
	{
		qsort(array, index, sizeof(int), sortarr);
		for (i=0; i<index; i++)
		{
			printf("%d ", array[i]);
		}
	}
};

void main()
{
	
	int j;
	int number;

	Sort s;
	
	for(j = 0; j<=5; j++)
	{
		printf("Enter number = ");
		scanf(&number);
		s.insert(number);
		
	}

	s.doSort();

	getch();
}

getting following errors

Error 1 error C3867: 'Sort::sortarr': function call missing argument list; use '&Sort::sortarr' to create a pointer to member e:\personal\brain mass\391331 - adt sort c\adt_sort\adt_sort\main.cpp 28 ADT_Sort
Error 2 error C2664: 'scanf' : cannot convert parameter 1 from 'int *' to 'const char *' e:\personal\brain mass\391331 - adt sort c\adt_sort\adt_sort\main.cpp 47 ADT_Sort

Error 1 is a little subtle. qsort expects a pointer to a function, which sortarr is not. Due to how C++ handles member functions, the type of the address of Sort::sortarr is different from a typical function pointer. Note that doing what the error suggests and using a pointer to a member will not work because that's a completely different pointer type from a pointer to a function.

You can correct the error by either hoisting sortarr out of the class:

int sortarr(const void *x, const void *y) 
{
    return (*(int*)x - *(int*)y);
}

class Sort {
public:
    void doSort()
    {
        qsort(array, index, sizeof(int), sortarr);
        for (i=0; i<index; i++)
        {
            printf("%d ", array[i]);
        }
    }
};

Or by making sortarr a private static (private because users of the class probably don't need to see it):

class Sort {
public:
    void doSort()
    {
        qsort(array, index, sizeof(int), sortarr);
        for (i=0; i<index; i++)
        {
            printf("%d ", array[i]);
        }
    }
private:
    static int sortarr(const void *x, const void *y) 
    {
        return (*(int*)x - *(int*)y);
    }
};

Both of these will give sortarr pointer to function status rather than member function status.

>scanf(&number);
This is not how scanf is called. You need a format string and then a list of objects to hold data conversions as specified in the format string. For an integer, that would be:

if (scanf("%d", &number) == 1) {
    /* Success */
}

What the error is saying is that the first argument is not a string, which is the expected first argument type scanf expects. I'd highly recommend getting a library reference, since your call to scanf suggests just winging it with no real understanding.

On a side note, is there any particular reason you're using the C library in C++ when "better" equivalents exist in the C++ library?

#include <algorithm>
#include <iostream>
#include <ostream>
#include <stdexcept>

template <typename T, int N>
class Sort {
    T _base[N];
    int _filled;
public:
    enum { SIZE = N };

    Sort(): _filled(0)
    {
        std::fill_n(_base, N, T());
    }

    void insert(T value)
    {
        if (_filled == N)
            throw std::runtime_error("No more room");

        _base[_filled++] = value;
    }

    void doSort()
    {
        std::sort(_base, _base + N);
    }

    template <typename T, int N>
    friend std::ostream& operator<<(std::ostream& out, const Sort<T, N>& s)
    {
        out<<'{';

        for (int i = 0; i < s.SIZE; i++) {
            out<< s._base[i];
            out<< (i < s.SIZE - 1 ? ',' : '}');
        }

        return out;
    }
};

int main() try {
    Sort<int, 5> s;
    int x;

    for (int i = 0; i < s.SIZE; i++) {
        if (!(std::cin>> x))
            throw std::invalid_argument("Invalid input or stream error");

        s.insert(x);
    }

    std::cout<< s <<'\n';
    s.doSort();
    std::cout<< s <<'\n';
}
catch (const std::exception& ex) {
    std::cerr<< ex.what() <<'\n';
}
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.