Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that asks the user to enter a series of one-digit non-negative n

ID: 3574742 • Letter: W

Question

Write a program that asks the user to enter a series of one-digit non-negative numbers (you will need to do error checking). When the user has finished entering numbers print out how many of each number the user entered. There must be three functions including main. The main function will read in the values from the user and do validity checking on the input. If the number is in the range 0 to 9 main will call a second function that will count the number. After all of the numbers have been entered the main function will call a third function to display the results. You must not use global variables. Name your program Labl0_Exercise2.cpp. Here is a sample output if the user entered -1, 0, 11, 7, 7, 2, 7, and 10: Enter a one digit number or 10 to exit: -1[Enter] The value -1 is not valid.

Explanation / Answer

#include<iostream>
using namespace std;
//Validity checking
void validateNumber(int arr[])
{
int no, c = 0;
//Loops till 10 is entered
do
{
//Accepts a number
cout<<" Enter a one digit positive number and 10 to exit: ";
cin>>no;
//If the number is 10 stop accepting data
if(no == 10)
{
cout<<" Thanks !";
break;
}
//If the number is negative or two digit number show a error message
if(no < 0 || no > 9)
cout<<" The value "<<no<<" is not valid: ";
//Otherwise store the data in array
else
{
arr[c++] = no;
}
}while(1);
}
//Returns the number of valid data available in array
int countNumbers(int arr[])
{
int c = 0;
while(arr[c] != -1)
{
c++;
}
return c;
}
//Display the valid numbers entered by the user
void display(int arr[], int no)
{
cout<<" Valid Numbers Entered by You: ";
for(int c = 0; c < no; c++)
cout<<" "<<arr[c];
}
int main()
{
int c, arr[100], no;

for(c = 0; c < 100; c++)
arr[c] = -1;
validateNumber(arr);
no = countNumbers(arr);
cout<<" Number of Valid Numbers Inputed = "<<no;
display(arr, no);
}

Output:

Enter a one digit positive number and 10 to exit: -1

The value -1 is not valid:
Enter a one digit positive number and 10 to exit: 2

Enter a one digit positive number and 10 to exit: 8

Enter a one digit positive number and 10 to exit: 9

Enter a one digit positive number and 10 to exit: 78

The value 78 is not valid:
Enter a one digit positive number and 10 to exit: 5

Enter a one digit positive number and 10 to exit: 3

Enter a one digit positive number and 10 to exit: 10

Thanks !
Number of Valid Numbers Inputed = 5
Valid Numbers Entered by You:
2 8 9 5 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote