ok im writing a program for school and the book said to open files one way and the compiler gave me an error i messed with and changed it to a different way and it ran but then crashed trying to open the file so i must be getting half way right but im not sure?
and also yes im aware you people hate gets() but i ahve to use it for school so deal with it. and if this isnt formatted 100% like C its probably cause this on a .cpp file and also our teacher doesnt really care as this is mainly a class on data structures but with a new dialect and that is C. and im using // comments not old /**/ so dont kill me for it.
however logic and other errors feel free to let me have it.
c:\skool\program 3\proj 3\assignment3.cpp(43): error C2440: '=' : cannot convert from 'bool' to 'FILE *'Code:#include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> struct data { int cust_id; char cust_name[30]; char state[3]; char dis_code; double balance; int outstanding_orders; }; int get_int(char msg[]); double get_double(char msg[]); void printfile(data records[],int elementsize); int binary(data records[], int elementsize, char cust_id_search); int main() { data records[12]; int response; int elementsize = 0; int found; int cust_id_search; char filename[30]; char buf[10]; FILE *fp; response = get_int("ENTER A CHOICE(use the numbers):\n1.LOAD THE FILE\n2.LIST THE FILE\n3.SEARCH THE FILE\n4.ADD A RECORD\n5.EXIT THE PROGRAM\n"); while(response != 5) { if(response == 1) { printf("Enter the name of the file: \n"); gets(filename); //fp = fopen(filename,"r+"); //if(fp == NULL) //the above two lines fix the problem but it crashes when //i try and open file if(fp = fopen(filename,"r+")== NULL)//ERROR 1 { printf("CANNOT OPEN FILE\n"); } else { //fp = fopen(filename,"r+"); fscanf(fp,"%d%s%s%c%f%d",records[elementsize].cust_id, records[elementsize].cust_name, records[elementsize].state, records[elementsize].dis_code, records[elementsize].balance, records[elementsize].outstanding_orders); while(!feof(fp)) { elementsize++; fscanf(fp,"%d%s%s%c%f%d",records[elementsize].cust_id, records[elementsize].cust_name, records[elementsize].state, records[elementsize].dis_code, records[elementsize].balance, records[elementsize].outstanding_orders); } } } else if(response == 2) { printfile(records,elementsize); } else if(response == 3) { cust_id_search = get_int("Enter the customer ID youd like to search for? \n"); found = binary(records,elementsize,cust_id_search); if(found == -1) printf("DATA NOT FOUND\n"); else printf("%d\n%s\n%s\n%c\n%f\n%d",records[found].cust_id, records[found].cust_name, records[found].state, records[found].dis_code, records[found].balance, records[found].outstanding_orders); } else if(response == 4) { records[elementsize+1].cust_id = get_int("enter the new records customer id\n"); printf("Enter the customers name\n"); gets(records[elementsize+1].cust_name); printf("Enter the customers State\n"); gets(records[elementsize+1].state); printf("Enter the customers distribution code\n"); gets(buf); records[elementsize+1].dis_code = buf[0]; records[elementsize+1].balance = get_double("Enter the customers balance\n"); records[elementsize+1].outstanding_orders = get_int("Enter the customers outstanding orders\n"); elementsize++; fprintf(fp,"%d\n%s\n%s\n%c\n%f\n%d",records[elementsize].cust_id, records[elementsize].cust_name, records[elementsize].state, records[elementsize].dis_code, records[elementsize].balance, records[elementsize].outstanding_orders); } else; } fclose(fp); return 0; } void printfile(data records[],int elementsize) { for(int x = 0;x < elementsize;x++) printf("%d\n%s\n%s\n%c\n%f\n%d\n",records[x].cust_id, records[x].cust_name, records[x].state, records[x].dis_code, records[x].balance, records[x].outstanding_orders); } int binary(data records[], int elementsize, char cust_id_search) { int first = 0; int last = elementsize - 1; int found = 0; int mid; while(first <= last && found == 0) { mid = (first + last)/2; if(records[mid].cust_id == cust_id_search) { found = 1; } else if(records[mid].cust_id < cust_id_search) { first = mid + 1; } else last = mid - 1; } if(found == 0) mid = -1; else; return mid; } //gets an integer from the keyboard int get_int(char msg[]) { char buf[40]; printf(msg); gets(buf); return atoi(buf); } //gets a double from the keyboard double get_double(char msg[]) { char buf[40]; printf(msg); gets(buf); return atof(buf); }



LinkBack URL
About LinkBacks


