nested loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • volunteerguy
    New Member
    • Oct 2006
    • 9

    nested loop

    one more quick question, this isn't complicated but somehow i'm causing infinnite loop when i attempt it. given the following code...

    starting after the first printf i need everything else to loop, any ideas using a nested loop? thanks!

    Code:
     #inclde<stdio.h> 
     
    #define START_CHAR ' '
    #define END_CHAR '.'
     
    int check_sum(int sum);
     
    int main(void) {
    int char_code,sum,checksum,x;
    char letter;
     
    printf("Enter a message ending in a period; or a period only to quit.\n\n");
     
    for(sum = 0; char_code != (int)END_CHAR; sum += char_code) {
    scanf(" %c", &letter);
    char_code = (int)letter;
    }
     
    checksum = check_sum(sum);
     
    printf("Checksum: %c %d\n", (char)checksum, (int)checksum);
     
    return(0);
     
    }
    Last edited by Niheel; Oct 22 '06, 10:49 PM.
  • ssharish
    New Member
    • Oct 2006
    • 13

    #2
    It donst look to me like it is giving you some problem. It will work fine and it is working fine in my system.

    the only suggestion which i can give is avoid using scanf() function. for reading a char. instead use getchar()!!!

    ssharish

    Comment

    • volunteerguy
      New Member
      • Oct 2006
      • 9

      #3
      Originally posted by ssharish
      It donst look to me like it is giving you some problem. It will work fine and it is working fine in my system.

      the only suggestion which i can give is avoid using scanf() function. for reading a char. instead use getchar()!!!

      ssharish
      haha yes i've been told to stay away from scanf, but for this particular program that is what is supposed to be used... don't ask me

      and what i have there does run and execute fine, its getting what I have there to loop that is giving me problems.

      i.e. i want to add another loop outside of my current for loop, that executes just after my first printf, and then will continue looping the entire program until solely a period is entered. Once scanf reads a line with soley a period the program should terminate.

      Comment

      • ssharish
        New Member
        • Oct 2006
        • 13

        #4
        So u mean after a user has entered a string or a char followed with the period which terminated the string. After that, u wanted again to to read a string. Is that right. If that was the case.

        Code:
        #include<stdio.h>
        
        void clear_buffer(void);
        
        int main()
        {
            char string[25];
            int choice=1;
            
            while(choice)
            {
                printf("Enter a string\n?");
                fgets(string, 25, stdin);
                
                printf("You entered %s\n",string);
                
                printf("Press 1 to continue and 0 for exit\n");
                scanf("%d", &choice);  // <-- Note this here
                clear_buffer();
            }
            return 0;
        }
        
        /* This is a function which is used to clear the input buffer */ 
        /* You might need this sort of routine in the future */
        
        void clear_buffer(void)
        {
            int ch;
            
            while((ch=getchar())!='\n' && ch != EOF);
        }  
        
        /* my output
        Enter a string
        ?hello
        You entered hello
        
        Press 1 to continue and 0 for exit
        1
        Enter a string
        ?there
        You entered there
        
        Press 1 to continue and 0 for exit
        0
        */
        ssharish

        Comment

        Working...