help with code

This is a discussion on help with code within the C Programming forums, part of the General Programming Boards category; I wrote this program in dev++5, and it compiles all right and when I try to run it it comes ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    25

    help with code

    I wrote this program in dev++5, and it compiles all right and when I try to run it it comes up and ask for the accout number and I enter one but that is all that happens after that it just sit there and wont ask other questions or let me input any thing else heres is the code I wrote hope I use tags allright.

    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int acct1;
    float bal, chrg, cred, limit;
    
    printf ("Enter in account number: (-1 to end)\n");
    scanf ("%d", &acct1);
    while (acct1 != -1);{
    
        printf ("Enter in begging balance:\n");
        scanf ("%f", &bal);
    
        printf ("Enter in total charges:\n");
        scanf ("%f", &chrg);
    
        printf ("Enter in total credits:\n");
        scanf ("%f", &cred);
    
        printf ("Enter in total credit limit:\n");
        scanf ("%f", &limit);
    
          bal = (bal+chrg)-cred;
    
                if(bal>limit){
    
                      printf ("Account: %d\n", acct1);
                      printf ("credit limit: %.2f\n", limit);
                      printf ("balance: %.2f\n", bal);
                      printf ("credit limit exceded\n");
          }
          }
          return 0;
          }

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    i alrdy told you when u compile pay attention to warnings and errors it may compile (else you wouldnt be able to run it) but there may still be warnings present in your code
    Code:
    while (acct1 != -1);{
    /*change it to*/
    while(acct!=-1){
    drop that ';' and it will run just fine....
    Last edited by GanglyLamb; 02-10-2003 at 11:16 AM.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    25
    The first time I tryed to compile it gave me a waring about the first printf statement undeclared printf or something like that I messed with it and didnt change it just rewrote it and then it compliled ok

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>The first time I tryed to compile it gave me a waring about the first printf statement undeclared printf
    You either spelled printf wrong or forgot to include stdio.h :-)
    *Cela*

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    25
    Ok I got it to compile then when I run it, it seems to get into a loop can some one look at it and tell me what I have done wrong I am posting new sorce code that I have now.


    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int acct1;
    float bal, chrg, cred, limit;
    
    printf ("Enter in account number: (-1 to end)\n");
    scanf ("%d", &acct1);
    while(acct1!=-1){
    
        printf ("Enter in begging balance:\n");
        scanf ("%f", &bal);
    
        printf ("Enter in total charges:\n");
        scanf ("%f", &chrg);
    
        printf ("Enter in total credits:\n");
        scanf ("%f", &cred);
    
        printf ("Enter in total credit limit:\n");
        scanf ("%f", &limit);
        
         }
          bal = (bal+chrg)-cred;
    
                if(bal>limit){
    
                      printf ("Account: %d\n", acct1);
                      printf ("credit limit: %.2f\n", limit);
                      printf ("balance: %.2f\n", bal);
                      printf ("credit limit exceded\n");
                  }
                 else                     
                                      
                 printf ("Enter in account number\n");
                 scanf ("%d", &acct1);
    
    
          
          return 0;
          }

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>it seems to get into a loop
    You mean an infinite loop? :-) The reason is that you ask for and place a value in acct outside of the loop, so there's no way to exit the loop unless you ask for it inside the loop. The simple fix is to ask again :-)
    Code:
    #include<stdio.h>
    
    int main()
    {
      int acct1;
      float bal, chrg, cred, limit;
      
      printf ("Enter in account number: (-1 to end)\n");
      scanf ("%d", &acct1);
      while(acct1!=-1){
        printf ("Enter in begging balance:\n");
        scanf ("%f", &bal);
        
        printf ("Enter in total charges:\n");
        scanf ("%f", &chrg);
        
        printf ("Enter in total credits:\n");
        scanf ("%f", &cred);
        
        printf ("Enter in total credit limit:\n");
        scanf ("%f", &limit);
    
        printf ("Enter in account number: (-1 to end)\n");
        scanf ("%d", &acct1);
      }
    
      bal = (bal+chrg)-cred;
      
      if(bal>limit){
        printf ("Account: %d\n", acct1);
        printf ("credit limit: %.2f\n", limit);
        printf ("balance: %.2f\n", bal);
        printf ("credit limit exceded\n");
      }
      else
        printf ("Enter in account number\n");
    
      scanf ("%d", &acct1);
    
      return 0;
    }
    *Cela*

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    You're joining in a loop that you cannot exit from him...
    Code:
    while(acct1!=-1)
    Your loop will not stop until acct1 is -1.
    You wanted to check if the account number is -1 or what?

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    25
    No vber I was trying to say that when finished with doing your accts that inputing -1 would end program

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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