Thread: Huge number output

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    1

    Huge number output

    Hello,

    I have an assignment to make which i have finished most of except that i have a bug (or so i think)

    The program works like a database where you can add, edit, delete persons and display their info that you add.

    3 different files: main.c, final.c, header.h

    the final.c file contains all the functions, i used the first one to add a person and save the details to a binary file. and function 5 to display the details i entered in a list, the only problem is that the year of birth and salary outputs are always huge (for example 423547) even if i input a small number such as 100.

    Could anyone help me find the problem? thank you

    FINAL.C
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "header.h"
    
    /* ---------------------------------- 1. ADD + SAVE FUCNTION -------------------------- */
    
    int add(PERSON *p) {
        
        i = count;
        count++;
                
        system("cls");
        printf("New Entry\n");
        printf("---------\n");
        
        printf("\nFirst Name: ");
    	scanf("%s", &p[i].fname);
    	
    	printf("\nLast Name: ");
    	scanf("%s", &p[i].lname);
    	
    	printf("\nYear of birth: ");
    	scanf("%d", &p[i].year);
    	
    	printf("\nNationality: ");
    	scanf("%s", &p[i].nation);
        						
    	printf("\nProfession: ");
    	scanf("%s", &p[i].prof);	
        				
    	printf("\nSalary: ");
    	scanf("%d", &p[i].salary);
    	
    	/* below the given data gets saved into data.bin */
    	
    	FILE *f;									
    	f = fopen("data.bin", "wb");				
    		if(f != NULL) {
    		fwrite(p, sizeof(PERSON), 100, f);
    	} else
            printf("Can't write the file...");
    	fclose(f);		
    	printf("\nData has been saved!\n");
    	system("PAUSE");
    	getchar();
    	return 0;	
    }
    	
    /* ---------------------------------- 2. EDIT FUNCTION -------------------------- */
    
    int edit(PERSON *p){
    	system("cls");
    	printf("Enter the number of the person whos data you want to edit: ");
    	i = 0;
    	while (i < count){	
    		printf("\n%1d %4s %8s\n", i+1, p[i].fname, p[i].lname);
    		i++;
    	}
    	scanf("%d", &ch);	
    	ch--;
    	system("cls");
    	printf("Enter new person data\n\n");
    	printf("\nFirst Name: ");
    	scanf("%s", &p[ch].fname);	
    	printf("\nLast Name: ");
    	scanf("%s", &p[ch].lname);						
    	printf("\nYear of birth: ");
    	scanf("%d", &p[ch].year);					
    	printf("\nNationality: ");
    	scanf("%s", &p[ch].nation);						
    	printf("\nProfession: ");
    	scanf("%s", &p[ch].prof);					
    	printf("\nSalary: ");
    	scanf("%d", &p[ch].salary);
    	printf("\nData was changed. Press enter to continue...");
    	getchar();
    	getchar();
    	return 0;
    		
    	}
    	
    /* ---------------------------------- 3. DELETE FUNCTION -------------------------- */
    
    int del(PERSON *p){
    	system("cls");
        printf("Enter the number of the person you'd like to delete: \n");
    	i = 0;
    	while (i < count){									
    		printf("%1d %4s %8s\n", i+1, p[i].fname, p[i].lname);
    		i++;
    	}
    	scanf("%d", &ch);				
    	ch--;
    	while (ch < count){		
    		p[ch] = p[ch+1];		
    		ch++;		
    		}
    	count--;
    	printf("\n\nData was deleted. press enter to continue...");
    	getchar();
    	getchar();
    	return 0;
    }
    
    /* ---------------------------------- 4. STATISTICS FUNCTION -------------------------- */
    
    int stat(PERSON *p){
    	i = 0;
    	while (i < count-1){		
    		if (p[i].year > p[i+1].year){	
    			p[99] = p[i];		
    			p[i] = p[i+1];
    			p[i+1] = p[99];
    			i = 0;
    		}
    		if (p[i].year <= p[i+1].year)
    			i++;
    		}
    	system("cls");
    	printf("-------------------------Statistics---------------------------------\n");
    	printf("\nOldest person: %s %s \n", p[count-1].fname, p[count-1].lname);
    	printf("\nYoungets person: %s %s \n", p[0].fname, p[0].lname);
    
        /* loop to find average year of birth */
    	i = 0;
    	ch = 0;
    	while (i < count){			
    		ch = ch + p[i].year;
    		i++;
    	}	
    	printf("\nAverage year of birth: %d\n", ch / count-1);
    	
    	/* loop to find average salary */
    	i = 0;
    	ch = 0;
    	while (i < count){	
    		ch = ch + p[i].salary;
    		i++;
    	}	
    	printf("\nThe average income is: %d\n", ch / count-1);
    	printf("\nPress enter to continue...");
    	getchar();
    	getchar();
    	return 0;
    }
    	
    	
    /* ---------------------------------- 5. DISPLAY FUNCTION -------------------------- */
    
    int disp(PERSON *p){
    	system("cls");
    	printf("# First name | Last name | Year of Birth | Nationality | Profession | Salary\n");
    	printf("----------------------------------------------------------------------------\n\n");
    	i = 0;
    	while (i < count){
    	printf("%1d %s  %s   %d     %s   %s %d\n", i+1, p[i].fname, p[i].lname, p[i].year, p[i].nation, p[i].prof, p[i].salary);
    		i++;
    	}
    	system("PAUSE");
    	getchar();
    	return 0;
    }
    
    /* ---------------------------------- 6. DOCUMENTATION -------------------------- */
    
    int doc(PERSON *p){
        system("cls");
        printf("-------------- Documentation ------------------\n\n");
        printf("User Guide: In the main menu, select a service by\n");
        printf("typing its number then pressing enter.\n\n");
        
        printf("Types of data structures used are 'int' and 'char'\n");
        printf("integer part used for year of birth and salary data\n");
        printf("while char is used for the rest.\n\n");
        
        printf("5 Different functions are availible for usage:\n");
        printf("To add, edit, delete, persons data and to display\n");
        printf("statistics (such as average salary) or personal data\n");
        printf("of each person in a list.\n\n");
        
        printf("All declarations are inside the header.h file and all\n");
        printf("the function bodies are inside final.c including this\n");
        printf("documentation!\n\n");
        
        system("PAUSE");
    	getchar();
    	return 0;
    	}
    MAIN.C
    #include <stdio.h>
    #include <stdlib.h>
    #include "header.h"

    int main(int argc, char *argv[]) {
    do {

    system("cls");
    printf("-----------Main Menu-----------\n");
    printf("1. Add new person\n");
    printf("2. Edit persons data\n");
    printf("3. Delete person\n");
    printf("4. Show statistics\n");
    printf("5. Display personal data\n");
    printf("6. Documentation\n");
    scanf("%d", &choice);

    switch (choice) {

    case 1: add(p);
    break;

    case 2: edit(p);
    break;

    case 3: del(p);
    break;

    case 4: stat(p);
    break;

    case 5: disp(p);
    break;

    case 6: doc(p);
    break;

    default: printf("Invalid Choice!\n");
    break;

    }
    } while(choice != 7);

    system("PAUSE");
    return 0;
    }
    HEADER.H
    Code:
    typedef struct {  /* data in struct, incl. variables that will be used /w pointer */
            char fname[20];
            char lname[20];
            char nation[20];
            char prof[20];
            int year[20];
            int salary[20];
            } PERSON;  /* struct name */
            
    PERSON p[100]; /* pointer declared to work with struct PERSON (p) */
    
    /* declaring functions */
    int add(PERSON*);
    int edit(PERSON*);
    int del(PERSON*);
    int stat(PERSON*);
    int disp(PERSON*);
    int doc(PERSON*);
    
    /* other declarations */
    
    int choice;
    int count;
    int i;
    int ch;
    int count;

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    year and salary are integer's, you probably don't want them to be arrays of integers in your PERSON struct, rather just a single integer instead.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Besides changing these data types to integers, this seems wrong:

    Code:
    fwrite(p, sizeof(PERSON), 100, f);
    You are writing out 1 record, not 100 records, at a time.

    It's just style, but nevertheless important.

    Code:
    if(someCondition is true)
       do something;
    else
       do something different;
    
    //Above looks great, and shows which are the sub-dominant lines of code that may not //be executed. 
    
    if(someCondition is true)
       do something;
    else
    do something different;
    another line of code;
    another line of code;
    yet another line of code;
    
    The second if statement misleads our eyes to believe that all the lines of code in blue, are the same, since they have the same indentation, and are grouped together. That's bad style, and an error "waiting to happen". I would run, not walk, away from this style of writing code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 12:06 PM
  3. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  4. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  5. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM