Thread: Problem if a code

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    2

    Question Problem if a code

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void) {
    typedef struct {
    char nome;
    int valor;
    } maismat;
    char n;
    int total, hr, min, sts;
    maismat *mais;
    int *dia;
    int *op;
    int ct = 0, oper1 = 0, oper2 = 0, oper3 = 0, i = 0, b = 0, a = 1;
    FILE *arq1;
    FILE *arq2;
    
    arq1 = fopen("Maq1.log", "r+"); //open file
    if (arq1 == NULL) {
        printf("Arquivo nao foi aberto corretamente.");
        return 0;
    }
    else {
        printf("Arquivo aberto com sucesso, alocando memoria... \n");
    }
    while (!feof(arq1)) {
        n = fgetc(arq1);
        if (n == '\n') { //count lines
            ct++;
            }
        }
    dia = (int*)malloc(ct*(sizeof(int))); //alloc memory
    if(dia==NULL) {
        printf("Não houve memoria alocada!\n");
        return 0;
    }
    op = (int*)malloc(ct*(sizeof(int))); //alloc memory
    if(op==NULL) {
        printf("Não houve memoria alocada!\n");
        return 0;
        }
    mais = malloc(ct*(sizeof(maismat)));
        if(mais == NULL){
        printf("Não houve memoria alocada!\n");
        return 0;
        }
    else {
        printf("Memoria alocada corretamente, contando.. \n");
        }
    
    fclose(arq1); //flush buffer and close file 1
    
    arq2 = fopen("Maq1.log", "r+"); //open file again to read data
    if (arq2 == NULL) {
        printf("Arquivo não foi aberto corretamente... \n");
    }
    else {
        printf("Arquivo aberto corretamente, gravando dados... \n");
    }
    
    for(i=0;!feof(arq2);i++) {
        fscanf(arq2, "%d,%d,%d,%d,%d", &dia[i],&hr,&min,&op[i],&sts); //Get data
    }
    
    for(b=0;b<=ct;b++) { //do the count
        if (dia[b] = a) {
            if(op[b] == 1){
            oper1++;
            }
        else if(op[b] == 2){
            oper2++;
            }
        else if(op[b] == 3){
            oper3++;
            }
        }
       else {
        a++;
            if (oper1 >= oper2 && oper1 >= oper3) { //print the answer and record data
                printf("No dia %d, uma das materia primas alocadas foi a Materia Prima 1 em %d operacoes \n", dia[b], oper1);
                mais->nome = dia[b];
                mais->valor = oper1;
            }
            else if (oper2 >= oper3 && oper2 >= oper1) {
            printf("No dia %d, uma das materia primas alocadas foi a Materia Prima 2 em %d operacoes\n", dia[b], oper2);
               mais->nome = dia[b];
                mais->valor = oper2;
            }
            else if (oper3 >= oper1 && oper3 >= oper2) {
            printf("No dia %d, uma das materia primas alocadas foi a Materia Prima 3 em %d operacoes\n", dia[b], oper3);
               mais->nome = dia[b];
                mais->valor = oper3;
            }
        }
    }
        fclose(arq2); //flush buffer and close file 2
        return 0;
    }
    The code doesn't give me any warn when I compile him, but stop until print the answer on the screen

    Some friends told me to debbug the program, how can I do that? I'm using code::blocks on Linux.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You need to make sure your code is consistently formatted and indented.

    Quote Originally Posted by honor3us View Post
    The code doesn't give me any warn when I compile him, but stop until print the answer on the screen
    You need to increase your compiler warnings then.

    Code:
    /*
    main.c||In function 'main':
    main.c|66|warning: suggest parentheses around assignment used as truth value
    main.c|10|warning: unused variable 'total'
    ||=== Build finished: 0 errors, 2 warnings ===|
    */
    The first one needs to be fixed.

    Also, it would help if you posted a sample of the file you're reading from.



    Code:
    int *dia;
    
    // ...
    
    dia = (int*)malloc(ct*(sizeof(int)));
    
    // ...
    
    for(b=0;b<=ct;b++) { //do the count
        if (dia[b] = a) {
            // ...
    You're accessing memory you don't own. That should be "b < ct".

    Don't use feof to control a loop: FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    Don't cast the return value of malloc: FAQ > Casting malloc - Cprogramming.com

    Quote Originally Posted by honor3us View Post
    Some friends told me to debbug the program, how can I do that? I'm using code::blocks on Linux.
    Have you searched the web for "debug in code blocks"?

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    2
    Man, you'r a f* genius KKKKKKKK TY a lot! I undestand everything you told me and the code run's perfectly now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem or compiler problem....?
    By miloki in forum C Programming
    Replies: 4
    Last Post: 03-05-2015, 12:48 AM
  2. code problem
    By dikumar2000 in forum C Programming
    Replies: 5
    Last Post: 10-09-2009, 01:15 AM
  3. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  4. code problem
    By NightWalker in forum C Programming
    Replies: 8
    Last Post: 11-16-2003, 05:58 AM
  5. Code problem
    By LOST_ONE in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2002, 01:55 PM