Hi, I'm new to C++ and I'm taking a course in it. I'm not asking for someone to do my homework because I spent a long time on this problem and I want to know why it won't work. I'm creating a four function calculator using character arrays. My boolean subfunction isn't returning what I want it to and I don't get why.
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
When I type digits into the terminal it still evaluates to false, and when I put in any number of letters it evaluates to false, but if I put in more than 5 characters of digits, then it evaluates to true. Can someone explain what I'm doing wrong and how to fix this? Also, we're not supposed to use anything outside the scope of our course, so if you could keep it to the intro topics it would help. Thanks to anyone who answers in advance!
In isValidNum, num is an array. array[i] means (i+1)th element of that array (+1 because arr[0] is 1st), so num[5] is 6th element of num. Even though you declared x to have only 5 elements in main().
Since cin >> does not bounds checking, when you enter a number long enough, 6th element becomes a digit and true is returned.
What you need is a for loop that goes from 0 to 4 or until the terminating null character is found and checks is all digits are really digits.