For my homework, I have to input some values and display the values. I won't display the full codes because I only need help with one part where I want to verify that an inputted number contains no letters. I have a good idea of how to do this, which is storing the inputted number in an array, and then checking each individual number if it is a letter, using isdigit(int)
How can I store the each digit of the number of the array? I will also need to know how many digits are in the number.
bool verifyNumber(int numberArray[], int count)
...
int main()
{
int array[5], number, count = 0;
...
cout << "What is the number? " << endl;
cin >> number;
//Here's where the code to store digits in the array should go.
//Here's where the code to count how many digits in the number should go.
verifyNumber (array, count);
...
}
...
bool verifyNumber (int numberArray[], int count)
{
for (int i = 0; i < count; i++)
{
if (!(isdigit(numberArray[i])))
returntrue;
}
returnfalse;
}
...
Numbers are numbers. You cannot read something into an int if that something is not a number. If you already have something in an int variable (or an array of them), then that by definition is a number.
What you can do is to read characters. If those characters are digits, convert them to numbers. If not, then discard them.
On line 7 array has no size and although it may compile it will most likely be hard to use when you try to use individual elements.
Line 12 is formatted input into "int number" there for any part of the input that is not a digit will cause "cin" to fail. You can check this with something like:
1 2 3 4
if (!cin)
{
// code to deal with failure.
}
As for putting each digit of an "int" into an array will take some conversion before storing the digits into an array. Along the lines of changing the "int" into a "string" and then storing each element of the "string" into an array. Or on line 12 you could use std::getline(std::cin,numberString); whre "numberString" is defined as a std::string. Then you could send the string to the "verifyNumber" function where "isdigit()" will work on each element of the string.
Not sure what to do with the "verifyNumber" function yet, but I do not think it will work the way you have it. It seems backwards to me right now. I will have to work on it for awhile.
bool verifyNumber(std::string numberString, int count); // <--- Added ;
int main()
{
int array[10], number{ 0 }, count = 0; // array[10] the 10 needed, but th whole not used.
std::string numberString{ "" };
bool isNum{ false };
//...
std::cout << "What is the number? " << std::endl;
//std::cin >> number;
std::getline(std::cin, numberString);
//Here's where the code to store digits in the array should go.
//Here's where the code to count how many digits in the number should go.
isNum = verifyNumber(numberString, count); // <--- Added isNum because function returns a bool.
std::cout << "\n isNum = " << std::boolalpha << isNum << " and count = " << count << std::endl; // Put her to see what was returned.
if (isNum) // <--- added
anIntVariable = stoi(numberString);
else
{
// Code for numberString not being a compleet number.
}
}
// count had no value here.
bool verifyNumber(std::string numberString, int count)
{
bool isNum{ true };
for (int i = 0; i < numberString.length(); i++)
{
if (!(isdigit(numberString[i])))
isNum = false;
else // <--- Edit Added these two lines.
count++;
}