Thread: infinite loop

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    12

    infinite loop

    This program works fine , right up to when i enter 0E for example to end tests it doesnt do anything its not ending nor printing out the printf statement


    Code:
     #include <stdio.h>
    int main (void)
      {  
     float value1; 
     float accumulator = 0;  
     char operator;    
    
    
    
    printf ("\t\tCalulator\n");   
    printf ("\t\t---------\n");   
    
    do {
    
    
    printf("Type in your test\n");  
    scanf("%f %c", &value1, &operator);     
    
        
    
    switch (operator) 
      {       
     case 'S':      
     accumulator = value1;   
     printf("%.2f\n", value1);     
     break;        
    
     case 's':       
     accumulator = value1;     
     printf("%.2f\n", value1);      
     break;        
    
     case '+':      
     accumulator += value1;      
     printf("%.2f\n", accumulator);       
     break;       
    
      case '-':       
     accumulator = accumulator - value1;      
     printf("%.2f\n", accumulator);       
     break;        
    
    case '/':      
    accumulator = accumulator / value1;       
    printf("%.2f\n", accumulator);        
    break;       
    
     case '*':       
     accumulator = accumulator * value1;       
    printf("%.2f\n", accumulator);                 
    break;        
    
     case 'E':        
     printf("End of Tests\n");       
     break;        
    
     case 'e':      
     printf("End of Tests\n");        
    break;             
    
     default:       
    printf("UNKNOWN OPERATOR\n");       
     break;
      }
    }
    while (operator != 'e' && operator != 'E');
      
    printf("\n%.2f\n", accumulator);
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Hint: 'e' never equals 'E'.

    Edit: I guess my Boolean logic needs some work. Go with bithub answer first.

    Tim S.
    Last edited by stahta01; 03-31-2011 at 04:26 PM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by stahta01 View Post
    Hint: 'e' never equals 'E'.

    Tim S.
    That's a horrible hint since it has nothing to do with his problem.

    Change your scanf statement to:
    Code:
    if(scanf("%f %c", &value1, &operator) != 2)
    {
        printf("Invalid input\n");
        return 0;
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does this code produce infinite loop?
    By matchkop in forum C Programming
    Replies: 15
    Last Post: 05-25-2010, 09:52 AM
  2. Using while produces an infinite loop...
    By UCF43 in forum C Programming
    Replies: 4
    Last Post: 04-01-2010, 04:47 PM
  3. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  4. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  5. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM