Question

I coded a C Program as below:-

#include <stdio.h>
#include <conio.h>
#include <string.h>
char getPositions(int randNo, int guessNo);

void main()
{
    char positions[6];
    clrscr();
    positions = getPositions(5242, 5243);
    printf(positions);
    getchar();

}
char getPositions(int randNo, int guessNo)
{
    char outPut[6];
    int randNoArr[4], guessNoArr[4];
    int c, w, p;

    for(int i=4; i>0; i--){
        randNoArr[i] = randNo%10;
        randNo /= 10;

        guessNoArr[i] = guessNo%10;
        guessNo /= 10;
        //If Number Possitioned Right Place Incress variable c
        if(randNoArr[i] == guessNoArr[i]){
            c++;
        }
    }
    for(int j=1; j<=4; j++){
        if(guessNoArr[j] != randNoArr[1] && guessNoArr[j] != randNoArr[2] && guessNoArr[j] && randNoArr[3] || guessNoArr[j] && randNoArr[4]){
            w++;
        }
    }
    if(guessNoArr[1] == randNoArr[2] || guessNoArr[1] == randNoArr[3] || guessNoArr[1] == randNoArr[4]){
        p++;
    }
    if(guessNoArr[2] == randNoArr[1] || guessNoArr[2] == randNoArr[3] || guessNoArr[2] == randNoArr[4]){
        p++;
    }
    if(guessNoArr[3] == randNoArr[1] || guessNoArr[3] == randNoArr[2] || guessNoArr[3] == randNoArr[4]){
        p++;
    }
    if(guessNoArr[4] == randNoArr[1] || guessNoArr[2] == randNoArr[2] || guessNoArr[2] == randNoArr[3]){
        p++;
    }
    sprintf(outPut, "%dC%dW%dP", c,w,p);
    return outPut;
}

I have Two Errors:

Error prog1.CPP 10: Lvalue required //positions = getPositions(5242, 5243);
Error prog1.cpp 50: Cannot convert 'char *' to 'char' //return outPut;
Was it helpful?

Solution 2

The problem was you try to put a pointer into an array. In C you cant copy pointers content by using a simple equal.

Here the solution.

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void getPositions(char *outPut, int randNo, int guessNo);

    void main()
    {
        char positions[6];
        clrscr();
        getPositions(positions, 5242, 5243);
        printf(positions);
        getchar();

    }
    void getPositions(char *outPut, int randNo, int guessNo)
    {
        int randNoArr[4], guessNoArr[4];
        int c, w, p;

        for(int i=4; i>0; i--){
            randNoArr[i] = randNo%10;
            randNo /= 10;

            guessNoArr[i] = guessNo%10;
            guessNo /= 10;
            //If Number Possitioned Right Place Incress variable c
            if(randNoArr[i] == guessNoArr[i]){
                c++;
            }
        }
        for(int j=1; j<=4; j++){
            if(guessNoArr[j] != randNoArr[1] && guessNoArr[j] != randNoArr[2] && guessNoArr[j] && randNoArr[3] || guessNoArr[j] && randNoArr[4]){
                w++;
            }
        }
        if(guessNoArr[1] == randNoArr[2] || guessNoArr[1] == randNoArr[3] || guessNoArr[1] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[2] == randNoArr[1] || guessNoArr[2] == randNoArr[3] || guessNoArr[2] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[3] == randNoArr[1] || guessNoArr[3] == randNoArr[2] || guessNoArr[3] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[4] == randNoArr[1] || guessNoArr[2] == randNoArr[2] || guessNoArr[2] == randNoArr[3]){
            p++;
        }
        sprintf(outPut, "%dC%dW%dP", c,w,p);
    }

That's was your code, but i'll compile and ran this following (i removed #include and clrscr();):

    #include <stdio.h>
    #include <string.h>

    void getPositions(char *outPut, int randNo, int guessNo);

    void main()
    {
        char positions[6];

        getPositions(positions, 5242, 5243);
        printf(positions);
        getchar();

    }
    void getPositions(char *outPut, int randNo, int guessNo)
    {
        int randNoArr[4], guessNoArr[4];
        int c, w, p, i, j;

        for(i=4; i>0; i--){
            randNoArr[i] = randNo%10;
            randNo /= 10;

            guessNoArr[i] = guessNo%10;
            guessNo /= 10;
            //If Number Possitioned Right Place Incress variable c
            if(randNoArr[i] == guessNoArr[i]){
                c++;
            }
        }
        for(j=1; j<=4; j++){
            if(guessNoArr[j] != randNoArr[1] && guessNoArr[j] != randNoArr[2] && guessNoArr[j] && randNoArr[3] || guessNoArr[j] && randNoArr[4]){
                w++;
            }
        }
        if(guessNoArr[1] == randNoArr[2] || guessNoArr[1] == randNoArr[3] || guessNoArr[1] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[2] == randNoArr[1] || guessNoArr[2] == randNoArr[3] || guessNoArr[2] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[3] == randNoArr[1] || guessNoArr[3] == randNoArr[2] || guessNoArr[3] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[4] == randNoArr[1] || guessNoArr[2] == randNoArr[2] || guessNoArr[2] == randNoArr[3]){
            p++;
        }
        sprintf(outPut, "%dC%dW%dP", c,w,p);
    }

OUTPUT:

    T0109059@P90b11c603564 ~/tmp/test
    $ ./a.exe
    759583832C2272364W2282526P

OTHER TIPS

First you are doing very horrible mistake which is:

trying to return an array declared locally in a function to another function. the right thing to return an array is to return a pointer to it. And make sure the pointer points to a memory you own. the local variable will lose focus when the function return so you do not own them when the function return.

Quick solution for you: Pass the positions array as a 3rd parameter and make the function return void is the easiest thing for you now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top