0

I'm doing a project in which the matrix is ​​a land and I have to add ecopoints inside it, and I'm having difficulty putting the function inside the main

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

function to add an ecopoint to the terrain, x and y is to get the position, "escolha" is to know which of the ecopoints you want to put

void adicionar(int terreno[][], int x, int y, int escolha)
{
    printf("Qual é a posição de x?\n");
        scanf("%d", &x);
    printf("Qual é a posição de y?\n");
        scanf("%d", &y);
    Printf("Voce Pode Adicionar os seguintes contentores: ");
    printf("---------------------------------------------------------\n");
    printf("1. Ecoponto papelao\n");
    printf("2. Ecoponto vidrao\n");
    printf("3. Ecoponto oleao\n");
    printf("---------------------------------------------------------");
    scanf("%d", &escolha);
    switch(escolha)
        {
            case 1:
                terreno[x][y]="papelao";
                break;

            case 2:
                terreno[x][y]="vidrao";
                break;

            case 3:
                terreno[x][y]="oleao";
                break;

            case 0:
                sair();
                break;
    
            default:
                printf("Digite uma opcao valida\n");
        }
}

int main()
{   
    int continuar=1;
    
    int terreno[1000][1000];

    do
    {
        printf("1. Adicionar Ecoponto\n");
        printf("0. Sair\n\n");

        scanf("%d", &continuar);
        system("cls || clear");

        switch(continuar)
        {
            case 1:
                adicionar();
                break;

            case 0:
                sair();
                break;
    
            default:
                printf("Digite uma opcao valida\n");
        }
    } while(continuar);
}
user438383
  • 5,716
  • 8
  • 28
  • 43
  • 3
    What are the difficulties? – Vlad from Moscow Jan 11 '22 at 12:31
  • What is an "ecopoint"? Is it important to the question? If yes: explain. If no: get rid of it from a minimum workable exmample. – gspr Jan 11 '22 at 12:31
  • 1
    @Francisco Lemos This declaration of the function parameter int terreno[][] is incorrect. – Vlad from Moscow Jan 11 '22 at 12:31
  • None of your scanf calls reads newlines that may be present in the input. It is always a bug to not test the return value from scanf. – Jens Jan 11 '22 at 12:32
  • @VladfromMoscow how i declare a matrix with no defined value? – Francisco Lemos Jan 11 '22 at 12:34
  • Also, you assign strings to int in `terreno[x][y]="papelao";` which your compiler should have told you. Please set the warning level of your compiler to maximum. This will save you countless hours. – Jens Jan 11 '22 at 12:34
  • @gspr is not important to the question. – Francisco Lemos Jan 11 '22 at 12:35
  • @Jens I just wanted this function to give value, then I would have a function to read the value – Francisco Lemos Jan 11 '22 at 12:41
  • @FranciscoLemos Then you should assign the `int` 42, not a string, which is a different type. And replace all your `"%d"` with `" %d"` to make scanf skip any amount of white space. And test whether the scanfs actually return 1. – Jens Jan 11 '22 at 12:44
  • First: get rid of your user input (`scanf("%d", &x);` ) , or put it into separate functions, which can be tested separately. – wildplasser Jan 11 '22 at 12:51
  • Fix a [mre]. Not only does it greatly help debugging. It also makes it easier for us to help you. This is way to much code than necessary to demonstrate the problem. – klutt Jan 11 '22 at 13:32

3 Answers3

0

Try to change its type to char*, since in adicionar you are assigning strings to its cells. Also you should call adicionar with at least 4 parameters from the main, since its declaration.

rikyeah
  • 1,896
  • 4
  • 11
  • 21
0

As pointed out for you in the comments you are trying to assign a string literal to an int with the assignment:

terreno[x][y]="papelao";

I am going to assume that an integer will be fine here based on your comment to Jens.

The first thing you should do is to move the parameters "int x, int y, int escolha" into the function as they receive their values in the function and do not require the calling function to provide any values for them. Then you need to change the declaration of the terreno parameter as that is not how you pass multidimensional arrays. You need to specify how large it is for the compiler to know how many bytes each index represent. It would be possible to leave the first open and just specify the second, i.e. terreno[][1000]. You can read more about that here: https://stackoverflow.com/questions/33328307/why-is-it-allowed-to-omit-the-first-dimension-but-not-the-other-dimensions-when

This would give you a function like this:

void adicionar(int terreno[1000][1000])
{
    int x, y, escolha;

    //Your printf,x and y scanf stuff here

    scanf("%d", &escolha);

    switch (escolha)
    {
      case 1:       
      case 2:
      case 3:
        terreno[x][y] = escolha;
        break;

      case 0:
        sair();
        break;

      default:
        printf("Digite uma opcao valida\n");
     }
}

and you would call it from main with:

case 1:
  adicionar(terreno);
  break;

Good luck, and have fun learning C

rmfeldt
  • 101
  • 1
  • 4
0

According to my understanding of your code, I think the following code works. At least, it is a working example for you.
Problems in your code:

  1. There is one typo in your original code. Should be printf instead of Printf.
  2. If you would like to store strings, you need a char array to do that. In your code, you tried to store strings, such as papelao and vidrao , into a two dimensional integer array. It is just not right.
  3. The variables x, y and escolha should be declared in the function body.
  4. Be sure to check whether the input data is out of bound. In the following code, you can see some while loops doing that.
  5. I really think you should consider using a different data structure to realize the program, for example, make a simple dimensional array to store the three different types. And then use a two dimensional array to store the corresponding index.
  6. And another thing needs to say, according to your implementation, the array terreno is just too large to stay in the function main locally. So it should be declared outside of all functions.
  7. I do not understand the word sair, but I guess it means end or exit, so I make it that way.
  8. The last thing is that be sure to have return 0 as the last statement of your main function. And put the word void in the parens of main when you do not need attributes.
    Hope this is useful to you.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define XSIZE 1000
#define YSIZE 1000
#define TYPENAME 50
char terreno[XSIZE][YSIZE][TYPENAME];

void sair()
{
    /* I don't understand what this function should do */
    exit(0);
}

void adicionar(char terreno[XSIZE][YSIZE][TYPENAME])
{
    int x = -1, y = -1, escolha = -1;

    printf("Qual é a posição de x?\n");
    while (!(x >= 1 && x <= XSIZE)) {
        scanf("%d", &x);
        if (x < 1 || x > XSIZE) {
            printf("Please input x between 1 and 1000: ");
        }
    }

    printf("Qual é a posição de y?\n");
    while (!(y >= 1 && y <= YSIZE)) {
        scanf("%d", &y);
        if (y < 1 || y > YSIZE) {
            printf("Please input y between 1 and 1000: ");
        }
    }

    x--;
    y--;

    printf("Voce Pode Adicionar os seguintes contentores: \n");
    printf("---------------------------------------------------------\n");
    printf("1. Ecoponto papelao\n");
    printf("2. Ecoponto vidrao\n");
    printf("3. Ecoponto oleao\n");
    printf("---------------------------------------------------------\n");
    while (!(escolha >= 1 && escolha <= 3)) {
        scanf("%d", &escolha);
        if (escolha < 1 || escolha > 3) {
            printf("Invalid option. Please try again: ");
        }
    }

    switch(escolha)
        {
            case 1:
                strcpy(terreno[x][y], "papelao");
                break;

            case 2:
                strcpy(terreno[x][y], "vidrao");
                break;

            case 3:
                strcpy(terreno[x][y], "oleao");
                break;

            case 0:
                sair();
                break;
    
            default:
                printf("Digite uma opcao valida\n");
        }
}

int main(void)
{   
    int continuar=1;
    

    do
    {
        printf("1. Adicionar Ecoponto\n");
        printf("0. Sair\n\n");

        scanf("%d", &continuar);
        system("cls || clear");

        switch(continuar)
        {
            case 1:
                adicionar(terreno);
                break;

            case 0:
                sair();
                break;
    
            default:
                printf("Digite uma opcao valida\n");
        }
    } while(continuar);

    return 0;
}
Wu Xiliang
  • 316
  • 3
  • 10