Im accepting a float from the user. I want to protect from him inputing a char cos it messes everything up. How do i do this?

do u mean numercal value ?only not charcters ??

btw ..Here is a simpler program that throws away the error characters:

#include <iostream>

using namespace std;

int main()
{
	int x = 0;
	do {
		cout << "Enter a number (-1 = quit): ";
		if(!(cin >> x)) {
			cout << "Please enter numeric characters only." << endl;
			cin.clear();
			cin.ignore(10000,'\n');
		}
		else if(x != -1) {
			cout << "You entered " << x << endl; // or goto any line of the code that u want //
		}
	}
	while(x != -1);
	cout << "Quitting program." << endl;
	
	return 0;
}

There is nothing magic about the number "10000." Just choose a large number. There is actually a maximum-sized number for purposes like this that is built into the C++ standard

sorry, I mean in C.

yeah ,Occasionally you may need a routine in on of your programs that restricts input. An example required all numeric input, no commas or other funny characters. There are really no C routines to deal with this, so you must concoct your own. As a foundation, you can use the INPUT.C program introduced here
In this example, u needed a routine to input a numerical value. The scanf function could not be used; if the user typed in a number such as 40,000, then scanf would interpret that as two values, 40 and zero. The gets function could read input, but the comma again would cause only the value 40 to be read. The best solution would be to write a bulletproof input routine that would do several things:

Restrict the user from typing in more characters than necessary (in this case, only 5 digits)
Ensure that only numbers zero through 9 could be entered.
Allow the user the ability to backspace and erase as well as potentially cancel input.
Each of these conditions can easily be met with a modified input function,
The following program shows how such a routine could be written. Pay attention to the input function and the slew of case statements toward the end.

Name: S_INPUT.C
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define CR 0x0d     //carriage return
#define LF 0x0a     //Line feed
#define BACKSPACE 0x08
#define NULL 0      //Empty character
#define TRUE 1
#define FALSE 0

void input(char *string, int length);


void main()
{
    char string[6];
    long value;

    printf("Enter a 5-digit value: ");
    input(string,5);
    value = atol(string);
    printf("\nYou entered %ld\n",value);
}

/* The customized input() routine */

void input(char *string, int length)
{
    int done = FALSE;
    int index = 0;
    char ch;

    string[0] = NULL;   // init the buffer

    do
    {
        ch = getch();

/* Check to see if the buffer is full */

        if (index == length)
        {
            switch(ch)
            {
                case CR:
                    break;
                case BACKSPACE:
                    break;
                default:
                    ch = NULL;
                    break;
            }
        }

/* process the keyboard input */

        switch(ch)
        {
            case CR:        //Enter key
                putchar(ch);
                putchar(LF);
                string[index] = NULL;
                done = TRUE;
                break;
            case BACKSPACE:
                if(index==0)
                {
                    break;
                }
                else
                {
                    putchar(ch);
                    putchar(' ');
                    putchar(ch);
                    index--;
                    break;
                }
            case NULL:
                break;
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                putchar(ch);
                string[index] = ch;
                index++;
                break;
            default:
                break;
        }
    }
    while(!done);
}

Type the above source code into your editor.
Normally, the input function would allow any text to be entered. This is done by including the following snippet of code as the default:

putchar(ch);
        string[index] = ch;
        index++;
        break;

However, in this program, that snippet is used only when the characters '0' through '9' are entered. The default condition - which holds true for all other characters - is merely a break statement. This means that only the numbers zero through 9 are allowed as input, and whatever string is modified by the input routine will be a number with no nonsense.

Similar modifications to the standard input routine can allow further restrictions. For example, you could modify the program to accept only uppercase characters. (In that case an isupper test would have to be made before input was examined.) Or you can restrict input to vowels or letters common to both Roman and Greek typefaces. It just requires a little deft manipulation and your very own input routine.

Thats quality. Thank you.

sorry, I mean in C.

Check out the strtol example here. It can be easily adapted to floating point instead of integer input by instead using strtod.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.