Thread: Program will not run thorugh the loop

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    11

    Program will not run thorugh the loop

    Hello
    The program asks the question one time then stops and creates a micorsoft error message. Could someone take a look at it and see if I am missing something. It is suppose to allow you to enter up to 10 accouts w/name and amounts and break if -999 is entered.

    Code:
    
    #include <stdio.h>
    struct info
    {
     int   client_num;
     char  last_name[20];
     float balance;
    };
    
    main()
    {
    
     struct info clients[10];
     int    x;
    
     printf ("Enter account number, last name, and balance.\nEnter -999 to end input.\n\n");
    
     for (x = 0; x < 10; x++)
     {   
    
     printf ("?");
     scanf ("%i", clients[x].client_num);
      if (clients[x].client_num == -999)
     {
     break;
     }
    
     scanf ("%s", &clients[x].last_name);
     scanf ("%f", &clients[x].balance);
     fflush(stdin);
    
    printf ("\n\n");
    }
    
    printf ("ACCOUNT \tLAST NAME \tBALANCE\n");
    
    for (x = 0; x < 10; x++)
     {   
    
    printf ("%08i%20s%.102f\n", clients[x].client_num, clients[x].last_name, clients[x].balance);
     }
    getchar();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > scanf ("&#37;i", clients[x].client_num);
    Here you forgot the &

    > scanf ("%s", &clients[x].last_name);
    Here you used an & where one wasn't necessary.

    > fflush(stdin);
    See the FAQ, this is undefined (even if it appears to work for you).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    Thank you, I knew it was something stupid. With the fflush(stdin); that is what the teacher requests which is why I used it.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > With the fflush(stdin); that is what the teacher requests which is why I used it.
    Not a good sign.
    http://c-faq.com/stdio/stdinflush.html

    What other horrors are they going to teach you I wonder.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    Can I bother you about one more thing??? It is now reading all of my info but if I enter the -999 it is breaking but not going to the next print statment. I thought it would just break in the current for statement and then move to the next one???

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Post your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    Code:
    #include <stdio.h>
    struct info
    {
     int   client_num;
     char  last_name[20];
     float balance;
    };
    
    main()
    {
    
     struct info clients[10];
     int    x;
    
     printf ("Enter account number, last name, and balance.\nEnter -999 to end input.\n\n");
    
     for (x = 0; x < 10; x++)
     {   
    
     printf ("?");
     scanf ("&#37;i", &clients[x].client_num);
      if (clients[x].client_num == -999)
         break;
    
     scanf ("%s", clients[x].last_name);
     scanf ("%f", &clients[x].balance);
     fflush(stdin);
    }
    printf ("\n\n");
    
    
    printf ("ACCOUNT LAST NAME BALANCE\n");
    
    for (x = 0; x < 10; x++)
     {   
    
    printf ("%08i%20s%.102f\n", clients[x].client_num, clients[x].last_name, clients[x].balance);
     }
    getchar();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 07-06-2009, 07:12 AM
  2. Replies: 5
    Last Post: 06-23-2009, 03:51 AM
  3. Timed loop in C program
    By bilmiyor in forum C Programming
    Replies: 10
    Last Post: 03-26-2008, 11:00 AM
  4. Read a file into a loop program
    By TimeClock in forum C Programming
    Replies: 5
    Last Post: 07-17-2003, 06:29 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM