Need some help with tweeking a simple program 
Author Message
 Need some help with tweeking a simple program

Hi

I've get three problems

1. Will make the file but will not write the data to it
    well I cant find data in it just through dos editor
    the data is displayed at the end which should mean
    it is being saved somewhere

2. Cannot put a space between anything entered in
    "Descript"
     Eg:      If I enter "White' its OK
                 but "black Lab" put auto enters in the next fscanf()

3. I get an error message at the end of the program
    illegal opertion

Also code it to enter until no more ?
At the moment I've just got      for(i=0; i<2; i++)

// This program is to be designed to accommodate a new computer system at a
Vet.
//    It will store records of all animals, which are treated in data file.
//                      The file is called "pets.dat".

struct vet
{
 int id;
 char descript[80];
 int time;
 float cost;

Quote:
};

typedef struct vet visit;

 void DataReceive(visit*ptr);
 void DataDisplay(visit*ptr);
 void CheckCriteria(visit*ptr);

#include <stdio.h>
#include <stdlib.h>

FILE *pet_file;

int main(void)
{
 visit pet[1];
 int i;

 pet_file = fopen("a:\\sa1\\pet.dat","w");
 if(!pet_file)
    {
     puts("An error occurred while opening the file!");
       EXIT_FAILURE;
    }

 for(i=0; i<2; i++)
   {
      DataReceive(&pet[i]);
      CheckCriteria(&pet[i]);
      fclose(pet_file);
   }
 pet_file = fopen("a:\\sa1\\pet.dat","r");
 if(!pet_file)
    {
     puts("An error occurred while opening the file!");
       EXIT_FAILURE;
    }
   for(i=0; i<2; i++)
   {
  DataDisplay(&pet[i]);
      fclose(pet_file);
   }
 return 0;

Quote:
}

void DataReceive(visit*ptr)
{
 printf("What's your ID number? :");
 fscanf(stdin,"%d", &(*ptr).id);
   printf("Describe your pet? :");
 fscanf(stdin, "%s", &(*ptr).descript);
 printf("How long did it take :");
 fscanf(stdin,"%d", &(*ptr).time);
 printf("How much will it cost :");
 fscanf(stdin,"%f", &(*ptr).cost);

Quote:
}

void DataDisplay(visit*ptr)
{
 printf("\nHere are what you entered :\n");
 printf("ID #: %04d\n", (*ptr).id);
 printf("Description: %s\n", (*ptr).descript);
 printf("Time: %02d\n", (*ptr).time);
 printf("Price: %0.2f\n", (*ptr).cost);

Quote:
}

void CheckCriteria(visit*ptr)
{
 if((*ptr).cost > 500)
     (*ptr).cost = 500;
 else if ((*ptr).cost < 20)
     (*ptr).cost = 20;
Quote:
}



Sat, 26 Oct 2002 03:00:00 GMT  
 Need some help with tweeking a simple program

: Hi

: I've get three problems

: 1. Will make the file but will not write the data to it
:     well I cant find data in it just through dos editor
:     the data is displayed at the end which should mean
:     it is being saved somewhere

The data is not being written because you have no code for writing it.
Just because it appears on your screen (stdout) doesn't mean it goes
into a file.

: 2. Cannot put a space between anything entered in
:     "Descript"
:      Eg:      If I enter "White' its OK
:                  but "black Lab" put auto enters in the next fscanf()

That's the way the ?scanf() family works. Use fgets() for string input
if you want to include spaces.

: 3. I get an error message at the end of the program
:     illegal opertion

: Also code it to enter until no more ?
: At the moment I've just got      for(i=0; i<2; i++)

: // This program is to be designed to accommodate a new computer system at a
: Vet.
: //    It will store records of all animals, which are treated in data file.
: //                      The file is called "pets.dat".

: struct vet
: {
:  int id;
:  char descript[80];
:  int time;
:  float cost;
: };

: typedef struct vet visit;

:  void DataReceive(visit*ptr);
:  void DataDisplay(visit*ptr);
:  void CheckCriteria(visit*ptr);

: #include <stdio.h>
: #include <stdlib.h>

: FILE *pet_file;

: int main(void)
: {
:  visit pet[1];

No, no, no. The number in the brackets tells how many elements there
are - it does not tell the highest index. Judging by your for loops,
you go from 0 to 1, so you should have visit pet[2]; here.

:  int i;

:  pet_file = fopen("a:\\sa1\\pet.dat","w");
:  if(!pet_file)
:     {
:      puts("An error occurred while opening the file!");
:        EXIT_FAILURE;
:     }

:  for(i=0; i<2; i++)
:    {
:       DataReceive(&pet[i]);
:       CheckCriteria(&pet[i]);
:       fclose(pet_file);

You should only fclose() after you've done with the whole loop.
Move this function call outside the braces.

:    }
:  pet_file = fopen("a:\\sa1\\pet.dat","r");
:  if(!pet_file)
:     {
:      puts("An error occurred while opening the file!");
:        EXIT_FAILURE;
:     }
:    for(i=0; i<2; i++)
:    {
:   DataDisplay(&pet[i]);
:       fclose(pet_file);

Likewise here. It should read first }, then fclose(pet_file);.

:    }
:  return 0;
: }

: void DataReceive(visit*ptr)
: {
:  printf("What's your ID number? :");
:  fscanf(stdin,"%d", &(*ptr).id);
:    printf("Describe your pet? :");
:  fscanf(stdin, "%s", &(*ptr).descript);
:  printf("How long did it take :");
:  fscanf(stdin,"%d", &(*ptr).time);
:  printf("How much will it cost :");
:  fscanf(stdin,"%f", &(*ptr).cost);
: }

: void DataDisplay(visit*ptr)
: {
:  printf("\nHere are what you entered :\n");
:  printf("ID #: %04d\n", (*ptr).id);
:  printf("Description: %s\n", (*ptr).descript);
:  printf("Time: %02d\n", (*ptr).time);
:  printf("Price: %0.2f\n", (*ptr).cost);
: }

The problem which you describe as "the data doesn't get saved" is
because you're not telling it to get saved. You should have calls
to fprintf() here too, like so...
fprintf(pet_file, "\nHere are what you entered :\n");
fprintf(pet_file, "ID #: %04d\n", (*ptr).id);
and so on...
Note that pet_file has to be opened in "write" mode (mode "w").

: void CheckCriteria(visit*ptr)
: {
:  if((*ptr).cost > 500)
:      (*ptr).cost = 500;
:  else if ((*ptr).cost < 20)
:      (*ptr).cost = 20;
: }

This function is correct.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"Roses are red, violets are blue, I'm a schitzophrenic and so am I."
   - Bob Wiley



Sat, 26 Oct 2002 03:00:00 GMT  
 Need some help with tweeking a simple program

Quote:

>  pet_file = fopen("a:\\sa1\\pet.dat","w");
>  if(!pet_file)
>     {
>      puts("An error occurred while opening the file!");
>        EXIT_FAILURE;
>     }

Presumably, you meant 'return EXIT_FAILURE;'.


Sun, 27 Oct 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Need some help in writing a simple C program

2. Need some help in writing a simple C program

3. Need Help on simple ISAM like program

4. need help in extremely simple c program

5. need help debugging simple c program

6. Help on a simple calculator program which needs Trigo, Arithmethic, log functions..[Apologies for repeat mail]

7. Need help with simple C Program

8. Simple BC+31 program - help needed

9. !Need help with simple C program

10. Need Help with a SIMPLE mouse control program in C.

11. Need Help in converting Simple C program to VC

12. Need help with simple dialog program?

 

 
Powered by phpBB® Forum Software