Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop Remember, try not to do the entire job all at once! First try input of a single number and make sure it works. Make sure that you detect eof correctly. Then add the error checks. Then add the loop...
Please find the following program cpp program to read the user inputs as integers until user enters EOF.
Program:
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
cout<<"Enter the integers\n";
string line;
int alpha=0;
int max =0;
int firstInput =1;
int count=0;
int noIntegers=0;
//read the input until EOF
while (getline(cin, line))
{
count++;
alpha=0;
for (int i = 0; i < line.length(); i++)
{
//check whether the input is Integer
if (! isdigit (line[i]) && line[i] != '-')
{
alpha=1;
break;
}
}
if(!alpha)
{
//convert the string to integer
stringstream intconversion(line);
int x = 0;
intconversion >> x;
//cout << x <<endl;
if(firstInput == count)
{
max=x;
}
// find the maximum value
if(max < x)
{
max=x;
}
noIntegers=1;
}
}
//check the inputs has any Integers
if(noIntegers)
{
cout << "Largest number is " <<max <<endl;
}
else
{
cout <<" No Integers" <<endl;
}
return 0;
}
Output 1:
Enter the integers
12
34
0
-1
100
23
1000
34
Largest number is 1000
Output 2:
Enter the integers
12
asd
0
Largest number is 12
Output 3:
Enter the integers
qw
df
we
No Integers
Screen Shot:
Get Answers For Free
Most questions answered within 1 hours.