chars and floats

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2004
Posts: 4
Reputation: sroberts82 is an unknown quantity at this point 
Solved Threads: 0
sroberts82 sroberts82 is offline Offline
Newbie Poster

chars and floats

 
0
  #1
Jul 22nd, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 141
Reputation: meabed is on a distinguished road 
Solved Threads: 3
Team Colleague
meabed's Avatar
meabed meabed is offline Offline
Junior Poster

Re: chars and floats

 
0
  #2
Jul 22nd, 2004
do u mean numercal value ?only not charcters ??
Real Eyes Realize Real Lies
My Resume
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 141
Reputation: meabed is on a distinguished road 
Solved Threads: 3
Team Colleague
meabed's Avatar
meabed meabed is offline Offline
Junior Poster

Re: chars and floats

 
0
  #3
Jul 22nd, 2004
btw ..Here is a simpler program that throws away the error characters:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int x = 0;
  8. do {
  9. cout << "Enter a number (-1 = quit): ";
  10. if(!(cin >> x)) {
  11. cout << "Please enter numeric characters only." << endl;
  12. cin.clear();
  13. cin.ignore(10000,'\n');
  14. }
  15. else if(x != -1) {
  16. cout << "You entered " << x << endl; // or goto any line of the code that u want //
  17. }
  18. }
  19. while(x != -1);
  20. cout << "Quitting program." << endl;
  21.  
  22. return 0;
  23. }
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
Real Eyes Realize Real Lies
My Resume
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 4
Reputation: sroberts82 is an unknown quantity at this point 
Solved Threads: 0
sroberts82 sroberts82 is offline Offline
Newbie Poster

Re: chars and floats

 
0
  #4
Jul 22nd, 2004
sorry, I mean in C.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 141
Reputation: meabed is on a distinguished road 
Solved Threads: 3
Team Colleague
meabed's Avatar
meabed meabed is offline Offline
Junior Poster

Re: chars and floats

 
0
  #5
Jul 22nd, 2004
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.
  1. Name: S_INPUT.C
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5.  
  6. #define CR 0x0d //carriage return
  7. #define LF 0x0a //Line feed
  8. #define BACKSPACE 0x08
  9. #define NULL 0 //Empty character
  10. #define TRUE 1
  11. #define FALSE 0
  12.  
  13. void input(char *string, int length);
  14.  
  15.  
  16. void main()
  17. {
  18. char string[6];
  19. long value;
  20.  
  21. printf("Enter a 5-digit value: ");
  22. input(string,5);
  23. value = atol(string);
  24. printf("\nYou entered %ld\n",value);
  25. }
  26.  
  27. /* The customized input() routine */
  28.  
  29. void input(char *string, int length)
  30. {
  31. int done = FALSE;
  32. int index = 0;
  33. char ch;
  34.  
  35. string[0] = NULL; // init the buffer
  36.  
  37. do
  38. {
  39. ch = getch();
  40.  
  41. /* Check to see if the buffer is full */
  42.  
  43. if (index == length)
  44. {
  45. switch(ch)
  46. {
  47. case CR:
  48. break;
  49. case BACKSPACE:
  50. break;
  51. default:
  52. ch = NULL;
  53. break;
  54. }
  55. }
  56.  
  57. /* process the keyboard input */
  58.  
  59. switch(ch)
  60. {
  61. case CR: //Enter key
  62. putchar(ch);
  63. putchar(LF);
  64. string[index] = NULL;
  65. done = TRUE;
  66. break;
  67. case BACKSPACE:
  68. if(index==0)
  69. {
  70. break;
  71. }
  72. else
  73. {
  74. putchar(ch);
  75. putchar(' ');
  76. putchar(ch);
  77. index--;
  78. break;
  79. }
  80. case NULL:
  81. break;
  82. case '0':
  83. case '1':
  84. case '2':
  85. case '3':
  86. case '4':
  87. case '5':
  88. case '6':
  89. case '7':
  90. case '8':
  91. case '9':
  92. putchar(ch);
  93. string[index] = ch;
  94. index++;
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. while(!done);
  101. }
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:
  1. putchar(ch);
  2. string[index] = ch;
  3. index++;
  4. 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.
Real Eyes Realize Real Lies
My Resume
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 4
Reputation: sroberts82 is an unknown quantity at this point 
Solved Threads: 0
sroberts82 sroberts82 is offline Offline
Newbie Poster

Re: chars and floats

 
0
  #6
Jul 22nd, 2004
Thats quality. Thank you.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,443
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: chars and floats

 
0
  #7
Jul 22nd, 2004
Originally Posted by sroberts82
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC