Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating input

Status
Not open for further replies.

DanJC

Programmer
Dec 6, 2001
19
SE
Can anyone tell me how to test input for a datatype?

I want to be able to read input into a float variable but also to identify if the user input is not compatible with a float before the program throws out an error and dies.

If I read into a char * then I can't convert the input to a float value for mathematical operations so I was wondering if there are any validation functions that can be used.

Also, my Compiler does not accept void * variables as I have already tried this approach.
 
const char CharSet[] = "1234567890+-.";

if(strspn(input,CharSet) == strlen(input))
{
// valid string
// use atof to get value

}



Matt
 
I didn't realise it was that easy,

Thanks very much, I bow to your knowledge.

DanJC

" The mind is like a monkey and it jumps around.... "

 
I tried this method,

My compiler has a problem with scanf, where it looks for full stop characters as well as null string terminators and keeps producing the error "scanf : unable to link to float types" does this mean that i need to use getch/getche()?

If i do, how do i move through a char * variable to store consecutive characters?

Are there any compilers out there which I can use that would work better than the Borland Turbo C 2.01 compiler and which are still free?

DanJC

"These ay the droids yow want maiite..."
(Imagine if Obiwan Kenobi came from Wolverhampton)
 
hello everybody,
could any one help me find out the differece with the following codes.i mean what is the advantage of using one over the other.

1>.
int & f()
{
static int a=5;
return a;
}

2>.int f()
{
static int a=5;
return a;
}

in both the cases the value of the a is retained between
the function calls ,then what is the use of following one methos over the other.

naresh.
chausi@rediffmail.com
 
It looks to me like in case 1>. you are returning the address of the variable rather than the value of it.

This means that in the first case, although the variable will hold it's value outside of the function you can still manipulate the value by using a pointer to it.
In the second case you are only returning the value created by the function f so you can't manipulate it directly.

I would guess that your data is more secure in the second function than in the first one, but in the first one, if you need to change the static variable's value you can do so.

If anyone out there thinks I have got this wrong then please tell me as I am not an advanced/professional programmer.

"Fuzzy wuzzy was a bear, fuzzy wuzzy had no hair, fuzzy wuzzy wasn't fuzzy so he changed his name to Ed Asner..."
 
hello thanks for you response.can you explain what it means to say that "you can manupulate the value by using pointer to it".

does it mean something like this.

int & f()
{
static int a =5;
return a;
}

void main()
{
// int *ptr =f();
//i am trying to accept the reference returned by the
//fuction f in an int * pointer. but it will generate the
//compile time error.
//did you mean that or you meant something else.
}
 
with the first function that returns a reference to the static variable you could do

f() = 17;

now a in the first funciton is no longer 5 but 17.

I am pretty sure you can do this.

also you can do

int& x = f();
any changes you make to x will affect a within the funciton f();

Matt
 
Status
Not open for further replies.

Similar threads

  • Locked
  • Question
Replies
4
Views
87
Replies
3
Views
86
Replies
10
Views
155
Replies
3
Views
114
Replies
2
Views
59

Part and Inventory Search

Sponsor

Back
Top