limit the characters using gets function

This is a discussion on limit the characters using gets function within the C Programming forums, part of the General Programming Boards category; Hello Any one know how to limit the character input by user if I use gets()? My program works, it ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    15

    Talking limit the characters using gets function

    Hello

    Any one know how to limit the character input
    by user if I use gets()?

    My program works, it suppose to compare two
    line enter by user and see if they are the same, but I think I need a loop some where
    to tell the user they enter to many characters
    if they enter more then 25 characters. The following is my code, can someone please take it look and give me some suggestions, thank you.

    #include <stdio.h>
    #include <stdlib.h>

    int compare(char *, char *); /* Function prototype for compare two lines */

    int main(void)

    {
    char array_1[26];
    char array_2[26];

    /* Have user enter two line */
    printf("\nPlease enter two lines, with maximum of 25 characters per line\n") ;

    gets(array_1); /* store the first line enter by user to array_1 */
    gets(array_2); /* store the second line enter by user to array_2 */

    /* Call function to compare the two array, and output the the results
    of lines are equal or not equal to screen */

    if(compare(array_1, array_2))
    printf("\nThe two line are equal\n");
    else
    printf("\nThe two line are not equal\n");

    return;
    }

    /* Function definition of compareing two line (arrays) */
    int compare(char *array_1, char *array_2)

    {
    int value;

    /* Use while loop to compare one character at a time from two arrays */
    while (*array_1 == *array_2 && *array_1 != '\0')

    {
    ++array_1;
    ++array_2;
    }

    if(*array_1 == *array_2)
    value = TRUE;
    else
    value = FALSE;

    return (value); /* return the value to the main */
    }

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Any one know how to limit the character input
    by user if I use gets()?
    Can't be done. But you can use fgets to do this. Specify the input stream as standard input (stdin).

    Also you can't compare arrays with logical operators. You must use 'strcmp' and test it's return value.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    15

    Talking

    Troll_King,

    Thanks for your input, the reason that I didn't use strcmp because teacher don't want us to use
    it, she want us to write the "strcmp" function ourself. fgets?? I though we only can use fgets with a open read file, we never learn how to use fgets with stdin, can you give me a example please? thank you

    yukon

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    	char array_1[30];
    	char array_2[30];
    
    
    
    	fgets(array_1,29, stdin);
    	puts(array_1);
    
    	fgets(array_2,29, stdin);
    	puts(array_2);
    This is how fgets works. I'm testing a few other things. I may make another post here concerning the rest of the code.
    Last edited by Troll_King; 10-08-2001 at 02:36 AM.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    15
    Thanks Troll_King,

    By the way, what did you do to witch King???


  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include "stdafx.h"
    #include<stdlib.h>
    
    #define TRUE 0
    #define FALSE 1
    int CompareStrings(char [],char []);
    void GetString(char []);
    
    int main()
    {
    	char array_1[30];
    	char array_2[30];
    	int status;
    
    	GetString(array_1);
    	GetString(array_2);
    
    	status = CompareStrings(array_1,array_2);
    
    	if(status == TRUE)
    	{
    		printf("\nThe strings are equivalent.");
    	}
    	else
    	{
    		printf("\nThe strings are different.");
    	}
    
      return 0;
    }
    
    void GetString(char ar[])
    {
    	printf("Enter string: ");
    	fgets(ar,29, stdin);
    }
    
    // Function definition of compareing two (arrays)  
    int CompareStrings(char array_1[], char array_2[]) 
    { 
    	int index;
    	int value = TRUE; 
    
    	// loop and compare one character at a time from two arrays  
    	for(index = 0; array_1[index] != '\0'; index++) 
    	{ 
    
    		if(array_1[index] == array_2[index])
    		{
    			continue;
    		}
    		else
    		{
    			return value = FALSE;
    		}
    	} 
    
    	return value; 
    }
    Umm, the Witch King was slain. He might some back as an undead specter though.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    15
    Troll King,

    I lost the connection to C borad last night,
    I'm at work right now, but, I'll study the example you give me and try to change my code tonight.

    Thanks again for all your help !!!

    Yukon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

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