Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Students Id in array

+9 votes
asked Feb 12, 2018 by Ammar

Using C++ programming. Define a class “Student” with the following public properties and a method “showInfo” to display each property in separate line in a header file “Student.h”

  • student_id        integer
  • student_name string
  • cgpa                 float

Create an array of 5 Students with student_id, student_name and cgpa of your choice. Write a program to search a StudentID in the array and call showInfo method if found, else display an error message.

2 Answers

0 votes
answered Jul 28, 2025 by Abhiram rangavajhala (390 points)
lowk kinda late but this is forbidden according to the updated terms and services. Sorry!
0 votes
answered Jul 28, 2025 by Mohammed Fareeth (140 points)

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

int main() {
    Student students[5] = {
        {101, "Alice", 3.8},
        {102, "Bob", 3.5},
        {103, "Charlie", 3.9},
        {104, "Diana", 3.6},
        {105, "Ethan", 3.7}
    };

    int searchId;
    printf("Enter Student ID to search: ");
    scanf("%d", &searchId);

    int found = 0;
    for (int i = 0; i < 5; i++) {
        if (students[i].student_id == searchId) {
            showInfo(students[i]);
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("Error: Student ID not found.\n");
    }

    return 0;
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...