944,107 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 352
  • C++ RSS
Oct 20th, 2009
0

Help with creating Input exclusion/ignoring code.

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vaati is offline Offline
2 posts
since Oct 2009
Oct 21st, 2009
1
Re: Help with creating Input exclusion/ignoring code.
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.

C++ Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Segmentation Fault ??
Next Thread in C++ Forum Timeline: hello everybody..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC