problem with pointers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: problem with pointers

  1. #1
    Join Date
    Apr 2013
    Posts
    2

    problem with pointers

    Hy there! Its my first time here, because i have not had a problem like this before. The program has a problem when i run it. When i choose "natakar" the second time and input the information it crashes. There is probably a problem with pointers or something in the function "elt natakar" but i do not know where, so if somebody could help me it would be a blessing
    my code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <Windows.h>
    
    #define MIZ 77
    
    typedef struct{
            int miza;
            char natakar[10];
            char kuhar[10];
            char jed[10];
            SYSTEMTIME str_t;
            int kolicina;
            int status;        //1...vneseno   2...pripravljeno
            }narocilo;
            
    typedef struct element{
            narocilo narocilnica;
            struct element *naslednji;
            }elt;
    
    elt *natakar(int *st){
         elt *prvi;
         elt *tmp;
         elt *pom;
     
         int i=*st;
         char ime[10];
         char jed1[10];
         
         system("CLS");
         tmp=(elt*)malloc(1*sizeof(elt));
         pom=(elt*)malloc(1*sizeof(elt));
         int temp=-1;
         while((temp<1)||(temp>77)){
                                    system("CLS");
                                    printf("\tMeni NATAKAR:\n\n");
                                    printf("Miza: ");
                                    scanf("%d",&temp);
                                    }
         tmp -> narocilnica.miza = temp;
         system("CLS");
         printf("\tMeni NATAKAR:\n\n");
         printf("Vase ime: "); 
         scanf("%s", ime);
         strcpy(tmp -> narocilnica.natakar, ime);
         strcpy(tmp -> narocilnica.kuhar, "?");
         system("CLS");
         printf("\tMeni NATAKAR:\n\n");
         printf("Kaj bodo jedli: "); 
         scanf("%s", jed1);
         strcpy(tmp -> narocilnica.jed,jed1);
         temp=-1;
         while ((temp<1)||(temp>20)){          
                               printf("Koliko jedi: ");
                               scanf("%d",&temp);
                               system("CLS");
                               }
         tmp -> narocilnica.kolicina = temp;
         tmp -> narocilnica.status = 1;
         tmp -> naslednji = NULL;
         GetSystemTime(&tmp -> narocilnica.str_t);
         
         if(i==0){
                   prvi=tmp;
                   prvi -> naslednji = NULL;
                   }
         else{
              pom = prvi;
              while(pom -> naslednji != NULL){
                                             pom = pom -> naslednji;
                                             }
              printf("konec zanke");
              pom -> naslednji = tmp;
              }
              
         *st=*st+1;
         return prvi;
         system("PAUSE");
         }
         
    void kuhar(int *st, elt *prvi){
         int i=*st;
         int x=-1;
         system("CLS");
         printf("\tMeni KUHAR:\n\n");
    
         if(i==0){
                   printf("Trenutno se ni nobenih narocil.\n\n");
                   }
         else{
              elt *pom;
              pom = (elt*)malloc(1*sizeof(elt));
              pom = prvi;
              printf("Pripravimo se na kuhanje...\n\n");
              printf("NAROCILA: \n\n");   //dejmo izpisat vsa narocila
              while (pom != NULL){
                                               printf("\nMiza: %d", pom -> narocilnica.miza);
                                               printf("\nJed: %s * %d",pom -> narocilnica.jed, pom -> narocilnica.kolicina);
                                               if(pom -> narocilnica.status == 1) printf("\nStatus: SE NI SKUHANO");
                                               if(pom -> narocilnica.status == 2) printf("\nStatus: Skuhal %s", pom->narocilnica.kuhar);
                                               printf("\nMizo je stregel %s", pom -> narocilnica.natakar);
                                               printf("\nDatum: %d.%d.%d \n\n",pom -> narocilnica.str_t.wDay,pom -> narocilnica.str_t.wMonth,pom -> narocilnica.str_t.wYear);
                                              
                                               pom = pom -> naslednji;
                                               }
              }
         
         system("PAUSE");
         }
    
    void meni(){
    	 int x=-1;	
         elt *prvi;
    
    	 int st=0;    //premikam se po tabeli
    	 
    	 while (x!=0){ 
    		system("CLS");
    		printf("\nIzbirajte:\n\t(1) Natakar\n\t(2) Kuhar\n\t(3) EXIT\n");
    		printf("\n\tIzbira: ");	
            scanf("%d",&x);
            if(x==1) prvi=natakar(&st);
            else if(x==2) kuhar(&st, prvi);
    		}
         free(prvi);
         }
    
    int main(){
        meni();
        return 0;
        }

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,272

    Re: problem with pointers

    So what debugging of the code have you done? For us to tell you what is wrong with your program, we have to debug it. So we have to copy your code, compile it, fire up the debugger, step through your code, see the bad results, and then get back to you with our observations and what to fix. Sorry to say this, but these are things that you should really be doing. If you wrote the code, you must know how to debug the code you wrote.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2013
    Posts
    2

    Re: problem with pointers

    I am writing in Windows 7 in DevC++.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center