infinite loop

This is a discussion on infinite loop within the C Programming forums, part of the General Programming Boards category; I am trying to wrote code that DOES NOT utilize pointers (that is another similar program for later) and has ...

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    infinite loop

    I am trying to wrote code that DOES NOT utilize pointers (that is another similar program for later) and has to use getchar
    to print in reverse whatever is input by user. So I first was trying to figure out how to use getchar to print exactly what was input. I am getting an infinite loop. Can someone tell me why??
    Just what am I doing wrong here???

    Code:
    #include <stdio.h>
    
    #define N 50
    
    main()
    {
    
      char ch;
      int i;
      
      printf("Enter in any message.\n");
    
      for (i=0; i < N; i++)
        {
          ch = getchar();
          while (ch != '\n')
            putchar(ch);
        }
      printf("\n");
    
     return 0;
    }
    Sue B.

    dazed and confused


  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    took me a sec but you problem is the while loop will never end cause ch will always be the same since getchar only returns the first character typed, there is nothing to change its value in the loop.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi Again,
    What I think you require here is two for loops, one to store user input into string, the other to store the output the reverse of the intput to the display of the input....

    Code:
    #include <stdio.h>
    #include <string.h>
    int main (void)
    {
    char in_str[50];
    int i;
    
    for(i = 0; i < 50; i++)
          in_str[i] = getchar();   /*this loop stores character in string*/
    
    in_str[i] ='\0'; /*add null character*/
    
    /*second loop*/
    printf("\n");
    for(i = strlen(in_str)-1; i >=0; i--)
         putchar(in_str[i]); 
    }
    /*second loop prints in reverse the input string - notice use of strlen- dont know if you have covered this yet- this sets i to last character input minus the null character in string- we print to display each character then decrement i (i-- ) until i is less than 0 (first element or character)*/
    Last edited by bigtamscot; 09-20-2001 at 11:51 PM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    nothing happens

    I compiled your code and nothing happens.
    After entering in message and hitting return,
    computer just sits there waiting ...............

    I added a while loop to check for \n after input of message,
    still nothing.

    What is missing here???
    Sue B.

    dazed and confused


  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Thats because i followed your code for 50 input characters. If your message is less than 50 then the program waits for the rest of the input of 50 chars. An obvious restriction. Busy just now answer later
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    It would be much better to stop when the user hits [enter] (AKA '\n'), rather than after a specific number of characters.


    Replace this:

    Code:
    for(i = 0; i < 50; i++)
      in_str[i] = getchar();
    with this:

    Code:
    i = -1;
    do
    { 
      i++;
      in_str[i] = getchar();
    } while ((instr[i] != '\n') && (i < 49))
    
    instr[i+1] = 0; //don't forget the null zero when you input this way.
    Of course, you should replace 50 with N and 49 with N-1 so you can vary the string length if you want.

  7. #7
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    or this should also work

    Code:
    #include <stdio.h>
    
    #define N 50
    
    main()
    {
        char ch;
        for(int i = 0;(i < N) && (ch == getchar) != '\n';i++)
        {
            putchar(ch);
        }
    
        return 0;
    }
    or this

    Code:
    #include <stdio.h>
    
    #define N 50
    
    main()
    {
        char ch[N];
        int inputsize = 0;
        for(int i = 0;(i < N) && (ch[i] == getchar) != '\n';i++);
    
        inputsize = i;
    
        for (/*int - this may be unneccessary*/ i  = 0;i < inputsize;i++)
            purchar(ch[i]);
        return 0;
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 03:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM

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