Thread: Checking that a string contain only numeric characters in C...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    14

    Checking that a string contain only numeric characters in C...

    User can either key in the answer for the question or key in ‘n’ to skip the question. If an invalid response is detected, user will be asked to re-enter the string until it is valid.

    This is the question... Can anyone help me please?? Thank you...


    Code:
    for(i=0;i<strlen(ANS);i++)
    		{
    				while(isalpha(ANS[i]) || (ANS != "n" || ANS != "N"))
    				{
    					printf ("Wrong input! Please re-enter the answer.\n");
    					fflush(stdin);
    					gets(ANS);
    				}
    		}
    		
    
    		ans = atoi(ANS);
    
    		if(strlen(ANS)==1 && (ANS[0] == 'n' || ANS[0] == 'N'))
    			{
    				Participants.skipped+=1;
    			}
    
    		else
    			if(ans == real_ans)
    			{
    				Participants.correct+=1;
    			}
    		else
    		{
    			Participants.wrong+=1;
    		}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well...
    • You don't need a nested loop.
    • You should compare ANS[i] with 'n', not "n".
    • Use !isdigit(ANS[i]).
    • Think carefully about your use of ||. Should && be used instead?
    • Do not use fflush(stdin). Rough alternatives include just reading and discarding chars until you encounter a '\n' or EOF.
    • Do not use gets. Rough alternatives include fgets.
    • Your indentation could be improved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't compare strings with ==. You need something like strcmp. Or you could do what you're doing later on there, and compare individual characters. I'm not sure why you're going back and forth. You shouldn't use gets either. You should just make a function that checks your input and returns something if it's OK, and something else if it's not.
    Code:
    int checkinput( char input[], int len )
    {
        if input[0] is n
            return OK (optionally, return something else saying they wanted to quit)
        else 
            for each character in
                if you found a non-number
                    return NOT OK
        else
            you reached the end and just had numbers so return OK
    }

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Numeric Characters to Hex
    By Pharrox in forum C++ Programming
    Replies: 13
    Last Post: 11-20-2008, 10:55 AM
  2. Checking if a var is numeric?
    By Glauber in forum C++ Programming
    Replies: 21
    Last Post: 05-18-2008, 12:51 PM
  3. Numeric checking
    By spaceboo in forum C Programming
    Replies: 6
    Last Post: 01-23-2003, 10:37 AM
  4. checking for valid numeric field..
    By ronkane in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2002, 09:04 PM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 02:53 PM