Help with creating Input exclusion/ignoring code.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 2
Reputation: vaati is an unknown quantity at this point 
Solved Threads: 0
vaati vaati is offline Offline
Newbie Poster

Help with creating Input exclusion/ignoring code.

 
0
  #1
Oct 20th, 2009
I just started learning C++ today, and after a making calculator I found here and succeeding, with some help from a friend, hence the other name) in changing it and adding a subtraction function, I was wodering if there is any code I can use to accept only the characters I add to a list?

I would be allowing only : 0123456789-

This calc performs oddly if a letter is inputted, and instead of trying to fix the way it is broken, I thought avoiding the problem to start with would be best.

Thanks in advance for helping

Also, im using Microsoft Visual C++ Express Edition.

  1. // initializing C++
  2. #include <iostream>
  3. using namespace std; // declaring function prototypes float addition (float a, float b);
  4. //main function
  5. int main () {
  6. float x; //
  7. float y; //declares variables
  8. float n; //
  9. int op;
  10. int b;
  11. b = 1; //sets value of b to 1
  12. cout << "vaati and Canti's collaborative learning calc. For teaching vaati ;P"; //displays info about program
  13. while (b==1) //creates loop so the program runs as long as the person wants to add numbers
  14. {
  15. //following code prompts the user for 2 numbers to add and calls function addition to display results
  16. cout << "\n" << "Type a number to manipulate (can also use negetive, decimals, etc.) addition and subtraction only.: ";
  17. cin >> x;
  18.  
  19. cout << " Second number: ";
  20. cin >> y;
  21.  
  22. cout << "What do you want to do with these? (1=addition, 2=subtract 3=leave program): ";
  23. cin >> op;
  24.  
  25. switch(op){
  26. case 1:
  27. cout << "Ans: " << x << " + " << y << " = " << x + y << "\n";
  28. break;
  29. case 2:
  30. cout << "Ans: " << x << " - " << y << " = " << x - y << "\n";
  31. break;
  32. default:
  33. cout<<"Not a valid operation\n";
  34. }
  35. //following code sets b to the value the user inputs to determine if the loop is broken to end the program
  36. cout << "Solve another operation? Or leave? (1=Continue, 2=Exit): ";
  37. cin >> b;
  38. cout << "\n";
  39. }
  40. return 0;
  41. //ends the main function of the code
  42. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,817
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
1
  #2
Oct 21st, 2009
This isn't a trivial problem. There are quite a few ways to do it. All are a bit advanced for the first day, but hey, this'll expose you to more code. I tried to think of the most elementary way that was still the closest to correct. Specifically I used atoi instead of strtol because, while strtol is generally better, it's a little more complicated.

The approach of this program is to read in the input as a string, test it against the allowable characters a character at a time, then if it passes, convert it to a number. If you have not already, bookmark this site:

http://www.cplusplus.com/reference/

It has all the C++ libraries and functions. If you don't know a function or library, go to this link and click on the library or type in the function in the search box. I imagine this program uses some new functions for you. Again, there are other ways to do it, but I don't think any are less involved than this one.

  1. #include <iostream>
  2. #include <cctype>
  3. #include <string>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7.  
  8. int main ()
  9. {
  10. int number;
  11. string input;
  12.  
  13. bool inputIsInvalid;
  14.  
  15. do
  16. {
  17. inputIsInvalid = false;
  18. cout << "Please enter an integer : ";
  19. cin >> input;
  20.  
  21. // verify input
  22. int stringLength = input.length ();
  23.  
  24. // test each character. If any characters fail, it's bad input
  25. for (int i = 0; i < stringLength; i++)
  26. {
  27. // test a character. Legal characters are digits and '-'.
  28. char aCharacter = input[i];
  29. if (aCharacter != '-' && !isdigit (aCharacter))
  30. {
  31. inputIsInvalid = true;
  32. }
  33. }
  34.  
  35. if (inputIsInvalid)
  36. {
  37. // error message
  38. cout << "Oops. Bad input. Please try again." << endl;
  39. }
  40. }
  41. while (inputIsInvalid);
  42.  
  43. number = atoi (input.c_str ());
  44.  
  45. cout << "You entered " << number << endl;
  46.  
  47. return 0;
  48. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC