Validating input in c
In this walkthrough, you will retrieve rows from the column and tries to leave the cell, the Cell Validating event handler will examine new company name string to make sure it is not empty; if the new value is an empty string, the Data Grid View will prevent the user's cursor from leaving the cell until a non-empty string is entered.To copy the code in this topic as a single listing, see How to: Validate Data in the Windows Forms Data Grid View Control.So far I have been using while loops and do while loops for input validation.This is good for simple programs, but if I put in characters when I need a int or a int when a I need a float it crashes my program.Otherwise, you could get scanf to parse the input for you, e.g., with %d instead of %s, which is probably what Sorin had in mind.Oh, and if you do use %s, then you should specify the field width to avoid buffer overflow.For #2, you can just do to discard as many characters in the input stream as possible until it reaches a newline character (from the user pressing Enter).
This is good validation because it works if the user enters in multiple characters as one word, but also works if the user enters in several words (such as "a b c") because of the use of getline.And breaking up the long string is usually the part that people have trouble with. So if you want to get one integer from the user (you can check to see if it's a single digit after the scanf itself succeeds), and they enter an integer, then scanf will return 1. Depending on if 1 or 0 is returned, you can go from there (if single digit, proceed to next step, if not single digit or if return is 0, try again).Otherwise, whiteflags link works for the string parsing route.I get maybe two dozen requests for help with some sort of programming or design problem every day.