0
#include<stdio.h>
#include<stdlib.h>  //libraries
#include<string.h>

#define ARRAY_SIZE 4 //define the array size is 4

Declare a Person structure containing the following two data members: name and age.

 struct Person
{
 char name[20];
 int age;
};

Define fillPersons function that takes an empty array of Persons, and fills the array.

 void fillPersons(struct Person * myPs);

For function using

 struct Person p[ARRAY_SIZE];

Define searchPerson function that takes an array of Persons, and the name to search for. The function will return the corresponding age if the person is found, -1 otherwise.

int searchPerson(struct Person * myPs, char * myName);

Define printPersons function that takes an array of Persons, and prints the content of the array.

void printPersons(struct Person * myPs);

Main Fuction

int main()
{
struct Person p[ARRAY_SIZE];
int a;
int isFound;
char myName[20];

Switch case for user interface

do
{
    printf("1)FILL ARRAY\n");
    printf("2)SEARCH BY NAME\n");
    printf("3)PRINT ARRAY\n");
    printf("4)EXIT\n");
    printf("Search Operation:");

    scanf_s("%d", &a);
    switch (a)
    {
    case 1:fillPersons(p); break;

I write the main part of searching but i cant fill the function.

    case 2:printf("\Give the name to search for:");
        scanf_s("%s", &myName);
        isFound = searchPerson(p, myName);
        if (isFound == -1)
        {
            printf("%s not avaible in the array. \n", myName);
        }
        else
        {
            printf("The age of %s is %d.\n", myName, isFound);
        }
        break;

After this part it is okay to read names and ages but cant do it searching part.

    case 3:printPersons(p); break; 
    case 4:printf("\nProgram exits ..."); exit(0);
    }
} while (a != 4);

system("pause");
return 0;
}

It is okay this scanf part

void fillPersons(struct Person * myPs)
{
    int i;
    for (i = 0; i < 4; i++)//defining 4 person i<4
    {
        scanf_s("%20s", p[i].name,_countof(p[i].name));//if i dont do _coutof 
        scanf_s("%d", &p[i].age);                      // i get null.ptr error 
    }                                                  //  on visiual studio   
}

Printing the student names and ages

void printPersons(struct Person * myPs)
{
    int i;
    for (i = 0; i < 4; i++)
    {
        printf("%s %d", p[i].name, p[i].age);
    }
}

But i dont know how to fill with inside on fuction?

  int searchPerson(struct Person * myPs, char * myName)

THE OUTPUT SHOULD BE LIKE THIS

output

10
  • First things first, compile your program, find and fix all the errors. Specifically, you call functions with *myPs but you access p[i].variable. So, the rule of SO is that you must post a minimal reproducible example and if you can't get it to compile, point out what errors you get when you try to compile. Commented Dec 19, 2016 at 18:57
  • One more thing, scanf_s("%s", &myName); is not ideal (use fgets instead). Regardless, the call should be scanf_s("%s", myName); Commented Dec 19, 2016 at 18:59
  • If you were sold a C++ course, you need to be informed that the course was missold. Commented Dec 19, 2016 at 18:59
  • @KevinDTimm when i compile it without the search function i get no error. Commented Dec 19, 2016 at 18:59
  • @KevinDTimm thnx for scanf :) Commented Dec 19, 2016 at 19:00

2 Answers 2

2

Seems the function you are missing is strcmp to compare two strings.

It could be like:

int searchPerson(struct Person * myPs, char * myName)
{
    int i;
    for (i = 0; i < ARRAY_SIZE; i++)
    {
        if (strcmp(myPs[i].name, myName) == 0)
        {
            return myPs[i].age;
        }
    }
    return -1;
}

In general notice:

When you pass the array to a function as struct Person * myPs, you must use myPs inside the function. Further you should use the defined array size instead of hard coding a 4.

So your printPersons should be:

void printPersons(struct Person * myPs)
{
    int i;
    for (i = 0; i < ARRAY_SIZE; i++)
    {
        printf("%s %d", myPs[i].name, myPs[i].age);
    }
}

Same applies to fillPersons

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

8 Comments

After using this as my fuction i get the error of couldn't find the name of
@BatuhanGöbekli - did you fix the scanf problem? scanf_s("%s", &myName); -> scanf_s("%s", myName);
@ 4386427 help me please
@BatuhanGöbekli - Don't know what you mean by i get the error of couldn't find the name of ! The name of what? Compile time error or run time error ?
when i write a name after that on searching i get the else part " not avaible in array "
|
0

First thing, you put in your question all the functions except the one that is giving you problems: searchPerson.
Then, your error is with this instruction:

scanf_s("%s", &myName);

Accordingly that you declared myName as a char pointer, this way, you're assigning the string to the address of myName, not to the memory cells it's pointing to.

Instead, this should be:

scanf_s("%s", myName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.