Question

In: Computer Science

Here is my C++ program so far. Is there anyway I can make it display an...

Here is my C++ program so far. Is there anyway I can make it display an error if the user enters a float? Thanks

#include <iostream>
using namespace std;
// Creating a constant for the number of integers in the array
const int size = 10;
int main()
{
// Assigning literals to the varibles
int a[size];
int sum=0;
float avg;
// For loop that will reiterate until all 10 integers are entered by the user
for(int i=0; i<size; i++)
{
cout<<"Enter integer value: ";
cin>>a[i];
}

cout<<"Your array contains the following numbers: "<<endl;
// For loop will display all the integers entered by the user
for(int i=0; i<size; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"From your list of 10 numbers:"<<endl;
// Determines the minimum integer entered
int min = a[0];
for(int i=1; i<size; i++)
{
if (min>a[i])
min=a[i];
}
// Displays the minimum
cout<<"The minimum is: "<<min<<endl;
// Determines the maximum integer entered by the user
int max = a[0];
for (int i=1; i<size; i++)
{
if (max<a[i])
max=a[i];
}
// Displays the maximum
cout<<"The maximum is: "<<max<<endl;
// Computes the sum of all the integers entered by the user
for (int i=0; i<size; i++)
{
sum+=a[i];
}
}
// Displays and computes the sum off all the integers
cout<<"The sum is: "<<sum<<endl;
// Computes the average of all the integers
avg = sum/10.0;
cout<<"Average is: "<<avg<<endl;
return 0;
}

Solutions

Expert Solution

Please find the updated C++ code,

1. First of all declare a float variable to store the user input        
float ans;  

2. Accept the user input
cin>>ans;

3. First check if the user input is equal to the int(user input) If both are the same, then we are sure that the user inputs int input. If the user inputted a float value, then we are using a while loop to iterate until the user enters an int value.
while(ans!=int(ans))

4. Raise an ERROR
cout<<"ERROR: FLOAT VALUE IS NOT ALLOWED"<<endl;

5.Prompt the user to enter the input again
cout<<"Enter integer value: ";

6.Accept the user input
cin>>ans;

7. If you want to terminate whenever the user enters a float, then replace the while with an if and also inside the if use a break statement after the error.

8.Then store the int input into the array
a[i]=ans;

(I believe that I made the code simple and understandable. If you still have any query, Feel free to drop me a comment)

Updated code:

#include <iostream>
using namespace std;
// Creating a constant for the number of integers in the array
const int size = 10;
int main()
{
// Assigning literals to the varibles
int a[size];
int sum=0;
float avg;
  
// For loop that will reiterate until all 10 integers are entered by the user
for(int i=0; i<size; i++)
{
cout<<"Enter integer value: ";
float ans; // 1. First of declare a float variable to store the user input
  
//2.Accept the user input
cin>>ans;
  
//3. First, check if the user input is equal to the int(user input)
//If both are same, then we are sure that the user inputs int input
//If the user inputted a float value, then we are using a while loop to
//iterate until the user enters an int value.
while(ans!=int(ans))
{
//4. Raise an ERROR
cout<<"ERROR: FLOAT VALUE IS NOT ALLOWED"<<endl;
//5.Prompt the user to enter the input again
cout<<"Enter integer value: ";
//6.Accept the user input
cin>>ans;
}
//7. If you want to terminate whenever the user enters a float,
//then replace the while with an if
//and also inside the if use a break statement after the error.
  
//8.Then store the int input into the array
a[i]=ans;
}
  
cout<<"Your array contains the following numbers: "<<endl;
  
// For loop will display all the integers entered by the user
for(int i=0; i<size; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"From your list of 10 numbers:"<<endl;
  
// Determines the minimum integer entered
int min = a[0];
for(int i=1; i<size; i++)
{
if (min>a[i])
min=a[i];
}
  
// Displays the minimum
cout<<"The minimum is: "<<min<<endl;
  
// Determines the maximum integer entered by the user
int max = a[0];
for (int i=1; i<size; i++)
{
if (max<a[i])
max=a[i];
}
  
// Displays the maximum
cout<<"The maximum is: "<<max<<endl;
  
// Computes the sum of all the integers entered by the user
for (int i=0; i<size; i++)
{
sum+=a[i];
}
  
// Displays and computes the sum off all the integers
cout<<"The sum is: "<<sum<<endl;
// Computes the average of all the integers
avg = sum/10.0;
cout<<"Average is: "<<avg<<endl;
return 0;
}

Please check the compiled program and its output for your reference:


Output:

Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...


Related Solutions

Here is what I have so far. I have created a code where a user can...
Here is what I have so far. I have created a code where a user can enter in their information and when they click submit all of the information is shown. How can I add a required field for the phone number without using an alert? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="signin.css"> <script> function myFunction(){ document.getElementById('demo').innerHTML = document.getElementById('fname').value + " " + document.getElementById('lname').value + " " + document.getElementById('street').value + " "...
The code following is what I have so far. It does not meet my requirements. My...
The code following is what I have so far. It does not meet my requirements. My problem is that while this program runs, it doesn't let the user execute the functions of addBook, isInList or compareLists to add, check, or compare. Please assist in correcting this issue. Thank you! Write a C++ program to implement a singly linked list of books. The book details should include the following: title, author, and ISBN. The program should include the following functions: addBook:...
I need a short and clear opinion so I can understand the question and make my...
I need a short and clear opinion so I can understand the question and make my own opinion Writing in the 19th century, the British economist Thomas Malthus argued that the law of diminishing marginal returns implied eventual misery for humanity because, in the case of agriculture, there is a fixed amount of available land and additional labor would only yield ever smaller increases in food production. The inevitable result, as Malthus saw it, was that population growth would eventually...
How can I make my own function for rbinom(n, size, prob) in R. So, basically I...
How can I make my own function for rbinom(n, size, prob) in R. So, basically I want to build my own rbinom function that gives me the same result as rbinom that's built in R software.
I JUST WANT AN EXAMPLE ANSWER SO I CAN MAKE MY OWN. THANK YOU!! Conduct research...
I JUST WANT AN EXAMPLE ANSWER SO I CAN MAKE MY OWN. THANK YOU!! Conduct research on New York City's large soda ban. Pretend you work at the Paradise City attorney's office. Draft a law that restricts the sale of large sugary drinks in Paradise City. Make sure to define the types of drinks and types of sellers restricted by the law. Example Statute: Title XXXIV (Links to an external site.)Links to an external site. ALCOHOLIC BEVERAGES AND TOBACCO Chapter...
How would I make it so that when I run my code it does not ask...
How would I make it so that when I run my code it does not ask for input (not having to enter after the statement and enter 0 for example) after ROXY (Forever ROXY Enterprises) appears? Like it would tell me the second statement right away along with the Roxy phrase. This is in C++. My code: #include / #include using std::endl; int main() {    void readAndConvert();    unsigned int stockSymbol;    unsigned int confNum;    std::cout << "ROXY...
Need solution for 3rd question I solved first part below is my answers so far. Really...
Need solution for 3rd question I solved first part below is my answers so far. Really need help on second question. Below is the Background, What I need to do, and the price data to start with. Any help with getting me started is appreciated.    Background The Book Emporium wants to price books to optimize profits. The spreadsheet for this homework has sales data on Harry Potter book 7. For each week, the Book Emporium varied prices on Harry...
MATLAB script Gaussian Elimination without pivoting, not working no matter what I try. So far my...
MATLAB script Gaussian Elimination without pivoting, not working no matter what I try. So far my script is : function [x] = myGauss( A, b ) n = size(A,1); % getting n Ab = [A,b];      % produces the augmented matrix x = zeros(n,1); % solution fprintf('Augmented matrix \n') %FORWARD ELIMINATION   for k=1:n-1     for i=k+1:n         lambda = A(i,k)/A(k,k);         for j=k+1:n             A(i,j) = A(i,j) - lambda*A(k,j);         end;         b(i) = b(i) - lambda*b(i)     end; end; %Backwards...
As a challenge question i was asked to make a program that can make a newly...
As a challenge question i was asked to make a program that can make a newly made Directory that has the name input from the user. I am to create a program that can make a txt file in the newly created directory. I am doing this in windows and this is what i got. #include <windows.h> #include <string> #include <iostream> #include <string.h> #include <fstream> #include <TlHelp32.h> using namespace std; int main() { string Text = "Test string"; ofstream file;...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT