Like Tree1Likes
  • 1 Post By quzah

infinite loop

This is a discussion on infinite loop within the C Programming forums, part of the General Programming Boards category; my program works fine but as soon as i try to enter E or e it just stays in a ...

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

    infinite loop

    my program works fine but as soon as i try to enter E or e it just stays in a infinite loop help please see if you can spot my problem.

    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("%.3f\n", value1);     
     
    break;        
    
     
    
    case 's':       
    
    accumulator = value1;     
    
    printf("%.3f\n", value1);      
    
     break;      
    
    
     case '+':      
    
     accumulator += value1;      
    
     printf("%.3f\n", accumulator);       
    
     break;       
    
     
    
     case '-':       
    
     accumulator = accumulator - value1;      
     
    printf("%.3f\n", accumulator);       
     
    break;       
    
     
    
    case '/':     
     
    accumulator = accumulator / value1;       
    
    printf("%.3f\n", accumulator);       
     
    break;       
    
     
    
    case '*':       
    
     accumulator = accumulator * value1;       
    
    printf("%.3f\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%.3f\n", accumulator);
    
    
    return 0;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are trying to scan for %f first. So if you are just entering an E, it gets stuck in the %f, which messes up your scanf call.

    You need to post code with less pointless blank lines, and actual indentation. You don't need a blank line between every line of code, and three between every block. I don't want to have to scroll two pages down just to read five lines.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,778
    The code you posted will not compile for me, check the position of your while and the do's closing brace.

    After fixing that the program works for me.

    Jim

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    thanks for the help guys tryed to move around the brackets still couldnt get it to compile :-(

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Start by fixing up your formatting:
    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("%.3f\n", value1);     
                break;        
                case 's':       
                    accumulator = value1;     
                    printf("%.3f\n", value1);      
                break;
                case '+':      
                    accumulator += value1;      
                    printf("%.3f\n", accumulator);       
                break;       
                case '-':       
                    accumulator = accumulator - value1;      
                    printf("%.3f\n", accumulator);       
                break;       
                case '/':     
                    accumulator = accumulator / value1;       
                    printf("%.3f\n", accumulator);       
                break;       
                case '*':       
                    accumulator = accumulator * value1;       
                    printf("%.3f\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%.3f\n", accumulator);
        return 0;
    }

    Quzah.
    Salem likes this.
    Hope is the first step on the road to disappointment.

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, 03:45 AM
  5. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM

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