Thread: function problem

  1. #1
    Unregistered
    Guest

    function problem

    below is part of the function i wrote, I tried editing it to give an error when there is no data in the file but cant... can anyone help?
    the case switch is displaying the below, but when there is no data in the file it just do nothing, i want to have an error or msg saying "no file or record" when they try to display an empty file.
    PHP Code:

    void display_rec
    (void)
    {
        
    int index;
        
    float total_fee;


        for(
    index=0;index<count;++index)
        {
            
    clrscr();
            
    mainpage();
            
    gotoxy(27,8); printf("@ Display Member Record @");
            
    gotoxy(27,9); printf("~~~~~~~~~~~~~~~~~~~~~~~~~");
            
    gotoxy(20,11); printf("Member First Name : %s\n"mem[index].mem_fname);
            
    gotoxy(20,12); printf("Member Last Name  : %s\n"mem[index].mem_lname);
            
    gotoxy(20,13); printf("Member NRIC No.   : %s\n"mem[index].mem_nric);
            
    gotoxy(20,14); printf("Member Address    : %s\n"mem[index].mem_address);
            
    gotoxy(20,15); printf("Member Gender     : %s\n"mem[index].mem_gen);
            
    gotoxy(20,16); printf("Member Birth Date : %s\n"mem[index].mem_dob);
            
    gotoxy(20,17); printf("Member Contact No.: %s\n"mem[index].mem_tel);
            
    gotoxy(20,18); printf("Amount Paid Per Month : $%.2f\n"mem[index].mem_installp);
            
    gotoxy(20,19); printf("No. Of Months Paid : %i\n"mem[index].mem_month);
             
    total_fee=mem[index].mem_installp*mem[index].mem_month;
            
    gotoxy(20,21); printf("Total amount Paid is $ %.2f\n"total_fee);


        
    //    gotoxy(20,22); printf("Amount left to be Paid is $");
            
    gotoxy(23,24);
            
    printf("Press Enter key to view more record");
            
    getchar();
            
    fflush(stdin);
            
    clrscr();
        }



  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    assuming that count will equal 0 when there is no data in the file, just stick an if-statement after the for-loop with the condition that 'count == 0' that prints an errror message if the condition is true

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    33
    hmmm, will try that, i have keep looking on the index instead just now, forgot i can use the count loop.

    THanks will try and see if successful.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    85
    count is undefined variable?

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    33
    my count is undefine, but the program still run smoothly though.. do i need to define my count?

    i still trying to figure the if (count==0).... or there is some problem with my program?

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    33
    i dun think is working....

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Kelvin
    count is undefined variable?
    The variable count is not defined in the code you have given above. If your code compiles, then it means that the count variable must be defined elsewhere (it's probably a global variable).

    Here's some sample code to do the if statement. Put it just before the for loop.
    Code:
    if (count <= 0)
    {
         printf ("No data!\n");
         getchar();
         return;
    }
    And, don't do this
    >fflush(stdin);
    it's undefined behaviour and therefore not good! (fflush is for output streams only)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    33
    sometime if i dun put fflush(stdin) the enter key will carry over to the next function, anyway else to prevent this?

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    33
    hey hammer, thanks

    finally your answer brings me light!! i din put in the return just now
    no wonder cant work
    thanks

  10. #10
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >sometime if i dun put fflush(stdin) the enter key will carry over to the next function, anyway else to prevent this?

    Code:
    while((ch = getchar()) != '\n');

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    33
    sometime if i dun put fflush(stdin) the enter key will carry over to the next function, anyway else to prevent this?

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >anyway else to prevent this?
    Please don't post the same question twice. Especially after it's been answered.

    -Prelude
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    May 2002
    Posts
    33

    Unhappy

    sorry accidently click on back , on my browser and it just repost everything

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by shaik786
    >sometime if i dun put fflush(stdin) the enter key will carry over to the next function, anyway else to prevent this?

    Code:
    while((ch = getchar()) != '\n');
    You can also do it slightly easier with this:
    >while(getchar() != '\n');
    It's the same thing, it just doesn't use a variable to store the return of getchar() in (no point in having one, if you're not going to use it).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM