Question

In: Computer Science

Hi, I have created the following code and I was wondering if it is possible to...

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;
}

Solutions

Expert Solution

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 :)


Related Solutions

Hi, I am wondering how to create code that will allow (in c) the console to...
Hi, I am wondering how to create code that will allow (in c) the console to read a password from a .txt file, and have the ability to change this password? I have a function that logs certain users in using simple if statements and hardcoded passwords, but i would like the ability to be able to change the password and read it from a .txt so it is editable instead of having it in a function in the code....
Hello, I have created the following code in C++. The goal of this code is to...
Hello, I have created the following code in C++. The goal of this code is to read 3 types of employee information(String/*Name*/, String/*Phone number*/,Int/*Office number*/, ) from a .txt file and store that information into an array which can be accessed by various functions. The code below is within a header file, and im trying to define some functions (within a .cpp file) of my class to carry out the following tasks. I have already managed to define a couple...
Hi, I'm currently doing an accounting case and I was wondering about this following statement: "On...
Hi, I'm currently doing an accounting case and I was wondering about this following statement: "On a tour of CEM's manufacturing facility, I noticed that their facilities have good security with cameras installed that have a view of all of the areas where inventory is stored. THere is a large holding area with various parts, nuts, bolts, and wire coils, etc. THe chief financial officer indicated that CEM keeps these leftover parts and supplies from finished construction projects in inventory...
I was wondering if you can tell me if the following code is correct and if...
I was wondering if you can tell me if the following code is correct and if its not can it be fixed so it does not have any syntax errors. Client one /** * Maintains information on an insurance client. * * @author Doyt Perry/<add your name here> * @version Fall 2019 */ public class Client { // instance variables private String lastName; private String firstName; private int age; private int height; private int weight; /** * First constructor for...
Hi I have a java code for my assignment and I have problem with one of...
Hi I have a java code for my assignment and I have problem with one of my methods(slice).the error is Exception in thread "main" java.lang.StackOverflowError Slice method spec: Method Name: slice Return Type: Tuple (with proper generics) Method Parameters: Start (inclusive) and stop (exclusive) indexes. Both of these parameters are "Integer" types (not "int" types). Like "get" above, indexes may be positive or negative. Indexes may be null. Description: Positive indexes work in the normal way Negative indexes are described...
HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
hi! I have this code. when I run it, it works but in the print(the number...
hi! I have this code. when I run it, it works but in the print(the number we are searching for is ) it prints the number twice. ex. for 5 it prints 55 etc. apart from this, it works #include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int n,i; printf("Give me the size of the table : \n"); scanf("%d", &n); int A[n],x,y; for (i=0;i<n;i++) A[i]=rand() % n; for (i=0;i<n;i++) printf("%d\n", A[i]); srand(time(NULL)); y=rand()% n ; printf("The number we are searching...
Hi I was wondering if I could get an explanation on how race, ethnic groups, multiracial...
Hi I was wondering if I could get an explanation on how race, ethnic groups, multiracial people, minority groups, dominanat groups, prejudice, discrimination and racisism all relate to one another?
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 + " "...
Hi, I was wondering how to calculate the molar masses of these 2 compounds since my...
Hi, I was wondering how to calculate the molar masses of these 2 compounds since my instructions say to use my data and not the periodic table. The data is also listed below. Thanks! · Glycerol · NaCl · CaCl2 Data: H2O freezing point: 0 Glycerol freezing point: -1 NaCl freezing point: -5 CaCl2 freezing point: -4.3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT