0

I need to create a program that uses a structure that alocates memory with the new operator. The information must be saved in a file, then displayed. The problem I have is that the program saves only the first character of the words I enter. Could someone please help me make it work.

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <new>
using namespace std;

    FILE *f;
    struct dog {
        char  name;
        char  family;
        int   age;

                }list1, list2;

    int i,n,optie;


//File Dog 
int file_dog()
{   
    dog* plist; 

    cout << "  Point the number of dogs you want to enter\n" << endl;
    cin >> n;   

    plist = new (nothrow) dog[n];
    f=fopen("Dog.txt","w");
    if (plist == 0)
    cout << "Error: memory could not be allocated";
    else
    {
    for(i=0;i<n;i++)
     {
      cout << "  Enter the name of the dog       - " << endl;
      cin >> list1.name;
       fflush(stdin);
      cout << "  Enter the family of the dog     - "  << endl;
      cin >> list1.family;
       fflush(stdin);
      cout << "  Enter the age                   - " << endl;
      cin >> list1.age;
      fwrite(&list1,sizeof(list1),1,f);
      cout << endl;
     }
    cout << endl;
    fclose(f);
    delete []plist;
    }
    return 0;
}


int display_the_file_dog()
{   

    if((f=fopen("Dog.txt","r")) == NULL )
       {
         cout << "Error" << endl;
         exit(1);
       }

    cout << "          Date despre ciini" << endl;
    cout << "  ===================================" << endl;
    cout << "  | Nr |  Name |  Family  | Age | " << endl;
    cout << "  ===================================" << endl;
    i=1;
    fread(&list2,sizeof(list2),1,f);
    while(!feof(f))
    {
     cout << "    " << i++ << "       " << list2.name << "        " << list2.family << "        " << list2.age << endl;
     fread(&list2,sizeof(list2),1,f);
    cout << "  -----------------------------------" << endl;
    }
    cout << endl;
    fclose(f);
    return 0;
}


int main()
{   
    system("color 0");
    system("color f0");

    while (1)
     {
      cout << "                      -------------- M E N I U -------------" << endl;
      cout << "                      ----------- Choose an option ---------" << endl;
      cout << endl;
      cout << "                      | [1] - Create the file Dog          |" << endl;
      cout << "                      | [2] - Display the file Dog         |" << endl;
      cout << "                      | [3] - Exit the program             |" << endl;
      cout << "                      | ---------------------------------- |" << endl;
      cin >> optie; fflush(stdin);
    switch (optie)
     {
      case 1: file_dog(); break;
      case 2: display_the_file_dog(); break;
      case 3: exit(1);
      default: printf("Choose the right option\n"); break;
     } 
    }
    return 0;
}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Saith
0

char name;
char family;
int age;

Could it be that your name is only 1 character long. If you want a full name, consider the use of a string. Same goes for the family.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.