A c++ program that prompts the user to enter an ID number then the program uses
ID: 3823686 • Letter: A
Question
A c++ program that prompts the user to enter an ID number then the program uses the input to scan and search for a Students ID # or from a pre-created file that contains the students information(the name, student ID number, grade level, residence address, city, state, zip-code, and home phone number) in a horizontal column format using at least one function and a loop. Once the program finds the information on the student, all the information pertaining to said student( in the same row, separated by a space) should be displayed to the screen. Please do not use the header file (Assuming the file has structure in format: last name first name student ID number grade level residence address city state zip-code phone number) i.e. ACHON,JORGE 91359677 10 8235W41STREET SALTLAKE CL 44523 7842553830
Explanation / Answer
/* This is used for importing header files */
#include <iostream>
#include <string>
using namespace std;
/*Define the structure */
struct student {
string id;
string lname;
string fname;
int grade;
string address;
string city;
string state;
int zip;
long long phone;
};
/*funtion to display data*/
void showData(struct student s)
{
cout<<" ID: "<<s.id<<" LAST NAME: "<<s.lname
<<" FIRST NAME: "<<s.fname<<" GRADE: "<<s.grade
<<" ADDRESS: "<<s.address<<" CITY: "<<s.city
<<" STATE: "<<s.state<<" ZIPCODE: "<<s.zip<<" PHONE: "<<s.phone;
}
/*Funtion to get data from user*/
struct student setData()
{
struct student s1;
cout<<"Enter Student Id ";
cin>>s1.id;
cout<<"Enter Last Name ";
cin>>s1.lname;
cout<<"Enter First Name ";
cin>>s1.fname;
cout<<"Enter Student Grade ";
cin>>s1.grade;
cout<<"Enter Residential Address ";
cin.ignore(); /*user to clear buffer, so that text including space can be read easily*/
getline(cin,s1.address);
cout<<"Enter City Name ";
cin>>s1.city;
cout<<"Enter State Name ";
cin>>s1.state;
cout<<"Enter Zip Code ";
cin>>s1.zip;
cout<<"Enter Phone No. ";
cin>>s1.phone;
return s1;
}
/*Funtion which will execute first*/
int main() {
// your code goes here
struct student s1;
s1 = setData();
string search;
cout<<"Enter ID or First Name or Last Name to search";
cin>>search;
if(s1.id == search || s1.lname == search || s1.fname == search)
showData(s1);
else
cout<<"No DATA FOUND";
return 0;
}
/*
Above example is for reading one input, searching that input and if found display it.
To search from many records, use loop as following
struct student s1[10];
for(int i=0;i<10;i++)
s1[i]= setData();
cout<<"Enter ID or First Name or Last Name to search";
cin>>search;
for(int i=0;i<10;i++)
{
if(s1[i].id == search || s1[i].lname == search || s1[i].fname == search)
showData(s1[i]);
}
In found any issue, please use comment section to let me know.
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.