Thread: error: invalid conversion from 'int' to 'int*'

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    error: invalid conversion from 'int' to 'int*'

    Hi everyone, on line 76 I get the error of an invalid conversion from and int to *int. Please bear in mind the function prototype for findmax must be the way it is. Can anyone help me? I appreciate it.
    Code:
    #include <iomanip>
    #include <iostream>
    
    using namespace std;
    
    #define MAX 100
    
    bool isAscending (int *a, int len);
    bool isDescending (int *a, int len);
    bool isInOrder (int *a, int len);
    double avg (const int *x, int len);
    int *findmax (const int *x, int n);
    int getInfo (int *a, int max);
    int total (const int *a, int len);
    void sort (int *x, int n);
    void swap (int &x, int &y);
    
    int main()
    {
        int a[MAX];
        int i = 0;
        int len;
        len = getInfo (a, MAX);
        double x = avg (a, len);
    
        if (len > 0) cout << "Avg: " << x << endl;
        for (;i < len; i++) cout << setw(4) << *(a+i);
        cout << endl;
    
        if (isInOrder(a, len)) cout << "its in order" << endl;
        else sort(a, len);
    
        if (isInOrder(a, len)) cout << "its in order" << endl;
    
        for (i = 0; i < len; i++) cout << setw(4) << *(a+i);
        cout << endl;
    
        return 0;
    }
    
    bool isAscending (int *a, int len)
    {
        for (int i = 0; i < len - 1; i++)
            if (*(a+i) > *(a+i+1)) return false;
        return true;
    }
    
    bool isDescending (int *a, int len)
    {
        for (int i = 0; i < len - 1; i++)
            if (*(a+i) < *(a+i+1)) return false;
        return true;
    }
    
    bool isInOrder (int *a, int len)
    {
        return isAscending(a, len) || isDescending(a, len);
    }
    
    double avg(const int * a, int len)
    {
        int tot = total(a,len);
        return (double)tot / len;
    }
    
    
    int *findmax(const int *x, int n)
    {
        int temp = 0;
        int i = 1;
        while (i <= n)
        {
            if (*(x + i) > *(x + temp)) temp = i;
            i++;
        }
        return temp;
    }
    
    int getInfo(int *a, int max)
    {
        int i = 0;
        while (cin >> *a)
        {
            a++;
            i++;
            if (i == max) break;
        }
        return i;
    }
    
    int total (const int *a, int len)
    {
        int sum = 0;
        while (len--) sum +=*a;
        return sum;
    }
    
    void sort (int *x, int n)
    {
        int j;
        while (n--)
        {
            j = *findmax(x, n);
            swap (*(x + j), *(x + n));
        }
    }
    
    void swap(int &x,int &y){
    int temp = x;
    x = y;
    y = temp;
    }
    Oh sorry! Line 76 is;
    Code:
    return temp;

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    You are supposed to return an integer pointer, not an int. According to my understanding of the meaning of this function, just return (x+temp).

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    I do not think that is it. It gives me the same error only now it says "const int* to int*" for the same line. The prototype for the function must be intact. If I remove the * in the function prototype the program works perfectly but unfortunately I have to use the *.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    x+temp is correct except that it will also require a const_cast to remove the constness.

    The method signature is a bit dubious though, due to the const in only one of the two places.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Thanks everyone! It is now working!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need help with this question:
    By marcuspax in forum C Programming
    Replies: 16
    Last Post: 08-16-2010, 08:12 AM
  2. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM