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.

// 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;

          case 1:
          cout << "Ans: " << x << " + " << y << " = " << x + y << "\n";
          case 2:
          cout << "Ans: " << x << " - " << y << " = " << x - y << "\n";
          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

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:


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.

#include <iostream>
#include <cctype>
#include <string>
#include <cstdlib>
using namespace std;

int main ()
    int number;
    string input;
    bool inputIsInvalid;
         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;
Quick, although I havent tested the code yet (I will do so once I understand it better), was polite and friendly.
This article has been dead for over six months. Start a new discussion instead.