Thread: Problem with print out the result

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    8

    Post Problem with print out the result

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #define N 2
    #define Q 4
    
    typedef struct{
        char ID[10], s_name[50], gender, IC[11], date[20];
        double sales[Q], total_s;
    }SALES_OF_SALESMAN;
    SALES_OF_SALESMAN D1[N];
    SALES_OF_SALESMAN NN;
    
    int AddSalesman(void);
    
    int main(void){
        double total_s[S]={0};
        int row=0, col=0, a=0;
    
    
        FILE * fptr, * fpt;
    
    
        fpt = fopen("Salesman.txt", "r");
        fptr = fopen("Sales.txt", "w");
    
    
        if(!fpt){
            printf("Cannot open Salesman.txt!\n");
            exit(-1);
        }
    
    
        while(!feof(fpt))
        {
            fscanf(fpt, "%[^|]|%[^|]|%[^|]|%[^|]|%[^\n]\n", &D1[row].ID1, &D1[row].s_name, &D1[row].gender, &D1[row].IC, &D1[row].date);
            row++;
        }
    
    
        for(row=0; row < S; row++)
        {
            printf("Salesman's ID   : %s\n", D[row].ID);
            printf("Salesman's name : %s\n", D[row].s_name);
            fprintf(fptr, "%s", D1[row].ID);
            fflush(stdin);
            for(col=0; col < Q; col++)
            {
                printf("Salesman's quanterly sales : ");
                scanf("%lf", &D1[row].sales[col]);
                while(D1[row].sales[col]<0){
                    printf("You enter incorrectly...Please enter again : ");
                    scanf("%lf", &NN.sales[col]);
                }
                D1[row].total_s += D1[row].sales[col];
                fprintf(fptr, "|%.2lf", D1[row].sales[col]);
            }
            fprintf(fptr,"\n");
        }
    
    
        mainmenu();
    
    
        fclose(fpt);
        fclose(fptr);
        
        return 0;
    
    int AddSalesman(void)
    {
        int row, col;
        FILE * fpt, * fptr;
    
    
        fpt = fopen("Salesman.txt", "a");
        fptr = fopen("Sales.txt", "a");
    
    
        if(!fpt){
            printf("Cannot open Salesman.txt!\n");
            exit(-1);
        }
    
    
        if(!fptr){
            printf("Cannot open Sales.txt!\n");
            exit(-1);
        }
    
    
        printf("-Please enter-\n");
        printf("New Salesman's ID : ");
        scanf("%s", &NN.ID);
        fflush(stdin);
        printf("New Salesman's name : ");
        scanf("%[^\n]", &NN.s_name);
        fflush(stdin);
        printf("New Salesman's gender (F/M)? : ");
        scanf("%c", &NN.gender);
        while(NN.gender != 'M' && NN.gender != 'm' && NN.gender != 'F' && NN.gender != 'f'){
            fflush(stdin);
            printf("You enter incorrectly...Please enter again : ");
            scanf("%c", &NN.gender);
        }
        NN.gender=toupper(NN.gender);
        printf("New Salesman's IC number (xxxxxx-xx-xxxx) : ");
        fflush(stdin);
        scanf("%s", &NN.IC);
    
    
        printf("New Salesman's date join : ");
        scanf("%s", &NN.date);
        fprintf(fpt, "%s|%s|%c|%s|%s\n",NN.ID, NN.s_name, NN.gender, NN.IC, NN.date);
        fclose(fpt);
    
    
        for(col=0; col < Q; col++)
        {
            printf("New Salesman's quanterly sales : ");
            scanf("%lf", &NN.sales[col]);
            while(NN.sales[col]<0){
                printf("You enter incorrectly...Please enter again : ");
                scanf("%lf", &NN.sales[col]);
            }
        }
    
    
        fprintf(fptr, "%s", NN.ID);
        for(col=0; col < Q; col++)
            fprintf(fptr, "|.2lf", NN.sales[col]);
        fprintf(fptr, "\n");
        fclose(fptr);
    
    
        system("pause");
        return 0;
    }
    
    }
    In AddSalesman(), why I can print out result in salesman.txt but cannot print out the result in sales.txt?
    Problem with print out the result-p-png
    Attached Files Attached Files
    Last edited by pp00; 11-13-2012 at 12:08 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Look closely to line #132, what do you see?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    fprintf(fptr, "|.2lf", NN.sales[col]);
    that one got problem?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by pp00 View Post
    fprintf(fptr, "|.2lf", NN.sales[col]);
    that one got problem?
    Yes, and it's the "|.2lf" part. What should it be for fprintf to print?
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    emm...actually I wanted to print it out in sales.txt...

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by pp00 View Post
    emm...actually I wanted to print it out in sales.txt...
    Come on, what special symbol is missing? It's like O/o, but different...
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    emm...sorry...I don't really get it...

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by pp00 View Post
    emm...sorry...I don't really get it...
    Ok, no more fooling around

    "|.2lf" should be "|%.2lf"
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    but it still the same..the result still no print out in the sales.txt
    Problem with print out the result-p1-png

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Given that the code won't even compile because you've nested functions, it shouldn't be producing output at all. Try forcing a complete rebuild (eg by deleting the object files and executable). Your IDE or build script is probably confused somehow, and you are running the executable corresponding to an old version of code.

    Apart from the problem that GReaper pointed out, it is a bad idea to use fflush(stdin) - fflush(stdin) yields undefined behaviour, and is not guaranteed to (say) clear pending input from stdin.
    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
    Nov 2012
    Posts
    8
    emm...still cannot...

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Have you tried to follow my recommendations?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I test for a result, but any result over 1 fails
    By jeremy duncan in forum C++ Programming
    Replies: 10
    Last Post: 05-23-2012, 01:23 PM
  2. Correct algorithm, wrong result (cout problem?)
    By dember in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2009, 06:03 PM
  3. Why i can get the result print to the screen
    By wise_ron in forum C Programming
    Replies: 7
    Last Post: 05-22-2006, 08:06 PM
  4. Replies: 4
    Last Post: 04-24-2004, 04:29 PM
  5. problem getting result when calling a function
    By drb2k2 in forum Windows Programming
    Replies: 1
    Last Post: 04-14-2003, 09:51 AM