|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Ziff Davis Enterprise Virtual Tradeshows: Hot Topics, Cutting Edge Technology, Real-time Networking among IT Professionals. Learn more
|
|
#1
|
|||
|
|||
|
determining integer/non-integer value
hi,
how do i write a program which determines whether the value entered is integer or non-integer by using only control structures. thanks |
|
#2
|
|||
|
|||
|
i have no idea what you mean by "control structures".
but you can determine if a string is float by testing for the "." eg.... and you can test for the int-value being "0" and it not being the string "0". could you please post more information about your specific problem?
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. |
|
#3
|
|||
|
|||
|
sorry about being unclear....
what i wanted to do is check the user entered value & see whether it's an integer or non-integer.... i'm allowed to use only if,for & while (control structures) & only allowed to use <iostream> or <iostream.h> thanks for the effort |
|
#4
|
|||
|
|||
|
Basically loop through the string and if you find a character that's not '0' - '9'
return false. |
|
#5
|
|||
|
|||
|
complete assignment
maybe it'll make more sense if i explain the whole thing...
i want to constantly ask the user whether he/she wants to enter a value(y/n) if 'y'... i want to test the value for integer/nonInteger & even/odd & add to the counter accordingly.... if 'n' if dispalys 1)no. of values entered 2)odd values entered 3)even values entered 4)sum 5)average 6)integer values entered 7)nonInteger values entered here's what i have so far #include <iostream.h> void main() { int value,sum=0,average=0; char response; int valueCount=0, intCount=0, nonIntCount=0; int evenCount=0, oddCount=0; cout <<"\nDo you want to enter a value? (y/n): "; cin >>response; while ((response|32)=='y'){ cout<<"\nEnter your Value: "; cin >>value; sum=sum+value; valueCount=valueCount+1; if (value%2==0) evenCount=evenCount+1; else oddCount=oddCount+1; cout<<"\nDo you want to enter a value? (y/n): "; cin>>response; } average=sum/valueCount; cout<<"\nThe number of Values you entered is: " <<valueCount; cout<<"\nThe sum of all the values you entered is: " <<sum; cout<<"\nThe average of the values you entered is: " <<average; cout<<"\nThe number of interger values you entered is: " <<intCount; cout<<"\nThe number of non-integer values you entered is: " <<nonIntCount; cout<<"\nThe number of even values you entered is: " <<evenCount; cout<<"\nThe number of odd values you entered is: " <<oddCount<<endl<<endl; } p.s. i've been able to test the value for odd/even if it' s an integer value but changing it to float '%' doesn't seem to work thanks for all the help |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > determining integer/non-integer value |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|