In: Computer Science
Hi, I have created the following code and I was wondering if it is possible to make an "if" statement in the first for loop that would output an error if the user enters a float, character or string? I was thinking of something along the lines of: if(a[i] != int) but that didn't work :( Thank you.
#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;
}
OUTPUT:
EDITED CODE:
#include <bits/stdc++.h>
using namespace std;
// Creating a constant for the number of integers in the
array
const int size = 10;
bool check(string x)
{
for(int i=0;i<x.length();i++)
{
if(x[i]=='.') //checking for a decimal value.
return false;
else if(isdigit(x[i])==false) //checking for string, characters or
invalid inputs.
return false;
}
return true;
}
int main()
{
// Assigning literals to the variables
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++)
{
//here, if you take an input in integer and try to input float or
string or char, it will throw and error and your program would
stop.
//so we take and input as string first. we will pass it through a
check function. the check function will then check for any
invalidity
//i have shown some examples in the output screenshot.
string x;
cout<<"Enter integer value: ";
cin>>x;
while(check(x)==false) //if its invalid, we can keep looping, until
the user enters a valid integer as input.
{
cout<<"Invalid integer, please enter a valid integer:
";
cin>>x;
}
a[i]=stoi(x);//we then convert the string to integer using
stoi.
}
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;
}
#include <bits/stdc++.h>
using namespace std;
// Creating a constant for the number of integers in the array
const int size = 10;
bool check(string x)
{
for(int i=0;i<x.length();i++)
{
if(x[i]=='.') //checking for a decimal value.
return false;
else if(isdigit(x[i])==false) //checking for string, characters or invalid inputs.
return false;
}
return true;
}
int main()
{
// Assigning literals to the variables
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++)
{
//here, if you take an input in integer and try to input float or string or char, it will throw and error and your program would stop.
//so we take and input as string first. we will pass it through a check function. the check function will then check for any invalidity
//i have shown some examples in the output screenshot.
string x;
cout<<"Enter integer value: ";
cin>>x;
while(check(x)==false) //if its invalid, we can keep looping, until the user enters a valid integer as input.
{
cout<<"Invalid integer, please enter a valid integer: ";
cin>>x;
}
a[i]=stoi(x);//we then convert the string to integer using stoi.
}
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;
}
If you have any doubts, or require any clarifications of any kind, do let me know in the comment section. Thank you :)