#include <bits/stdc++.h>
#include <conio.h>
#include <iostream>
using namespace std;
typedef struct {
string sdt;
string holot;
string ten;
string gioitinh;
string diachi;
string namsinh;
} SoDienThoai;
vector<string> split (const string& text, const string& delims) {
vector<string> tokens;
size_t start = text.find_first_not_of(delims), end = 0;
while ((end = text.find_first_of(delims, start)) != string::npos) {
tokens.push_back(text.substr(start, end - start));
start = text.find_first_not_of(delims, end);
}
if (start != string::npos) tokens.push_back(text.substr(start));
return tokens;
}
void NhapMang(SoDienThoai s[], int &n, string stm, string delims) {
vector<string> tokens = split(stm, delims);
//================Dua thong tin vao Struct============
// cout << tokens[0] <<" "<< tokens[1]<<" "<<tokens[2]<<" "<< tokens[3]<<" "<< tokens[4]<<" "<< tokens[5]<<endl;
n++;
s[n].sdt = tokens[0];
s[n].holot = tokens[1];
s[n].ten = tokens[2];
s[n].gioitinh = tokens[3];
s[n].diachi = tokens[4];
s[n].namsinh = tokens[5];
tokens.clear();
}
void XuatMang(SoDienThoai s[], int n) {
//================Dua thong tin vao Struct============
for (int i = 1; i <= n; i++)
{
cout <<s[i].sdt << " " << s[i].holot << " " << s[i].ten << " " << s[i].gioitinh << " " << s[i].diachi << " " << s[i].namsinh << endl;
}
}
void timKiemTheoSDT(SoDienThoai s[], int n, string find_sdt) {
for (int i = 1; i <= n; i++){
if (s[i].sdt == find_sdt) {
cout <<s[i].sdt << " " << s[i].holot << " " << s[i].ten << " " << s[i].gioitinh << " " << s[i].diachi << " " << s[i].namsinh << endl;
}
}
}
void pressAnyKey() {
cout << "\n\nBam phim bat ky de tiep tuc...";
getch();
system("cls");
}
int main() {
fflush(stdin);
SoDienThoai S[100];
int N = 0;
string stm;
string delims = ",";
freopen("profile.csv","r",stdin);
while (getline(cin,stm)) {
NhapMang(S,N,stm,delims);
}
cin.ignore();
int key;
string tel;
while(1) {
cout << " CHUONG TRINH STRUCT SO DIEN THOAI \n";
cout << "*************************MENU**************************\n";
cout << "** 1. Hien thi danh sach SoDienThoai. **\n";
cout << "** 2. Tim kiem thong tin nguoi theo so dien thoai. **\n";
cout << "** 3. Sap xep tang dan theo so dien thoai. **\n";
cout << "** 4. Sap xep tang dan theo ten. **\n";
cout << "** 0. Thoat **\n";
cout << "*******************************************************\n";
cout << "Nhap tuy chon: ";
cin >> key;
switch(key){
case 1:
if (N > 0) {
cout << "\nHien thi danh sach SoDienThoai" << endl;
XuatMang(S,N);
}else {
cout << "\nDanh sach trong" << endl;
}
pressAnyKey();
break;
case 2:
cin.ignore();
cout <<"\nNhap vao so dien thoai can tim: ";
getline(cin,tel);
timKiemTheoSDT(S,N,tel);
pressAnyKey();
break;
default:
cout << "\nKhong co chuc nang nay!";
cout << "\nHay chon chuc nang trong hop menu.";
pressAnyKey();
break;
}
}
return 0;
}
Đây là nội dung File .cvs ạ!
0772418791,Le Van,Anh,Nu,Hue,2000
0772418791,Truong Viet,Giang,Nam,HCM,2002
0772418792,Nguyen Thi,Binh,Nu,Da Nang,2001
0772418793,Truong My,Hoa,Nu,Sai Gon,2003
0772418794,Truong Van,Lap,Nam,Quang Tri,2002
0772418795,Ha Trong,Nam,Nam,Hue,2000
0772418765,Nguyen Quoc,Anh,Nam,Hue,2001
Sau khi khai báo file thì không nhập được dữ liệu từ bàn phím ạ!