0

Im making a game about gathering elements from exoplanets. I would have no problem if i could do this all in one function but Im passing the pointer of an array of structs to multiple functions. So I keep getting errors because i dont know how many pointers to put the parameter and argument of each function. especially the function within a function (called missionMenu()) is a real headache to work with. Im also probably messing up the pointers in the function earth() too.. Any help would be greatly appreciated!

EDIT: I need help fixing the scanf in the missionMenu() function and i think misuse of pointers i mentioned above have something to do with it

#include <stdio.h>
#include <stdlib.h>
#define pause system("pause")
#define cls system("cls")
#define flush fflush(stdin)
#define SIZE 1000


main(){

    int count=0;

    ELEMENT* element[SIZE];
    earth(&count,element);

}//End main 

void earth(int *c,ELEMENT **element){

    int userMenuChoice = 0; 
    int i;
    int number;
    displayMenu(); 

    element[*c] = malloc(sizeof(ELEMENT));

    scanf("%i", &userMenuChoice); 

    switch (userMenuChoice) { 
            case 1: 

                missionMenu(element[*c],*c);

                break; 
            case 2: 
                for(i=0;i<*c;i++)
                    printf("\t%i\t%i\n", element[i]->hydrogen);
                break; 
            case 3: 

                break;
    }while(userMenuChoice != 4); 

}//end end

void missionMenu(ELEMENT *element, int c ){
    int missionChoice;
    cls;

    printf("Which planet would you like to escapade/exploit? \n\n"); 
    printf("1. Gliese 436 b \n");
    printf("2. Oxygen Planet\n"); 
    printf("3. 55 Cancri E\n"); 
    printf("4. Nitrogen \n"); 
    printf("Please input your choice (1-3): \n\n"); 

    missionChoice = 0; 
    scanf("%i", &missionChoice); 
    switch (missionChoice) { 
            case 1: 

                cls;
                printf("\n\n\nWelcome to Planet Gliece 436 b, a planet made of burning ice.\n");
                printf("The extreme gravity of this planet forces the water to stay in solid form,\neven though the temperature is over 570° fahrenheit.\n\n");
                pause;




                printf("\n\n\tHow much Hydrogen would you like to collect?");
                //scanf("%i",&collect);
                scanf("%[^\n]s", element->hydrogen); flush;
                printf("\n\ntHow much Oxygen would you like to collect?");
               // scanf("%i",&collect);
                scanf("%[^\n]s", element->oxygen); flush;
                c=c+1;
                break; 
            case 2: 

                break; 
            case 3: 

                break;
            case 4:

                break;
    }while(missionChoice != 5); 
}//end mission menu



ELEMENT** newElement() {
    ELEMENT** element;
    element = malloc(sizeof(ELEMENT));
    return element;
} //end newTeam
11
  • 2
    What's your question? Commented Apr 4, 2014 at 20:12
  • How do i fix the pointers so that both scanf work in Mission Menu Commented Apr 4, 2014 at 20:15
  • 1
    Can you try reducing the problem to the smallest subset that expresses your issue? You've posted a lot of code that likely isn't relevant. Please make this clearer in the question itself or people will vote to close it as unclear. Commented Apr 4, 2014 at 20:18
  • 1
    This seriously needs a breakdown, but as for the part you've spotted, the format string "%[^\n]s" would try assigning a string that is terminated with a '\n' to hydrogen and oxygen, and then try to obtain and discard an 's' character. hydrogen and oxygen aren't even character arrays that could hold a string in the first place... If you want to just store numbers inside, simply use the format string "%d", along with the second argument &element->hydrogen and &element->oxygen. Commented Apr 4, 2014 at 20:28
  • 1
    If you are hoping to have that switch statement issued again and again, until missionChoice == 5, then rather enclose the switch with while like while ( ... ) { switch { ... } }, and if you want switch to get evaluated at least once, enclose your switch with a do like do { switch { ... } } while ( ... ); Commented Apr 4, 2014 at 20:45

1 Answer 1

1

In your main you try to call the earth function with a pointer to an ELEMENT, your array. If you check the definition of earth(...), you see that it accepts a pointer to a pointer to an ELEMENT, which is not what you're passing it.

Sign up to request clarification or add additional context in comments.

2 Comments

That is indeed what I mean
Then you should update your answer to use the correct terminology :-) That kind of edit isn't currently one I'd be able to make successfully.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.