-3

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.

enter image description here

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);
}
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
dinhvan2804
  • 93
  • 14
  • See [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). – R Sahu Dec 28 '15 at 03:23
  • 1
    Note that [Using `fflush(stdin)`](https://stackoverflow.com/questions/2979209/using-fflushstdin) is mildly controversial – but it works OK on Windows. Mixing standard I/O such as `gets_s()` with C++ stream I/O such as `std::cin >> nv.money` is often regarded as a bad idea. – Jonathan Leffler Dec 28 '15 at 03:45
  • thanks man. But how should i fix it man ? , i have used fget_s,getw_s but when i input information, an error dialogue always come up. – dinhvan2804 Dec 28 '15 at 05:59

1 Answers1

0

use fflush(stdin) before gets_s() ... It will clear the input stream, and gets_s() will be called. The reason gets_s() is skipping is that some data rests in the input stream from previous input, and that is filling up the gets_s(), causing the gets_s() to skip and take the leftover data in input stream as input.

haider_kazal
  • 3,298
  • 2
  • 15
  • 39