While loop that detects too many characters

This is a discussion on While loop that detects too many characters within the C Programming forums, part of the General Programming Boards category; Hi Guys Im struggling to figure out how to make this work. im trying to make this little program that ...

  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
    32,820
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,820
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 04:40 PM
  3. While loop with multiple characters
    By arctic_blizzard in forum C Programming
    Replies: 16
    Last Post: 09-20-2008, 01:25 PM
  4. For Loop question for printing characters
    By trprince in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 10:01 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 02:46 AM

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