Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Input validation Posted by Minis on 26 Aug 2009 at 4:54 PM
Please tell me how to validate the input as numerical only. I need to use a loop to validate the input.

Thanks in advance!
Report
Re: Input validation Posted by toddlerasim on 26 Aug 2009 at 10:49 PM
What do you mean by " you need to use a loop"? If the validation is for a single digit ( 0 to 9) input then the isdigit() will work for you. If the validation is for a two or higher digit number then the approach is quite different. Could you confirm what exactly you need.

But then I am just a toddler in C / C++.
Report
Re: Input validation Posted by Minis on 27 Aug 2009 at 5:56 AM
I am a toddler too and this is my task in the course-to use a loop. Like while(condition) -true-false.
Report
Re: Input validation Posted by AsmGuru62 on 27 Aug 2009 at 8:45 PM
Numerical definition may be quite complex:

"6272"
"+6272"
"-6272"
"2.890"
".498232"
"2.4893e-78"
"2.4893E+78"

If you need to validate all of these cases - you need some work to do. However, for simple integer validation you need two things:

1. Check for +- sign at the first character and if sign is found then skip that character.
2. Check every next character as being a digit and if so, then you got a correct integer value.
3. If you meet character with value == 0, then stop checking
Report
Re: Input validation Posted by Minis on 28 Aug 2009 at 6:26 AM
C Primer Plus book gives an option to check if the input is an integer or not through the return value of scanf. If the return value is != 1 than the input is not an integer. But I can't make the while loop that works. Any ideas for how the code should look like?
Thanks.
Report
Re: Input validation Posted by AsmGuru62 on 30 Aug 2009 at 5:34 AM
If you want to use the scanf return value then loop to enter few values will looke like this:
int flag=1, value;

while (flag == 1)
{
	printf ("\nENTER VALUE: ");

	// scan value into integer
	flag = scanf ("%d", &value);

	// use value if it is an integer
	if (flag == 1)
	{
		// use it here ...
	}
}
Report
Re: Input validation Posted by Minis on 30 Aug 2009 at 6:17 PM
Thanks for the idea. I saw other examples with the same structure working just fine. But for my program I need the user to re-enter the value if it's incorrect. So I need to use !=1. Unfortunately, that doesn't work. The loop stops at printf"Please re-enter" and never ends.
Report
Re: Input validation Posted by AsmGuru62 on 1 Sept 2009 at 4:49 AM
Please, post your code.
Report
Re: Input validation Posted by Minis on 1 Sept 2009 at 6:45 AM
int main(void)
{
//declare variables
int number, status;
int i = 1;

//ask the user for the desired number and validate the input. Only numeric input allowed.
do
{
printf("Please enter a number:");
status = scanf("%d", &number);
while(status != 1)
{
printf("The input is not numeric. Please try again:");
status = scanf("%d", &number);

}
//print the amount of asterisks
for(i=1; i<= number; i++)
{
printf("*");
}
printf("\n");

}while(number >= 1);

//end of the program
printf("End\n");

What is really interesting for me is not how to fix the code(I already got an answer)but why the loop is not working. Thanks.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.