Problem w/break statement

This is a discussion on Problem w/break statement within the C Programming forums, part of the General Programming Boards category; When I enter the -999 to break the loop it does not follow to the next loop but ends the ...

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

    Problem w/break statement

    When I enter the -999 to break the loop it does not follow to the next loop but ends the program. Am I missing something here??

    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);
    
     getchar();
    
    }
    
    printf ("\n\n");
    
     
    
     
    
    printf ("ACCOUNT LAST NAME BALANCE\n");
    
     
    
    for (x = 0; x < 10; x++)
    
     {   
    
     
    
    printf ("%08,i%20s,%.102f\n", clients[x].client_num, clients[x].last_name, clients[x].balance);
    
     }
    
    getchar();
    
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You go and print out 10 clients, even though you may not have read that many. You need to keep track of how many you have inputted. Something like
    Code:
    clients_read = i;
    after the first for loop would probably work.

    fflush(stdin) is non-standard, see the FAQ.

    Your program seems like it ends, but it doesn't really. Run it from the command prompt or replace the getchar() at the end of main() with
    Code:
    while(getchar() != '\n');
    getchar();
    That's a little too much whitespace for my taste . . . double-spaced code is hard to read.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 03:05 PM
  2. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM
  3. If statement re-do problem
    By RoD in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 04:46 PM
  4. Major Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 12:06 PM

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