In: Computer Science
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;
}
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...