Thank you, rags_to_riches, Elysia.
@ Elysia: The problem is I have to use C-strings because this is what I'm being taught
Okay. I think I have solved the problem in the code in my first post above. Now I face another problem. Please have a look on the output below. It should be "#2" instead. Where am I going wrong? Please help me. Thanks.
Code:
// if_any_student_named_jackson_heights.cpp
// check if any student is named Jackson Heights
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
using namespace std;
struct Student {char name[16]; int rollno; string sex; int age; float marks;};
const int N = 2;
Student stud[N];
int main()
{
int i; char name_search[20];
cout << "enter the students' details below\n\n";
for (i=0; i<N; i++)
{
cout << "\nenter student #" << (i+1) << "\'s name: ";
cin.get(stud[i].name, 16);
cout << "enter roll no.: ";
cin >> stud[i].rollno;
cout << "enter sex: ";
cin >> stud[i].sex;
cout << "enter age: ";
cin >> stud[i].age;
cout << "enter marks: ";
cin >> stud[i].marks;
cin.ignore();
}
cout << "\n\nenter the name to be searched for: ";
cin.get(name_search, 20);
for (i=0; i<N; i++)
{
if (stud[i].name == name_search)
{
break;
}
}
cout << "the student at #" << (i+1) << " in the record"
<< " matches the entered name\n\n";
system("pause");
return 0;
}
OUTPUT
Code:
enter the students' details below
enter student #1's name: jackson
enter roll no.: 1
enter sex: male
enter age: 18
enter marks: 99
enter student #2's name: heights
enter roll no.: 2
enter sex: male
enter age: 19
enter marks: 100
enter the name to be searched for: heights
the student at #3 in the record matches the entered name
Press any key to continue . . .