Like Tree1Likes
  • 1 Post By AndiPersti

Function to check whether the input is composed ONLY of alphabet/space

This is a discussion on Function to check whether the input is composed ONLY of alphabet/space within the C Programming forums, part of the General Programming Boards category; I got this code. (It's more of a test code since I will use this as a function for my ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    33

    Function to check whether the input is composed ONLY of alphabet/space

    I got this code. (It's more of a test code since I will use this as a function for my address book program)
    This code is supposed to ask the user to input characters at most 20 and they should only be composed of alphabets/spaces..


    Code:
    #include <string.h>
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {   
       char addressbook[6][30][0];
       int current_record = 0, length, i=0;
       int address_count = 0;
       current_record = address_count-1; //variable for the current entry
        
        do{
        printf("Enter the first name:"); //asks the user to input the first name of the entry
        scanf("%s", addressbook[0][current_record]);
        length = strlen(addressbook[0][current_record]); //gets the length of the input (should be less than 20)
        for(i=0; i<=20; i++){
            if(isalpha(addressbook[0][current_record][i])||addressbook[0][current_record][i]==' ')
            { //checks if the input characters in first name are alphabets/a space
                break; //
            } //exits once it sees that the characters are composed of alphabets/space
            else {
            printf("You can only use alphabets and space and a maximum of 20 characters. Input again."); //puts a message that prompts the user to enter alphabets/space alone
            } //end else    
        } // end for
        } //end do
        while(length<20)
        
        
        return 0;
    }
    I have removed the error from the main code but there's still this lingering error in the return part.

    syntax error before 'return'.

    It won't compile. Help me see what's wrong with the code?

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Code:
    do{
    ...
    }while(...) ;
    

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    yay. thanks. ) now it's working. although it loops even though I've typed only alphabets and spaces. Do you know how to stop it looping after getting the desired input?

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Boy, where to start. You got this from somewhere else? If so, my advice is to burn it and put together your own solution.
    Code:
    char addressbook[6][30][0]
    What the heck is that supposed to do? Apparently create a 3D array but have the 3rd dimension of zero elements.
    Which leads to more nonsense:
    Code:
    if(isalpha(addressbook[0][current_record][i])||addressbook[0][current_record][i]==' ')
    The rest of the looping and logic makes no sense either given what you want to accomplish. The do/while loop will only end when length >= 20. The for loop is broken when 'presumably' the first alphabetic/space character is seen, otherwise it will print an error and continue looping. (I say presumably because of the zero-length 3rd dimension).

    Also, some minor things:
    Magic Numbers are bad
    and
    See here for excellent brief on good commenting

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    In future, please don't open a new topic just because you can't wait for an answer.
    Stick to one thread.

    Bye, Andreas
    Salem likes this.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Agreed - use the other thread, as AndiPersti points out.
    Closed.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to check if string contains only alphabet
    By thungmail in forum C Programming
    Replies: 10
    Last Post: 11-05-2009, 05:08 PM
  2. Check char for a space or for end of array?
    By BC2210 in forum C Programming
    Replies: 10
    Last Post: 11-16-2008, 03:14 AM
  3. space between input
    By dNNNY in forum C Programming
    Replies: 8
    Last Post: 07-15-2008, 12:52 PM
  4. space problem with user input
    By codebrawler in forum C++ Programming
    Replies: 5
    Last Post: 01-08-2006, 01:01 PM
  5. Is there a way to check a string for a space
    By Raigne in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2005, 02:27 AM

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