| | |
Help with creating Input exclusion/ignoring code.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
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)
// initializing C++ #include <iostream> using namespace std; // declaring function prototypes float addition (float a, float b); //main function int main () { float x; // float y; //declares variables float n; // int op; int b; b = 1; //sets value of b to 1 cout << "vaati and Canti's collaborative learning calc. For teaching vaati ;P"; //displays info about program while (b==1) //creates loop so the program runs as long as the person wants to add numbers { //following code prompts the user for 2 numbers to add and calls function addition to display results cout << "\n" << "Type a number to manipulate (can also use negetive, decimals, etc.) addition and subtraction only.: "; cin >> x; cout << " Second number: "; cin >> y; cout << "What do you want to do with these? (1=addition, 2=subtract 3=leave program): "; cin >> op; switch(op){ case 1: cout << "Ans: " << x << " + " << y << " = " << x + y << "\n"; break; case 2: cout << "Ans: " << x << " - " << y << " = " << x - y << "\n"; break; default: cout<<"Not a valid operation\n"; } //following code sets b to the value the user inputs to determine if the loop is broken to end the program cout << "Solve another operation? Or leave? (1=Continue, 2=Exit): "; cin >> b; cout << "\n"; } return 0; //ends the main function of the code }
•
•
Join Date: Jan 2008
Posts: 3,817
Reputation:
Solved Threads: 501
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.
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)
#include <iostream> #include <cctype> #include <string> #include <cstdlib> using namespace std; int main () { int number; string input; bool inputIsInvalid; do { inputIsInvalid = false; cout << "Please enter an integer : "; cin >> input; // verify input int stringLength = input.length (); // test each character. If any characters fail, it's bad input for (int i = 0; i < stringLength; i++) { // test a character. Legal characters are digits and '-'. char aCharacter = input[i]; if (aCharacter != '-' && !isdigit (aCharacter)) { inputIsInvalid = true; } } if (inputIsInvalid) { // error message cout << "Oops. Bad input. Please try again." << endl; } } while (inputIsInvalid); number = atoi (input.c_str ()); cout << "You entered " << number << endl; return 0; }
![]() |
Similar Threads
- While loop user input (C++)
- how to input joystick in c++ to this code? (Game Development)
- Creating a file input function and calling structs. (C++)
- Some errors in my code. can someone help? (C++)
- Why does my code not take my input? (C++)
- Creating a Basic String Database (C++)
- Check the code!! (C++)
- Creating a GUI that accepts user input help (Java)
Other Threads in the C++ Forum
- Previous Thread: Segmentation Fault ??
- Next Thread: hello everybody..
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






