Thread: I need help with this program

  1. #1
    Registered User
    Join Date
    Dec 2013
    Location
    Trinidad
    Posts
    6

    Red face I need help with this program

    This is a little practice program I wrote in my spare time:

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<string.h>
    #include<time.h>
    
    
    struct work{
        char fname[10][10];
        char lname[10][10];
        char id[10][10];
        float sal[10];
    };
    
    
    
    
    
    
    FILE*comp;
    void fcheck(),fupdate(),menu(),file(int);
    int accept();
    time_t t;
    
    
    
    
    
    
    
    
    int main(){        
    menu(); 
    getchar();
    }
    
    
    
    
    
    
    
    
    void menu(){
        printf("Welcome to Test Database. Please select an option below:\n\t1-Enter records\n\t2-Update records\n\t3-View Records\n\t4-Erase records\n\t5-Exit\n");
    }
    
    
    int accept(){//stores option number
        int n;
        scanf("%d",&n);
        getchar();
        return n;
    }
    
    
    
    
    void file(int x){//accept option number
        struct work work;
        time(&t);
        int a;
            if(x==1){
                comp = fopen("C:\\Dur\\company.txt","a+");
                //fcheck();
                for(a=0;a<=9;a++){
                    printf("First name:\t");
                    fgets(work.fname[a],10,stdin);
                    work.fname[a][strlen(work.fname[a])-1]='\0';
                    printf("Last name:\t");
                    fgets(work.lname[a],10,stdin);
                    work.lname[a][strlen(work.lname[a])-1]='\0';   
                    printf("ID:\t");
                    fgets(work.id[a],10,stdin);
                    work.id[a][strlen(work.id[a])-1]='\0';
                    printf("Salary:\t");
                    scanf("%f",&work.sal[a]);
                    getchar();
                    system("cls");
                    fprintf(comp,"%s:\t",work.fname[a]);
                    fprintf(comp,"%s:\t",work.lname[a]);
                    fprintf(comp,"%s\t",work.id[a]);
                    fprintf(comp,"%.2f:\t\n",(float)work.sal[a]);
                }
                fprintf(comp,"\n\nLast Data Entry/Update:\t",ctime(&t));
                fclose(comp);
            }else if(x==2){
                comp = fopen("C:\\Dur\\company.txt","a+");
                fcheck();
                fupdate();
                fclose(comp);
            }else if(x==3){
                char fnamev[10][10];
                char lnamev[10][10];
                char idv[10][10];
                float salv;
                while(!feof(comp)){
                    int z = 0;
                    fscanf(comp,"%s %s %s %f",fnamev[z],lnamev[z],idv[z]);
                    printf("%s %s %s %.2f\n",fnamev[z],lnamev[z],idv[z]);
                    z++;
                }
                fclose(comp);
            }else if(x==4){
                comp = fopen("C:\\Dur\\company.txt","w");
                printf("File successfully erased");
                fclose(comp);
            }else if(x==5){
                exit(0);
            }
    }
    
    
    
    
    
    
    
    
                
    void fcheck(){
        if(comp==NULL){
            printf("File Read Error\n");
        }else{
            printf("File opened successfully\n");
            sleep(2000);
            system("cls");
        }
    }
    
    
    
    
    
    
    
    
    
    
    void fupdate(){
        time(&t);
        char fnameu[10];
        char lnameu[10];
        char idu[10];
        float salu;
        comp = fopen("C:\\Dur\\company.txt ","a");
        fcheck();
        printf("First name:\t");
        fgets(fnameu,10,stdin);
        fnameu[strlen(fnameu)-1]='\0';
        printf("Last name:\t");
        fgets(lnameu,10,stdin);
        lnameu[strlen(lnameu)-1]='\0';
        printf("ID:\t");
        fgets(idu,10,stdin);
        idu[strlen(idu)-1]='\0';
        printf("Salary:\t");
        scanf("%f",&salu);
        getchar();
        fprintf(comp,"%s:\t",fnameu);
        fprintf(comp,"%s:\t",lnameu);
        fprintf(comp,"%s\t",idu);
        fprintf(comp,"%.2f:\t\n",(float)salu);
        fprintf(comp,"\n\nLast Data Entry/Update:\t",ctime(&t));
        fclose(comp);   
         
    }
    It's supposed to be a small database...i'm in the process of learning structs so I decided to try and write a program incorporating it. Problem is that when I enter a number corresponding to an option that shows up on the screen, the program closes off and I can't figure out why it's doing it. The program compiles so I know it's a logic error and not a syntax for sure.

    Any help would be greatly appreciated...Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps you should call some more functions.

    All that happens at the moment is main() calls menu(), which does nothing more than print a string, and then waits for a single character of input (could be anything).

    Nothing else gets called, so nothing else can possibly happen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Location
    Trinidad
    Posts
    6
    Which ones do you suggest I call and where???

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by Ronaldo95163 View Post
    Which ones do you suggest I call and where???
    That's strange.
    Because someone capable of mixing fgets() and scanf() successfully ought to have figured that bit out for themselves long ago.

    Further, if you did actually write it, you must have had some idea of how accept() was going to be used, otherwise you would never have figured out it was supposed to return an int.
    If you had the idea for accept, you would also have the idea of how to use it, and more importantly, where to use it.

    I suggest you look again, this should come second nature to you.
    Just giving you the answer won't help you long term.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Ronaldo95163 View Post
    This is a little practice program I wrote in my spare time:
    Quote Originally Posted by Ronaldo95163 View Post
    Which ones do you suggest I call and where???
    I find it hard to understand how you had the wherewithal to write a program like the one above, yet don't understand what functions to call.

    What seems more plausible to me is that you have been given a homework assignment, and want some help (i.e., you want someone else to do your work / you want the easiest possible way of making the grade).

    Homework is for your benefit. If you want to actually understand and remember subject material, it takes practice; to that end, homework is assigned. Please, do yourself a favour, and try harder. You will be glad you did.

    In the meantime, I would refer you to the following:


  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you went to a lot of trouble to write all those other functions; perhaps you could consider calling them. You will have to actually pay attention to the input you receive (at the moment you just ignore it), and perhaps you could make a decision based on that input, which function to call.

  7. #7
    Registered User
    Join Date
    Dec 2013
    Location
    Trinidad
    Posts
    6
    It's not a HW assignment.
    Reason I asked which should I call is that i've tried calling them but they don't seem to work...even if I allow file to be called in main and allow it to just print a simple statement it still doesn't work...only fupdate and menu works properly...

    I need help with this program-work-jpg

    Before this program I wrote the solution to an actual problem and I used a function with it and it worked correctly:

    Code:
    #include<stdio.h>#include<stdlib.h>
    int next(int),n;
    int main(){
        printf("Enter a Number: \t");
        scanf("%d",&n);
        getchar();
        int x = next(n);
        printf("Result: \t\t%d",x);
        getchar();
        return 0;
    }
    int next(int a){
        int x = 1;
    if( (a>=0) && (a<=10000) ){    
        if(a>=0 && a<10){
            return a+1;
        }else if(a>=10 && a<=98){
            while(x=1){
                a++;
                int b=a/10;
                int c=a%10;
                if(a!=b){
                    return a;
                    break;
                }
            }
        }else if( (a>=987) && (a<=9876) ){
            while(x=1){
                a++;
                int w = a/1000;
                int x = (a/100)%10;
                int y = (a%100)/10;
                int z = a%10;
                if( (w!=z) && (x!=y) && (w!=x) && (y!=z) && (w!=y) && (x!=z) ){
                    return a;
                    break; 
                }
            }
        }else if( (a>9876) && (a<=10000) ){
            while(x=1){
                a++;
                int b = a/10000;
                int c = (a/1000)%10;
                int d = (a%1000)/100;
                int e = (a%100)/10;
                int f = a%10;
                if( (b!=c) && (b!=d) && (b!=e) && (b!=f) && (c!=d) && (c!=e) && (c!=f) && (d!=e) && (d!=f) && (e!=f) ){
                    return a;
                    break;
                }
            }
        }else if( (a>98) && (a<=987) ){
            while(x=1){
                a++;
                int b = a/100;
                int c = (a%100)/10;
                int d = a%10;
                if( (b!=d) && (b!=c) && (c!=d) ){
                    return a;
                    break;    
                        }
                    }
                }
            }else{
                printf("Number out of range");
                sleep(1000);
                exit(0);
            }
        }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Reason I asked which should I call is that i've tried calling them but they don't seem to work
    So post what you tried then.

    Because I just added 1 line of code to your first post, and some of it seems to work at least.

    One of your fscanf calls is however broken.
    Code:
    $ gcc foo.c
    foo.c: In function ‘file’:
    foo.c:99:17: warning: format ‘%f’ expects a matching ‘float *’ argument [-Wformat]
    foo.c:100:17: warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I write functions all the time that I have no idea how to call. I design them very carefully, with a nice top-down design (sometimes combination of bottom-up and top-down) and then get lost and have no idea how they all fit together. *shrug*

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Hodor View Post
    I write functions all the time that I have no idea how to call. I design them very carefully, with a nice top-down design (sometimes combination of bottom-up and top-down) and then get lost and have no idea how they all fit together. *shrug*

    Yeah, if your main() calls menu() followed by getchar(), and menu() only contains a single call of printf(), you would not expect any functions other than menu(), printf(), or getchar() to magically be called. That is what the first post in this thread involved.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Dec 2013
    Location
    Trinidad
    Posts
    6
    I need help with this program-datab-jpg
    ^Sorry for the late reply but I figured it out...just need to fix option 3

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Please post code in future, not fuzzy bandwidth sapping images.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    As an aside, Ronaldo95163, there is a new (forked) version of Dev-C++. You can find it here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  2. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  3. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM