Thread: While loop that detects too many characters

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    While loop that detects too many characters

    Hi Guys

    Im struggling to figure out how to make this work. im trying to make this little program that repeats lines of text you type unless it exceeds 9 characters in which case it lets you know

    The line of code for the while loop is completely wrong i know but i left it there so you could hopefully at least see what i was striving for

    ill put the code in here. if anyone has any idea how i need to write it so it works id be grateful

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define MAX 9
    
    
    int main () 
    {
        char *input;
        
        input = malloc(9);
    
    
        while (gets(input) < MAX)
        {
              printf("\nYou typed %s", input);
        }
        
    printf("\nToo many letters you Entered!");
        
      
        
        return 0;
        
        
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    More excessive pre-colouring mucking up the code formatter...

    [COLOR=#78482f ]#include [COLOR=#d12c26 ]<stdio.h>[/COLOR ][/COLOR ]
    [COLOR=#d12c26 ][COLOR=#78482f ]#include [/COLOR ]<stdlib.h>[/COLOR ]


    [COLOR=#78482f ]#define MAX [COLOR=#2c2ecf ]9[/COLOR ][/COLOR ]


    [COLOR=#bb2d9d ]int[/COLOR ] main ()


    Paste as TEXT people!!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you could RT?M to find out that gets() doesn't return an integer.

    Then you could read this -> SourceForge.net: Gets - cpwiki
    and forget all about ever using gets() again in a program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Thanks ill check that out

    And plain text from now on!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  2. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  3. While loop with multiple characters
    By arctic_blizzard in forum C Programming
    Replies: 16
    Last Post: 09-20-2008, 12:25 PM
  4. For Loop question for printing characters
    By trprince in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 09:01 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM