Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Finding a integer in a file

Options
  • 15-03-2012 7:08pm
    #1
    Registered Users Posts: 620 ✭✭✭


    Hey guys I am working on a assignment for next week, basically I have to create a structure for an employee database. Everything is running fine in the code its step two that is wrecking my head! If the employee identification number is already in the file I have to produce an error. I cant think how to do this, i have researched fread but i tried using it to no avail! I'm not looking for code help here more so advice on what to use!I'll post my code so you can have a better understanding
    #include <stdio.h>
    
    int main()
    {
    
    	FILE *fp, *fp2;
    	
    
    
    	struct info{
    		char first_name[16];
    		char last_name[16];
    		int identification;
    		int hours;
    		float rate;
    		float tax;
    		float gross;
    		float net;
    		}id;
    		
    		printf("Please enter ID Number: ");
    		scanf("%d",&id.identification);
    		fflush(stdin);
    		printf("Please enter First name: ");
    		fgets(id.first_name,16,stdin);
    		printf("Please enter Last name: ");
    		fgets(id.last_name,16,stdin);
    		printf("Please enter hours worked: ");
    		scanf("%d",&id.hours);
    		printf("Please enter rate of pay: ");
    		scanf("%f",&id.rate);
    		printf("Please enter rate of tax: ");
    		scanf("%f",&id.tax);
    		id.gross= id.hours*id.rate;
    		id.net=id.gross-(id.gross*id.tax/100);
    		
    		
    		fp=fopen("employee.dat","a");
    		fprintf(fp,"\nID Number: %d\n",id.identification);
    		fprintf(fp,"First Name: %s",id.first_name);
    		fprintf(fp,"Last Name: %s",id.last_name);
    		fprintf(fp,"Hours Worked: %d\n",id.hours);
    		fprintf(fp,"Rate of Pay: %.2f\n",id.rate);
    		fprintf(fp,"Rate of Tax: %.2f\n",id.tax);
    		fprintf(fp,"Gross Pay: %.2f\n",id.gross);
    		fprintf(fp,"Net Pay: %.2f\n",id.net);
    		
    		fp2=fopen("employee.dat","r");		
    		
    		
    		
    return 0;
    }
    


Comments

  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    What kind of file are you reading from? I just ripped some code from a program of mine that reads from a text file and converts the text to integer, perhaps it will help...
    #include <stdio.h>
    #include <stdlib.h>
    #include <sstream>
    #include <iostream>
    #include <string>
    #include <fstream>
    
    ifstream myFile;
    
    myFile.open("aFile.txt");
    
    string data;
    
    int intVal = 0;
    
    
    
    getline(myFile, data, ','); //my file is comma delimited, yours is probably different
    
    intVal = atoi(data.c_str()); now you can compare intVal to the other id numbers for any matches
    


  • Registered Users Posts: 2,781 ✭✭✭amen


    minor item but your struct as your ID as the third element but you write ID to the file in the first position.

    While not wrong this is just bad practice. The file write/read order should match the struct. Makes life a lot easier when debugging.


  • Registered Users Posts: 620 ✭✭✭Laika1986


    What kind of file are you reading from? I just ripped some code from a program of mine that reads from a text file and converts the text to integer, perhaps it will help...
    #include <stdio.h>
    #include <stdlib.h>
    #include <sstream>
    #include <iostream>
    #include <string>
    #include <fstream>
    
    ifstream myFile;
    
    myFile.open("aFile.txt");
    
    string data;
    
    int intVal = 0;
    
    
    
    getline(myFile, data, ','); //my file is comma delimited, yours is probably different[B]What do you mean by comma delimited!?[/B]
    
    intVal = atoi(data[B].c_str())[/B]; now you can compare intVal to the other id numbers for any matches
    

    I am reading from a .dat file. I was thinking about it last night and i intended to try using atoi! Ill mess around with what you provided(thanks very much by the way!) My only confusion are the parts i have bolded. Could you explain them to me?

    @amen

    Thanks for the tip. I removed it yesterday evening as I wanted to figure out how to pass the structure to a function. I am going to post my new code below as it wont compile i get an error "incompatiable type when assigning type File from type struct, can anyone help me on this?
    #include <stdio.h>
    
    	
    	struct info{
    		char first_name[16];
    		char last_name[16];
    		int identification;
    		int hours;
    		float rate;
    		float tax;
    		float gross;
    		float net;
    		};
    		
    		typedef struct info next;
    		
    		next DataReceive(next id);
    		
    		main(void)
    		{
    		
    		
    		next id;
    		id= DataReceive(id);
    		
    		return 0;
    		}
    		
    		next DataReceive(next id)
    		{
    		FILE *fp,fp2;
    		printf("Please enter ID Number: ");
    		scanf("%d",&id.identification);
    		fflush(stdin);
    		printf("Please enter First name: ");
    		fgets(id.first_name,16,stdin);
    		printf("Please enter Last name: ");
    		fgets(id.last_name,16,stdin);
    		printf("Please enter hours worked: ");
    		scanf("%d",&id.hours);
    		printf("Please enter rate of pay: ");
    		scanf("%.2f",&id.rate);
    		printf("Please enter rate of tax: ");
    		scanf("%.2f",&id.tax);
    		id.gross= id.hours*id.rate;
    		id.net=id.gross-(id.gross*id.tax/100);
    		
    		
    		fp=fopen("employee.dat","a");
    		fprintf(fp,"\nID Number: %d\n",id.identification);
    		fprintf(fp,"First Name: %s",id.first_name);
    		fprintf(fp,"Last Name: %s",id.last_name);
    		fprintf(fp, "Hours Worked: %d\n",id.hours);
    		fprintf(fp,"Rate of Pay: %.2f\n",id.rate);
    		fprintf(fp,"Rate of Tax: %.2f\n",id.tax);
    		fprintf(fp,"Gross Pay: %.2f\n",id.gross);
    		fprintf(fp,"Net Pay: %.2f\n",id.net);
    		
    		fp2=fopen("employee.dat","r");		
    		
    		return id;
    		}
    

    Someday I am looking forward to being able to help on here not just look for answers!!


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    Comma delimited basically means that each piece of data in the file is separated by a comma:

    23,44,12,66,764

    So the program knows when it reaches a comma that it has all of that particular piece of data and that it can move onto the next. You dont necessarily have to use a comma either, you could use any character or string as a "delimiter" (Google that word).



    The c_str() method turns a string into a cstring. The atoi function requires a cstring to work. When we read in the file, we are reading strings as opposed to cstrings so the conversion is required.

    string and cstring are basically the same thing but one is part of the standard language and the other is part of MFC. (Google MFC, your going to come across that allot)


  • Registered Users Posts: 620 ✭✭✭Laika1986


    Kidchameleon thanks for your reply, I didn't have much time over the weekend to reply. I have researched what you suggested and am on the right track however the only problem im coming across is when i print the variables first and last name to the file it skips a line. I have tried loads of different combinations for this but i can't seem to get it in sync. I am presuming this is somehow caused by fgets?


  • Advertisement
  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    Laika1986 wrote: »
    Kidchameleon thanks for your reply, I didn't have much time over the weekend to reply. I have researched what you suggested and am on the right track however the only problem im coming across is when i print the variables first and last name to the file it skips a line. I have tried loads of different combinations for this but i can't seem to get it in sync. I am presuming this is somehow caused by fgets?

    Try removing the "\n" from any write statements. This adds a new line to any output.


  • Registered Users Posts: 620 ✭✭✭Laika1986


    Try removing the "\n" from any write statements. This adds a new line to any output.

    I tried that, there's no "\n" in the program at all however here is what it produces
    ID Number: 1651656,First Name: Super 
    ,Last Name: Man
    , Hours Worked: 40,Rate of Pay: 8.65,Rate of Tax: 12.75,Gross Pay: 346.00,Net Pay: 301.89,
    


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    Laika1986 wrote: »
    Try removing the "\n" from any write statements. This adds a new line to any output.

    I tried that, there's no "\n" in the program at all however here is what it produces
    ID Number: 1651656,First Name: Super 
    ,Last Name: Man
    , Hours Worked: 40,Rate of Pay: 8.65,Rate of Tax: 12.75,Gross Pay: 346.00,Net Pay: 301.89,
    

    Could you post your code as is and ill have a look im afc at the moment


  • Registered Users Posts: 620 ✭✭✭Laika1986


    Could you post your code as is and ill have a look im afc at the moment
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main()
    {
    
    	FILE *fp, *fp2;
    	fp2=fopen("employee.dat", "r");
    	
    
    
    	struct info{
    		char first_name[16];
    		char last_name[16];
    		int identification;
    		int hours;
    		float rate;
    		float tax;
    		float gross;
    		float net;
    		}id;
    		
    		printf("Please enter ID Number: ");
    		scanf("%d",&id.identification);
    		fflush(stdin);
    		printf("Please enter First name: ");
    		fgets(id.first_name,16,stdin);
    		printf("Please enter Last name: ");
    		fgets(id.last_name,16,stdin);
    		printf("Please enter hours worked: ");
    		scanf("%d",&id.hours);
    		printf("Please enter rate of pay: ");
    		scanf("%f",&id.rate);
    		printf("Please enter rate of tax: ");
    		scanf("%f",&id.tax);
    		id.gross= id.hours*id.rate;
    		id.net=id.gross-(id.gross*id.tax/100);
    		
    		
    		fp=fopen("employee.dat","w");
    		fprintf(fp,"ID Number: %d,",id.identification);
    		fprintf(fp,"First Name: %s,",id.first_name);
    		fprintf(fp,"Last Name: %s, ",id.last_name);
    		fprintf(fp,"Hours Worked: %d,",id.hours);
    		fprintf(fp,"Rate of Pay: %.2f,",id.rate);
    		fprintf(fp,"Rate of Tax: %.2f,",id.tax);
    		fprintf(fp,"Gross Pay: %.2f,",id.gross);
    		fprintf(fp,"Net Pay: %.2f,",id.net);
    		
    		fp2=fopen("employee.dat","r");		
    		
    		
    		
    return 0;
    }
    

    Thanks again whenever you get a chance no rush


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    ok just had a look at your Code.

    first off where is your
    "namespace std;" ??


    Second you are Opening


    fp2=fopen("employee.dat", "r");I take it you are using Pointers ?

    if so can i see the
    #include <stdio.h>
    #include <stdlib.h>Code if you have...

    Where is your Loop to Read the Files,

    Look up Random Access File System, When i get home i can Upload a Bubble Sort Code that will Sort out the Array which is alot easier to search when looking for ID number.

    I have a Random Access Codes at home i will post when i get a chance.


  • Advertisement
  • Registered Users Posts: 2,426 ✭✭✭ressem


    You're retrieving the first and last names using fgets, which keeps the newline (unless you type 15 chars for the firstname and/or lastname).

    So you'll want to check whether the character array ends in a newline + null, and write another null over the newline character.


  • Registered Users Posts: 620 ✭✭✭Laika1986


    ressem wrote: »
    You're retrieving the first and last names using fgets, which keeps the newline (unless you type 15 chars for the firstname and/or lastname).

    So you'll want to check whether the character array ends in a newline + null, and write another null over the newline character.

    Ah yes i thought that might have been the problem. So basically if name[16]=='\n' name[16]=='\0'???in a while i should have said!


  • Registered Users Posts: 620 ✭✭✭Laika1986


    Cork24 wrote: »
    ok just had a look at your Code.

    first off where is your
    "namespace std;" ??


    Second you are Opening


    fp2=fopen("employee.dat", "r");I take it you are using Pointers ?

    if so can i see the
    #include <stdio.h>
    #include <stdlib.h>Code if you have...

    Where is your Loop to Read the Files,

    Look up Random Access File System, When i get home i can Upload a Bubble Sort Code that will Sort out the Array which is alot easier to search when looking for ID number.

    I have a Random Access Codes at home i will post when i get a chance.

    I didnt intend to use pointers but are they necessary?Im not gonna lie either iv never heard of namespace.std, ill search it now and RAC. Is the loop you speak of just to keep re-entering data?Thanks for the help lads


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    RAC is what you use when you need to open, read, write and close a file,

    Random access places the data inside the text file as they are entered so I could have a student list of 1,5,2,7,6 etc un-sorted data file



    You will need a loop that searches each data inside your text file

    So if your search finds a Id number it will then print it out if not it will go to the next section of your program,

    An will allow you to enter data using that ID number


  • Registered Users Posts: 2,426 ✭✭✭ressem


    Laika1986 wrote: »
    Ah yes i thought that might have been the problem. So basically if name[16]=='\n' name[16]=='\0'???in a while i should have said!


    No.
    If you enter 'Super' and enter
    info.firstname[0]='S'
    info.firstname[1]='u'
    info.firstname[2]='p'
    info.firstname[3]='e'
    info.firstname[4]='r'
    info.firstname[5]='\n'
    info.firstname[6]=NULL
    info.firstname[7] through [15] are undefined

    So you'd want to use strlen to get the string length, and work backward checking for and writing null over whitespace characters to trim it.


  • Registered Users Posts: 620 ✭✭✭Laika1986


    Thanks again lads iv improved it a good bit, Im just about ready to start reading into the array now
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 16
    
    
    
    int main()
    {
    
    	FILE *fp, *fp2;
    	fp2=fopen("employee.dat", "r");
    	int j;
    	
    
    
    	typedef struct info{
    		char first_name[MAX];
    		char last_name[MAX];
    		int identification;
    		int hours;
    		float rate;
    		float tax;
    		float gross;
    		float net;
    		}info;
    
    info id;
    		int i;
    //		for(i=0;i<100;i++){
    		
    		printf("\nPlease enter ID Number: ");
    		scanf("%d",&id.identification);
    		fflush(stdin);
    		printf("\nPlease enter First name: ");
    		fgets(id.first_name,MAX,stdin);
    		int count;//intializes count to zero
    		for (count=0; count<MAX; count++){//loops while number is less than max
    			if (id.first_name[count]== '\n'){//if the value is the new line char replaces it with null
    				id.first_name[count]= '\0';
    			}
    		}
    			
    		printf("\nPlease enter Last name: ");
    		fgets(id.last_name,MAX,stdin);
    		printf("\nPlease enter hours worked: ");
    		scanf("%d",&id.hours);
    		printf("\nPlease enter rate of pay: ");
    		scanf("%f",&id.rate);
    		printf("\nPlease enter rate of tax: ");
    		scanf("%f",&id.tax);
    		id.gross= id.hours*id.rate;
    		id.net=id.gross-(id.gross*id.tax/100);
    		
    		
    		fp=fopen("employee.dat","w");
    		
    		
    		fprintf(fp,"ID Number: %d,",id.identification);
    		fprintf(fp,"First Name: %s,",id.first_name);
    		fprintf(fp,"Last Name: %s, ",id.last_name);
    		fprintf(fp,"Hours Worked: %d,",id.hours);
    		fprintf(fp,"Rate of Pay: %.2f,",id.rate);
    		fprintf(fp,"Rate of Tax: %.2f,",id.tax);
    		fprintf(fp,"Gross Pay: %.2f,",id.gross);
    		fprintf(fp,"Net Pay: %.2f,",id.net);
    		
    		fp2=fopen("employee.dat","r");		
    		
    //}		
    		
    return 0;
    }
    


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    Please dont Define Array Size

    Good Programing should Allow the User to Define how many Records he may want to place inside it..

    But for starting off it would be ok, but dont get into that type of Programing.

    Plus why are you doing i <100 your Array is define to 16 ?

    use...

    for (i = 0; i < MAX; i++)


  • Registered Users Posts: 620 ✭✭✭Laika1986


    Yeah i understand what you mean as regards allowing the user to define the amount of records they can enter, I would do this but i dont think ill have time!I have taken that for loop out of it for the time being until i get the basics working properly


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    Try
    #include <iostream>
    #include<string>
    #include<sstream>
    
    #define N_Employee  16
    
    
    using namespace std;
    
    
    int x[N_Employee];
    
    struct  employee
    {
    
    
        int Employee_number;             
        string EmployeeFName;    
         string EmployeeLName;           
        int hours;
    float rate;
                  float tax;
    float gross;
    float net;  
    };
    
    employee record[N_Employee];
    
    
    int main ()
    {    
        
        int n;  
        
        for (n = 0; n < N_Employee; n++)
        {
               while((cout<<"Enter Employee Number: ")&&(!  (cin>>record[n].Employee_number)||record[n].Employee_number<0))
            {
                cout<<"Invalid Input! Please Enter Employee Number "<<endl;
                cin.clear();
                cin.ignore(1000,'\n'); 
            }
            
            cout << "Enter Employee First  Name: ";
            cin >> record[n].EmployeeFName;  
    
       cout << "Enter Employee Last  Name: ";
            cin >> record[n].EmployeeLName;  ;
            
      
            while((cout<<"Enter Hours Worked: ")&&(!(cin>>record[n].hours)||record[n].hours<0))
            {
                cout<<"Invalid Input! Please Enter Hours Worked"<<endl;
                cin.clear();
                cin.ignore(1000,'\n'); 
            }
            
    
            while((cout<<"Enter Rate: ")&&(!(cin>>record[n].rate)||record[n].rate<0))
            {
                cout<<"Invalid Input! Please Enter Rate"<<endl;
                cin.clear();
                cin.ignore(1000,'\n'); 
            }
    
    
    
            while((cout<<"Enter Tax: ")&&(!(cin>>record[n].tax)||record[n].tax<0))
             {
                 cout<<"Invalid Input! Please Enter Tax"<<endl;
                 cin.clear();
                 cin.ignore(1000,'\n'); 
             }
    
    
            while((cout<<"Enter gross: ")&&(!(cin>>record[n].gross)||record[n].gross<0))
             {
                 cout<<"Invalid Input! Please Enter Gross"<<endl;
                 cin.clear();
                 cin.ignore(1000,'\n'); 
             }
    
    
    
    
            while((cout<<"Enter Net: ")&&(!(cin>>record[n].net)||record[n].net<0))
             {
                 cout<<"Invalid Input! Please Enter Net"<<endl;
                 cin.clear();
                 cin.ignore(1000,'\n'); 
             }
    
    
    

    This should Loop Over 16 Times and then you can


    Read in the Array
     for(int i=0; i<N_Employee; i++)
                   {
                       cout << "Employee Number: " <<record[i].employee_number << endl;   
     
    
                       cout << " " << record[i]. << endl;         
    
    
    etc....
    
    
                   }
    
    
    
    
    


  • Registered Users Posts: 620 ✭✭✭Laika1986


    Thanks for the help but is that C++?? it's useful though


  • Advertisement
  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    You doing c# or c++


  • Registered Users Posts: 2,013 ✭✭✭SirLemonhead


    The code in his/her original post is C...

    I think the people trying to help are just as confused as the OP is :pac:

    Do you need to store this data in a .dat file as text, or can it be binary data? ie if someone opens it in Notepad, should they see human readable understandable text?

    Edit: Sorry I see, it's a plain text file..


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    Im sorry i was using my iphone,

    But OP C & C++ are more or less the same, its just C++ had added Lib,


  • Registered Users Posts: 2,426 ✭✭✭ressem


    Cork24 wrote: »
    Im sorry i was using my iphone,

    But OP C & C++ are more or less the same, its just C++ had added Lib,

    Sorry Cork, that's not accurate and since Laika's in learning mode I'm going to be pedantic. Most of c is a subset of c++, and most of us can use a c++ compiler to compile our c code for x86 without hassle.
    But a dedicated c compiler will not process c++ even if the libraries are available. C has no call-by-reference for example.

    The OP has been given a C project, then s/he can't even count on the Gnu C getline() function mentioned by Kidchameleon as a good alternative to scanf / fgets (or all the other non-standard alternatives like scanf_s).
    A PITA when we're starting to learn C, and every tutorial says not to use this and that unsafe functions in real code due to buffer overflow concerns.


  • Registered Users Posts: 620 ✭✭✭Laika1986


    Well guys to clear up some confusion yes indeed I am doing C.

    @Cork24 I do appreciate that code although its in C++ like i said I can see the logic in it to a certain extent.

    @Everyone! Thanks for your help and suggestions, I can only guess how frustrating it is to deal with noobs like myself but we all know it's no-ones fault really!


  • Registered Users Posts: 620 ✭✭✭Laika1986


    Hey Guys. New problem, at the lower section of my code i have a type fo search function so when the user enters a number it prints the relevant details for that ID number. If the number does not exist it produces and error. However the array seems to be empty. I had this working earlier but now im at a loss to what i changed that breaks it, any help as always would be great
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 16
    
    
    
    int main()
    {
    
    	FILE *fp, *fp2;
    	
    	fp=fopen("employee.dat","a");
    	
    	
    
    
    	typedef struct info{
    		char first_name[MAX];
    		char last_name[MAX];
    		int identification;
    		int hours;
    		float rate;
    		float tax;
    		float gross;
    		float net;
    		}info;
    
    info id;
    info array[100];
    		
    		
    		
    		
    		
    
    		
    		printf("\nPlease enter ID Number: ");
    		scanf("%d",&id.identification);
    		fflush(stdin);
    		
    			if (id.identification==array[100].identification){
    		
    					printf("Error: User already exists");
    					
    				}
    			
    		printf("\nPlease enter First name: ");
    		fgets(id.first_name,MAX,stdin);
    		
    		int count;//intializes count to zero
    		for (count=0; count<MAX; count++){//loops while number is less than max
    			if (id.first_name[count]== '\n'){//if the value is the new line char replaces it with null
    				id.first_name[count]= '\0';
    			}
    		}
    			
    		printf("\nPlease enter Last name: ");
    		fgets(id.last_name,MAX,stdin);
    		
    		for (count=0; count<MAX; count++){//loops while number is less than max
    			if (id.last_name[count]== '\n'){//if the value is the new line char replaces it with null
    				id.last_name[count]= '\0';
    			}
    		}
    		
    		printf("\nPlease enter hours worked: ");
    		scanf("%d",&id.hours);
    		printf("\nPlease enter rate of pay: ");
    		scanf("%f",&id.rate);
    		printf("\nPlease enter rate of tax: ");
    		scanf("%f",&id.tax);
    		id.gross= id.hours*id.rate;
    		id.net=id.gross-(id.gross*id.tax/100);
    		
    		
    		
    		
    		fprintf(fp,"ID Number: %d,",id.identification);
    		fprintf(fp,"First Name: %s,",id.first_name);
    		fprintf(fp,"Last Name: %s, ",id.last_name);
    		fprintf(fp,"Hours Worked: %d,",id.hours);
    		fprintf(fp,"Rate of Pay: %.2f,",id.rate);
    		fprintf(fp,"Rate of Tax: %.2f,",id.tax);
    		fprintf(fp,"Gross Pay: %.2f,",id.gross);
    		fprintf(fp,"Net Pay: %.2f,\n",id.net);
    		
    		
    	
    		int idnum,i;	
    		printf("Please enter ID Number: ");
    		scanf("%d",&idnum);
    		
    		for(i=0;i<=100;i++){
    		
    			if (idnum==array[i].identification){
    				
    				printf("%s ",id.first_name);
    				printf("%s\n",id.last_name);
    				printf("Hours: %d\n",id.hours);
    				printf("Pay: %.2f\n",id.rate);
    				printf("Tax: %.2f\n",id.tax);
    				printf("Gross Pay: %.2f\n",id.gross);
    				printf("Net Pay: %.2f\n",id.net);
    				}
    				
    			else if(idnum!=array[i].identification){
    				printf("Error: User does not exist");
    				break;
    				}
    			}
    				
    				
    		
    		
    	
    		
    return 0;
    }
    


  • Registered Users Posts: 2,426 ✭✭✭ressem


    Course not.
    At no point do you put anything into the array.

    What tools are you using to write and debug the code? Most will support watching variables and arrays as they change when you step through the code in debug mode one line at a time.
    Then it's much quicker to find what you're missing, and when your code does not behave as you're expecting.

    You'll have to learn this, perhaps from a TA, a student colleague, your notes, the help file of the program or a youtube video.

    Laika1986 wrote: »
    Hey Guys. New problem, at the lower section of my code i have a type fo search function so when the user enters a number it prints the relevant details for that ID number. If the number does not exist it produces and error. However the array seems to be empty. I had this working earlier but now im at a loss to what i changed that breaks it, any help as always would be great
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 16
    
    
    
    int main()
    {
    
    	FILE *fp, *fp2;
    	
    	fp=fopen("employee.dat","a");
    	
    	
    
    
    	typedef struct info{
    		char first_name[MAX];
    		char last_name[MAX];
    		int identification;
    		int hours;
    		float rate;
    		float tax;
    		float gross;
    		float net;
    		}info;
    
    info id;
    info array[100];
    		
    		
    		
    		
    		
    
    		
    		printf("\nPlease enter ID Number: ");
    		scanf("%d",&id.identification);
    		fflush(stdin);
    		
    			if (id.identification==array[100].identification){
    		
    					printf("Error: User already exists");
    					
    				}
    			
    		printf("\nPlease enter First name: ");
    		fgets(id.first_name,MAX,stdin);
    		
    		int count;//intializes count to zero
    		for (count=0; count<MAX; count++){//loops while number is less than max
    			if (id.first_name[count]== '\n'){//if the value is the new line char replaces it with null
    				id.first_name[count]= '\0';
    			}
    		}
    			
    		printf("\nPlease enter Last name: ");
    		fgets(id.last_name,MAX,stdin);
    		
    		for (count=0; count<MAX; count++){//loops while number is less than max
    			if (id.last_name[count]== '\n'){//if the value is the new line char replaces it with null
    				id.last_name[count]= '\0';
    			}
    		}
    		
    		printf("\nPlease enter hours worked: ");
    		scanf("%d",&id.hours);
    		printf("\nPlease enter rate of pay: ");
    		scanf("%f",&id.rate);
    		printf("\nPlease enter rate of tax: ");
    		scanf("%f",&id.tax);
    		id.gross= id.hours*id.rate;
    		id.net=id.gross-(id.gross*id.tax/100);
    		
    		
    		
    		
    		fprintf(fp,"ID Number: %d,",id.identification);
    		fprintf(fp,"First Name: %s,",id.first_name);
    		fprintf(fp,"Last Name: %s, ",id.last_name);
    		fprintf(fp,"Hours Worked: %d,",id.hours);
    		fprintf(fp,"Rate of Pay: %.2f,",id.rate);
    		fprintf(fp,"Rate of Tax: %.2f,",id.tax);
    		fprintf(fp,"Gross Pay: %.2f,",id.gross);
    		fprintf(fp,"Net Pay: %.2f,\n",id.net);
    		
    		
    	
    		int idnum,i;	
    		printf("Please enter ID Number: ");
    		scanf("%d",&idnum);
    		
    		for(i=0;i<=100;i++){
    		
    			if (idnum==array[i].identification){
    				
    				printf("%s ",id.first_name);
    				printf("%s\n",id.last_name);
    				printf("Hours: %d\n",id.hours);
    				printf("Pay: %.2f\n",id.rate);
    				printf("Tax: %.2f\n",id.tax);
    				printf("Gross Pay: %.2f\n",id.gross);
    				printf("Net Pay: %.2f\n",id.net);
    				}
    				
    			else if(idnum!=array[i].identification){
    				printf("Error: User does not exist");
    				break;
    				}
    			}
    				
    				
    		
    		
    	
    		
    return 0;
    }
    


Advertisement