Help doing an e-mail program in c...

This is a discussion on Help doing an e-mail program in c... within the C Programming forums, part of the General Programming Boards category; You're passing mailr, a single instance of a MAILR struct, to leficheiro. But then in leficheiro you're trying to use ...

  1. #61
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    You're passing mailr, a single instance of a MAILR struct, to leficheiro. But then in leficheiro you're trying to use it as an array. You can't do that.
    If you understand what you're doing, you're not learning anything.

  2. #62
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by itsme86
    You're passing mailr, a single instance of a MAILR struct, to leficheiro. But then in leficheiro you're trying to use it as an array. You can't do that.
    so can you please tell me what to change in the code to solve the error? I would be really thankful

  3. #63
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    In quite a few places, you are using the MAIL or the MAILR structure as if it is a variable, when it is not.

    For example, in leficheiro.c, the lines 13-17, you use this following code:
    Code:
     fgets("MAILR[i].to",120,fp);
     fgets("MAILR[i].from",120,fp);
     fgets("MAILR[i].sub",120,fp);
     fgets("MAILR[i].msg",120,fp);
    fgets("MAILR[i].spc",120,fp);
    Where it SHOULD be using the ptr variable. Also, when using variables, you don't put them inside quotes as you have. The compilable, working code should look like this:

    Code:
     fgets(ptr[i].mail.to,120,fp);
     fgets(ptr[i].mail.from,120,fp);
     fgets(ptr[i].mail.sub,120,fp);
     fgets(ptr[i].mail.msg,120,fp);
    I got rid of the .spc value in there as I didn't find an spc variable anywhere....

    But that is basically your problem, that same thing occurs elsewhere in the program, you just need to remember to get rid of the quotes and make sure you are pointing to the correct values.

    MAILR is an array of MAIL, therefor any MAILR variable should look like so:

    Code:
    MAILR variable;
    variable[0].mail.<place variable here>
    That is rough pseudocode, but you should see what I mean.

    Also, you do not need to pass a pointer to a MAILR variable, as I noticed you do in one of the functions you use MAILR[] which isn't necessary, as the MAILR structure is already declared as an array, which in itself is a pointer. So it's best to remove the [] and also the MAILR *ptr in the declaration of the function.

    Hopefully this clears a bit up, I have a working, compileable version of the code, but I want to see if you can fix it yourself.

  4. #64
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Tyler_Durden
    so can you please tell me what to change in the code to solve the error? I would be really thankful
    Pass the address of the array and its number of element.
    Code:
       int a[3];
        f(a, 3);
    rather than the address of a single variable :
    Code:
       int a;
     
       f(&a);
    Last edited by Emmanuel Delaha; 01-02-2005 at 04:14 AM.
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #65
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by jverkoey
    In quite a few places, you are using the MAIL or the MAILR structure as if it is a variable, when it is not.

    For example, in leficheiro.c, the lines 13-17, you use this following code:
    Code:
     fgets("MAILR[i].to",120,fp);
     fgets("MAILR[i].from",120,fp);
     fgets("MAILR[i].sub",120,fp);
     fgets("MAILR[i].msg",120,fp);
    fgets("MAILR[i].spc",120,fp);
    Where it SHOULD be using the ptr variable. Also, when using variables, you don't put them inside quotes as you have. The compilable, working code should look like this:

    Code:
     fgets(ptr[i].mail.to,120,fp);
     fgets(ptr[i].mail.from,120,fp);
     fgets(ptr[i].mail.sub,120,fp);
     fgets(ptr[i].mail.msg,120,fp);
    I got rid of the .spc value in there as I didn't find an spc variable anywhere....

    But that is basically your problem, that same thing occurs elsewhere in the program, you just need to remember to get rid of the quotes and make sure you are pointing to the correct values.

    MAILR is an array of MAIL, therefor any MAILR variable should look like so:

    Code:
    MAILR variable;
    variable[0].mail.<place variable here>
    That is rough pseudocode, but you should see what I mean.

    Also, you do not need to pass a pointer to a MAILR variable, as I noticed you do in one of the functions you use MAILR[] which isn't necessary, as the MAILR structure is already declared as an array, which in itself is a pointer. So it's best to remove the [] and also the MAILR *ptr in the declaration of the function.

    Hopefully this clears a bit up, I have a working, compileable version of the code, but I want to see if you can fix it yourself.

    i changed my leficheiro.c like you said but when compiling it gives me this error for lines 13-17
    -> request for member "MAILR" in something not a structure or union

    and like you say in the end of your post, how should I declare then the functions?

  6. #66
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    In your source, progmail.h:

    Code:
    void leficheiro(MAILR[]);
    should be:
    Code:
    void leficheiro(MAILR);
    And you need to change the leficheiro function in the definition file respectively so that it is just MAILR ptr in the parameter list.

  7. #67
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by jverkoey
    In your source, progmail.h:

    Code:
    void leficheiro(MAILR[]);
    should be:
    Code:
    void leficheiro(MAILR);
    And you need to change the leficheiro function in the definition file respectively so that it is just MAILR ptr in the parameter list.

    ok, i changed progmail.h, but what you said in the end i didn't understand..definition file? parameter list? sorry but I'm really not getting it....God, I have to do this quickly, lol

  8. #68
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    Gah, sorry.

    Ok, where you actually define the function, is what I meant by the "definition file." This is the file where you find the actual function. The parameter list is everything found inside of the parenthesis.

    So:

    myfunc(int a, int b)

    has two parameters.

    -edit-
    W00T, POST 1000!!
    *does a dance*

  9. #69
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    so in leficheiro.c I should declare it like this?
    void leficheiro(MAILR,i) ??

    I tried it but if I don't declare it leficheiro(MAILR *ptr) it then says in lines from 13-17 that ptr is undeclared...
    It's being difficult solving this and I still have to solve the fgets "\n" problem..lol

  10. #70
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    This is what I have for leficheiro.c:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "progmail.h"
    
    void leficheiro(MAILR ptr)
    {
      FILE *fp;
    int i;
    
    fp = fopen("mail.txt","r");
    
    for(i=0;i<10;i++){
     fgets(ptr[i].mail.to,120,fp);
     fgets(ptr[i].mail.from,120,fp);
     fgets(ptr[i].mail.sub,120,fp);
     fgets(ptr[i].mail.msg,120,fp);
    }
     fclose(fp);
    
    }
    and it compiles fine.

    -edit-
    I'm not sure exactly what you are trying to achieve with this program, as when I ran it it appeared all it did was took input and then I could display it. I'm assuming you want to somehow keep track of multiple emails, in which case you will want to make the function in the way you stated above with the extra int i variable to say which slot in the array to read in to.

  11. #71
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by jverkoey
    This is what I have for leficheiro.c:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "progmail.h"
    
    void leficheiro(MAILR ptr)
    {
      FILE *fp;
    int i;
    
    fp = fopen("mail.txt","r");
    
    for(i=0;i<10;i++){
     fgets(ptr[i].mail.to,120,fp);
     fgets(ptr[i].mail.from,120,fp);
     fgets(ptr[i].mail.sub,120,fp);
     fgets(ptr[i].mail.msg,120,fp);
    }
     fclose(fp);
    
    }
    and it compiles fine.

    -edit-
    I'm not sure exactly what you are trying to achieve with this program, as when I ran it it appeared all it did was took input and then I could display it. I'm assuming you want to somehow keep track of multiple emails, in which case you will want to make the function in the way you stated above with the extra int i variable to say which slot in the array to read in to.

    yep, I need to keep track of numerous emails... I have to save them to "mail.txt" to and be able to view them, and erase them...

    /me wants to dance too when the program is done

    EDIT - changing my progmail.h like you posted gets me this error from lines 13-17
    structure has no member named MAILR

  12. #72
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    Ok, then you'll need to define your function as:

    void leficheiro(MAILR,int)

    in the .h file

    and

    void leficheiro(MAILR ptr,int i)

    in the .c file.

    Then get rid of the for loop using the i variable and it should work fine. Just make sure to put checking in there so i is 0<=i<MAXSIZE where MAXSIZE is the size of your array.

  13. #73
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by jverkoey
    Ok, then you'll need to define your function as:

    void leficheiro(MAILR,int)

    in the .h file

    and

    void leficheiro(MAILR ptr,int i)

    in the .c file.

    Then get rid of the for loop using the i variable and it should work fine. Just make sure to put checking in there so i is 0<=i<MAXSIZE where MAXSIZE is the size of your array.
    Done...
    but the error "structure has no member named MAILR " is still there...
    this is so frustrating... I had the program working but now I can't seem to get it... Bye bye positive grade ...

  14. #74
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    Can you post what's currently in the leficheiro.c file? It seems something's different between our two compilations somehow. Also, what compiler are you using? I compiled this successfully with VC++6.0

  15. #75
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by jverkoey
    Can you post what's currently in the leficheiro.c file? It seems something's different between our two compilations somehow. Also, what compiler are you using? I compiled this successfully with VC++6.0
    Code:
    #include <stdio.h>
    #include <string.h>
    #include "progmail.h"
    
    void leficheiro(MAILR ptr, int i)
    {
      FILE *fp;
      int i;
    
      fp = fopen("mail.txt","r");     /*Abre o ficheiro para escrita em modo append */
    
      for(i=0;i<10;i++){                 /*Ciclo para atribuir um numero a cada email*/
        fgets(ptr[i].MAILR.to,120,fp);       /*Obtenção dos parametros dos mails a partir do ficheiro e passagem para estrutura*/
     fgets(ptr[i].MAILR.from,120,fp);
     fgets(ptr[i].MAILR.sub,120,fp);
     fgets(ptr[i].MAILR.msg,120,fp);
     fgets(ptr[i].MAILR.spc,120,fp);
     }
      fclose(fp);                         /*Fecho do ficheiro*/
    
    }
    I'm using gcc...

    my progmail.h is like this:
    Code:
    #ifndef PROGMAIL_H_INCLUDED
    #define PROGMAIL_H_INCLUDED
    #define MAX 120
    #define NUM 20
    
    typedef struct mail      /*Definição da estrutura dos mails escritos*/
    {
      char from[MAX+1];
      char to[MAX+1];
      char sub[MAX+1];
      char msg[MAX+1];
    } MAIL;
    
    typedef struct          /*Definição da estrutura dos mails lidos do ficheiro*/
    {
      MAIL mail;
    }MAILR[NUM];
    
    void leficheiro(MAILR, int); /Declaração dos comandos usados no main*/
    void comandom(MAIL *m);
    void comandoC(MAIL *m);
    void comandoh(MAILR[]);
    void comandoL(MAIL *m);
    #endif
    and progmail.c
    Code:
    #include <stdio.h>    /* funções como printf, scanf, etc */
    #include <stdlib.h>   /* função exit */ 
    #include <string.h>   /* funções relacionadas com strings */
    #include "progmail.h" /* Para uso de funcoes inerentes a todo o programa*/
    
    int help(){                    /*Função de ajuda*/
      
      printf ("Help \n \n");
      printf ("Mail Commands \n \n");
        
    printf("------------------------------------------------------------------------\n");
      printf("|   t     <message list> type messages                                 |\n");
      printf("|   d     <message list> delete messages                               |\n");
      printf("|   R     <message list> reply to message senders                      |\n");
      printf("|   r     <message list> reply to message senders and all recipients   |\n");
      printf("|   m     <user list> mail to specific users                           |\n");
      printf("|   q          quit, saving unresolved messages in mbox                |\n");
      printf("|   x          quit, do not remove system mailbox                      |\n");
      printf("|   h          print out active message headers                        |\n");
      printf("------------------------------------------------------------------------\n");
    
      return 0;
    }
    
    int main (){
      char c,input[BUFSIZ];
      MAIL mailmessage;
      MAILR mailr;
    
      printf("E-mail program.\n");
    
      leficheiro(&mailr);  
      
      do{
      putchar ('&');
      fgets(input,BUFSIZ,stdin);          /*Detecção do input do user*/
      c = input[0];
        
      switch (c)                        /*Detecção do comando correspondente ao input do user */
          {
          case '?':help();break;
          case 't':printf("ok\n");break;
          case 'd':printf("ok\n");break;
          case 'R':printf("ok\n");break;
          case 'r':printf("ok\n");break;
          case 'm':comandom(&mailmessage);break;
          case 'q':printf("ok\n");break;
          case 'x':printf("Program shutting down!\n"); break; 
          case 'h':printf("ok\n");break;
          case 'C':comandoC(&mailr);break;
          case 'L':comandoL(&mailmessage);break;
          default: printf("Unknown command -> %s \n",input);break;  /*Detecta se o comando existe no programa e mostra-o ao user*/
          } 
    
          }while (c!='x');
      
        return 0;
      }

Page 5 of 6 FirstFirst 123456 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21