Question
C++ Write a program which: Prompts the user to enter 5 integers Validates each user entry to ensure that it is an integer Stores the
C++
Write a program which:
Prompts the user to enter 5 integers
Validates each user entry to ensure that it is an integer
Stores the user entries in a one-dimensional array of type int
Displays the largest and smallest integer in the array (assume all integers in the array are unique and distinct).
Requirements:
The program must consist of a class called Integers. The class must have:
A public function/method called setUserEntry. This function/method is used to:
Prompt the user for an integer
Invoke a method called validate which verifies that the entry is in fact an integer, if the entry is not an integer, the user is NOT allowed to proceed until he/she enters an integer.
A public function/method called validate ; which verifies that the entry is in fact an integer, if the entry is not an integer, the user is NOT allowed to proceed until he/she enters an integer.
The constructor uses a loop to initialize the elements in the array to zero
The displayIntegers function/method is a const public function/function which takes no parameters and uses a loop to display the values of the array.
The getLargestSmallestIntegers is a public function/method which displays the largest and the smallest integers in the array.
The array is of type int, size 5 and is a private member.
The main function must use a loop to keep the user in the program until he/she wants to quit.
You MUST submit The following THREE files:
A separate .h file called Integers.h.txt
A separate .cpp file called IntegersImp.cpp.txt
A separate .cpp file with main() called main.cpp.txt
Don't forget the corresponding pdf files!
GENERAL RESTRICTIONS
-No global variables
-No labels or go-to statements
-No infinite loops, examples include:
-for(;;)
-while(1)
-while(true)
-do{//code}while(1);
-No break statements to exit loops
this is what i have and my program still crashes
#include
#include
#include "Integers.h"//include Integers.h
using namespace std;
int main()
{
Integer userInput;//class name is integer and the object is userInput
string continu;//string variable continu
cout << "In this program you wil be prompted to enter five integers" << endl;//display a message for the user to know they will have to input five numbers
system("pause");
do {
userInput.setUserEntry();//calling setUserEntry
userInput.displayIntegers();//call method to print values
userInput.getLargestSmallestIntegers();//call method to print largest and smallest numbers
cout << " ";
cout << "y/Y to continue, any other char to exit" << endl;//
cin.ignore();//Adding cin.ignore() clears/ignores the newline from the stream.
getline(cin, continu);//gets string input from the user and inputs it into continu
system("cls");//clears screen
} while (continu == "y" || continu == "Y");//if continu is equal to y/Y then the loop repeats
return 0;
}
-----------------------------------------------------------------------------------------------------------------------
#include
#include
#include "Integers.h"
using namespace std;
void Integer::setUserEntry()
{
float userInput;
int count = 0;
for (int index = 0; index < SIZE; index++)//goes into a loop asking the user to input five numbers
{
do
{
cout << "Enter element " << (index + 1) << endl;
cin >> userInput;
if (!validate(userInput))//my program does not validate properly only makes sure if the numbers are ints<0 or>=0
cout << "that is not an integer, try again";
} while (!validate(userInput));//if it doesn't validate that its an int in those ranges then it goes back into the loop
arraySize[index] = userInput;//if it is a valid number then it is stored in the array
}
}
bool Integer::validate(int userInput)
{
return userInput >= 0 || userInput < 0;
}
void Integer::displayIntegers() const//display the numbers the user typed in
{
cout << "The integers in the array are" << endl;
for (int index = 0; index cout << arraySize[index] << endl; } void Integer::getLargestSmallestIntegers()//gets the largest and smallest numbers inputed and then displays them { int largest = arraySize[0]; int smallest = arraySize[0]; for (int index = 0; index { if (arraySize[index]>largest) largest = arraySize[index]; if (arraySize[index] smallest = arraySize[index]; } cout << "The smallest integer is " << smallest; cout << "and the largest integer is " << largest << endl; } ------------------------------------------------------------------------------------------------------------------ #ifndef INTEGER_H//header file that contains class declaration is called a class specification file #define INTEGER_H//This directive tells the preprocessor to see if a constant named INTEGER_H has not been previously created with a #define directive. const int SIZE = 5;//five is the constant number //Integer class defintion class Integer// integer class declaration. { private: int arraySize[SIZE];//the array is set to size five so that will be it size public: void setUserEntry();//method prompt the user for an integer/validation bool validate(int);//method which verifies that the entry is in fact an integer void displayIntegers() const;//function which takes no parameters and uses a loop to display the values of the array void getLargestSmallestIntegers();//method which displays the largest and the smallest integers in the array }; #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started