Hey everyone! Been a while since I have been on the board, but so far I have had no problems..lol Well this semesterI am learning C++ and ran into a bit of trouble.
The program is supposed to read from the supplied file and then count how many files are present. The problem is though that everytime I run it I get a coredump.
Any advice?
Code:
#include<iostream.h>
#include<stdio.h>
#define NAME_LEN 50
typedef struct nodetag{
int recordNum;
char name[NAME_LEN+1];
int quantity;
float unitPrice;
}Hardware;
FILE *fp;
int count(void);
char menu(void);
int main()
{
system("clear");
int count2=0, c=0;
if((fp = fopen("hardware.dat", "r")) == NULL)
cout<< "Error to open the file";
else
{
cout<< "\n\tTotal counts: " << count();
}
return 0;
}
int count(void)
{
int c=0;
int recordNum, quantity;
char name;
float unitPrice;
fscanf(fp, "%d%s%d%f", &recordNum, name, &quantity, &unitPrice);
while(!feof(fp))
{
c++;
fscanf(fp, "%d%s%d%f", &recordNum, name, &quantity, &unitPrice);
}
return c;
}