Thread: Weird loop keep happeneing....pls help debug...

  1. #1
    Registered User Diamonddrago's Avatar
    Join Date
    Nov 2009
    Posts
    6

    Weird loop keep happeneing....pls help debug...

    I wrote this program for a project due on the 21st.....

    well as it goes into a module SerEmp() it strats to do a weird lopping sequence...

    if u go thru option 1 > 4 u can search a record off a file that is opened....and i had included an error msg to catch any items that are not found.....and for some weird reason after the error message shows up....u cannot exit the program etc...it keeps flashing the error message even out of the module....

    everything has to stay void as i need to add more to the porgram...but for now...pls

    find out why its going out of wack.....i never seen such a logical error...ever....T.T


    void SerEmp(void)
    { clrscr();

    int Target,EmpID,Found;
    char check,EmpName[40],DOA[9],DOP[9],DOB[9],Mstat;
    FILE *mem,*pay;

    if((mem=fopen("E:\\Proj\\Member.dat","r"))==NULL)
    { printf("File is Empty!");
    sleep(2);
    SerEmp();
    }

    else
    { printf("Please Enter Employee ID to Search : ");
    fflush(stdin);
    scanf("%i",&Target);

    while (!feof(mem))
    { fscanf(mem,"%i %[^/]%*c %[^/]%*c %[^/]%*c %[^/]%*c %c",&EmpID,EmpName,DOA,DOP,DOB,&Mstat);
    if(Target==EmpID)
    { printf("Employee ID : %04i\n",EmpID);
    printf("Employee Name : %s\n",EmpName);
    printf("Date of Appointment : %s\n",DOA);
    printf("Date of Probational Period : %s\n",DOP);
    printf("Date of Birth : %s\n",DOB);
    printf("Maritial Status : %s\n",&Mstat);
    sleep(5);
    Employeemgt();
    }

    else if(Target!=EmpID)
    { printf("Record Not Found, please check Employee Id"); // Here is where it occurs......i can't find what i did wrng....pls help....
    sleep(2);
    Employeemgt();
    }
    }
    }

    //getch();
    fclose(mem);
    //Employeemgt();

    }
    Last edited by Diamonddrago; 11-19-2009 at 12:03 AM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Ok, I guess it is here

    Code:
    void MainMenu()
    {
    ...
       while(choice>4 || choice<1)
      {
         clrscr();
         Border();
         Header();
         gotoxy(29,21);
         printf("Invalid Entry!!!!Please try again");
         sleep(2);
          MainMenu();
       }
    ....
    }
    So, if the choice is wrong, you will call again MainMenu(). You enter correct choice and it terminates. What happens then? The while-loop continues. It prints again an invalid message and calls MainMenu() again. Forever and ever and ever and ever.
    Your error in your logic? Well "choice" is not a global variable. It is local for each function call in MainMenu.
    Nevertheless, I really don't think this is necessary. Just make main be wrapped around a while loop.
    Code:
    void MainMenu()
    {
        int choice;
        ....
        while (1)
        {
              ....
              if (choice < 1 || choice > 4)
              {
                   printf("Error");
                   ...
                   continue;
               }
               ...
               //when done break or return
         }
    }

  3. #3
    Registered User Diamonddrago's Avatar
    Join Date
    Nov 2009
    Posts
    6
    no C_nuta...that works fine....

    but i guess i could try that in the part that is causing the error....

    thanks you so much but a bit more feedback would be helpful guys.....
    Last edited by Diamonddrago; 11-18-2009 at 03:34 PM.

  4. #4
    Registered User Diamonddrago's Avatar
    Join Date
    Nov 2009
    Posts
    6
    That wasn't it....it is still doing the same thing....

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You did two things wrong:

    1. You didn't indent anything.
    2. You posted everything in bold.

    No one wants to read either of those.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User Diamonddrago's Avatar
    Join Date
    Nov 2009
    Posts
    6
    Quote Originally Posted by quzah View Post
    You did two things wrong:

    1. You didn't indent anything.
    2. You posted everything in bold.

    No one wants to read either of those.


    Quzah.
    but i did indent them.....they keep appearing like that in the post.....and sorry for the bold.....i used that in most other forums....

    and i have attached the code in .cpp format....just open it & view in notepad etc....

    please guys....help......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  2. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  3. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM
  4. Replies: 1
    Last Post: 11-19-2001, 04:45 PM
  5. for loop = weird
    By -KEN- in forum C# Programming
    Replies: 2
    Last Post: 11-11-2001, 10:58 AM