Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

c - How do I search through an array?

For my assignment I have to create a structure that allows the user to enter student info (ID, DOB, & Phone number). I have no problem doing this that is quite simple. Now I need to search through that enter info using the student ID to display that students corresponding DOB and phone number, this is the problem that I am having trouble working with. If you see any other problems with my program please let me know what is wrong and why I should change so I can learn from my mistakes.

I'm also not sure how to store all these different parts of the students info to an array and have them correspond to each other. So when I search an ID how does it know to return the correct DOB and phone. I'm really lost here guys and needing some help. What ever you tell me or if you give me code, please explain why I should be doing what you're telling me to do.

NOTE: All my classes are online so getting hold of my professor for help is a challenge, so I've turn to you guys for help.

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

struct infoStruct 
{
    int studentID;
    int year;
    int month;
    int day;
    int phone;
    int end;
};

int main (void)
{
    int students = 0;   
    int infoArray [students];
    struct infoStruct info;
    int studentID;
    int year;
    int month;
    int day;
    int phone;
    int end;



    while (info.end != -1) {
        students = students + 1;
        printf("Enter student information (ID, day, month, year, phone)
");
        printf("Enter -1 following the phone number to end the process to continue enter 0
");
        scanf("%d %d %d %d %d %d", &info.studentID, &info.day, &info.month, &info.year, &info.phone, &info.end);
    }
    if (info.end = -1){
        printf("You entered %d student(s)
", students);
    }
    //Student Search
    printf("Please enter the student ID of the student your looking for
.");
    scanf("%d", info.studentID);
    printf(" DOB: %d %d %d, Phone: %d", info.month, info.day, info.year, info.phone);

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i need to read better want you want to do, however, to simplify your code for you, and for who read it, don't write everything in your main and separate in functions. After that you can call that functions and, with their names, program understanding will be increased.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...