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