I'm new to programming. I tried to write a program which can write the name and print to filename txt
. I can run it but it has some problems.
When I try to input information, sometimes it automatically moves to the next row. I think it is due to the gets_s
function but I'm not sure what those problems are.
My code is below:
#include <iostream>
#include <conio.h>
#include <string.h>
struct date
{
int day ,month, year;
};
struct employee
{
int ID;
char name[100];
date birthdate;
char address[20];
char rank[20];
int money;
};
void Inputinformation(char *filename)
{
char s[20];
FILE *fp;
errno_t err;
err = fopen_s(&fp,filename, "ab");
employee nv;
std::cout<<"\nInput information of an employee:\n";
std::cout<<"\tInput ID : ";
std::cin >> nv.ID;
fflush(stdin);
std::cout<<"\tInput name : ";
gets_s(s);
gets_s(nv.name);
std::cout<<"\tInput birthdate (Day Month Year ) : ";
std::cin >> nv.birthdate.day >> nv.birthdate.month>> nv.birthdate.year;
fflush(stdin);
std::cout<<"\tInput address: ";
gets_s(s);
gets_s(nv.address);
std::cout<<"\tInput rank : ";
gets_s(s);
gets_s(nv.rank);
std::cout<<"\tMoney : ";
std::cin >> nv.money;
fwrite(&nv, sizeof(nv), 1, fp);
fclose(fp);
}
void Show(char *filename)
{
employee nv;
FILE *fp;
errno_t err;
err = fopen_s(&fp,filename, "rb");
std::cout<< "ID:", "NAME:", "BIRTHDATE", "ADDRESS:", "RANK:", "MONEY:";
do
{
fread(&nv, sizeof(nv), 1, fp);
if (feof(fp))
break;
std::cout<< nv.ID<<nv.name<<nv.birthdate.day<<nv.birthdate.month<< nv.birthdate.year <<nv.address<<nv.rank<<nv.money;
} while (0 == 0);
//Buoc 4 :Close
fclose(fp);
}
//============================================================================
//SHow information of employee
void Show1(employee nv)
{
std::cout << " " << nv.ID<<"\n";
std::cout << nv.name<<"\n";
std::cout<< nv.birthdate.day<<nv.birthdate.month<<nv.birthdate.year<<"\n";
std::cout << nv.address<<"\n";
std::cout << nv.rank<<"\n";
std::cout << nv.money<<"\n";
}
void Findname(char *szName, char *szFilename)
{
employee nv;
int find = 0;
// 1 : file pointer
FILE *fp;
errno_t err;
// 2 : Open file
err = fopen_s(&fp,szFilename, "rb");
if (fp == NULL)
{
std::cout << "\nfailed to open file!!!";
}
else
{ // 3 : Read contend and show it
do
{
fread(&nv, sizeof(nv), 1, fp);
if (_stricmp(nv.name, szName) == 0)
{
std::cout<< "ID: NAME: BIRTHDATE: ADDRESS: RANK: MONEY:";
Show1(nv);
find = 1;
break;
}
} while (!feof(fp));
//Buoc 4 :Close
fclose(fp);
}
if (find == 0)
printf("cant find this employee !!!");
}
//===========================================================
void main()
{
char filename[100] = "fool.txt";
int i, n;
int pick;
employee dsnv[100];
//Menu
do
{
std::cout << "\n*********************************************";
std::cout << "\n* 1.Input info of new employee *";
std::cout << "\n* 2. Print the list *";
std::cout << "\n* 3. Find the name of employee *";
std::cout << "\n* 4. Arrange the list by name *";
std::cout << "\n* 5. Exit *";
std::cout << "\n*********************************************";
std::cout << "\n\nWhat do you choose ?(1->5)";
std::cin >> pick;
switch (pick)
{
case 1:
std::cout << "\nInput n=";
std::cin >> n;
for (i = 0; i < n; i++)
Inputinformation(filename);
break;
case 2:
Show(filename);
break;
case 3:
char nameofemployee[100];
std::cout << "\Input name:";
fflush(stdin);
gets_s(nameofemployee);
Findname(nameofemployee, filename);
break;
case 4:
break;
case 5:
break;
}
} while (0 == 0);
}