I'm new to c++ and trying to code a v simple program where it uses a while loop for user to input numbers where it adds those numbers together but as soon as a word or letter is input it breaks and outputs the sum of, excluding any alphabetical input. Here's the code:
#include <iostream>
using namespace std;
int main() {
char ans;
int ans1 = 0;
cout << "Enter numbers to be added" << endl;
cin >> ans;
while (isdigit(ans)) {
cin >> ans;
ans1 = ans1 + ans;
}
cout << ans1 << endl;
return 0;
}
It's probably something that's been staring me right in the face, but I'd appreciate any help. Thank you