Question

In: Computer Science

Write a C++ program that reads integers from standard input until end of file. Print out...

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.

The whole program is under 40 lines of code. Just read from cin.

Now, you need to detect non integers. In this case, 100 is an integer but things like foo or 375.2 or 23skidoo are not.

You can read into integers using cin >> something, but you need to check for those error cases. The stream will read the integer and will stop at whatever is not an integer. Maybe (if it tries to read foo) it fails and you have to clear() the error. Maybe some number was read but there’s extra characters after... SO you need to peek at the next character, and if it’s not a space then it’s one of the error cases. You have to read and throw away the characters. Look into the peek and get methods on streams, and use isspace to decide if something is a space or not. Remember that there may be more than one character to discard.

Solutions

Expert Solution

#include<fstream.h>
#include<string.h>
#include<ctype.h>
int a[1000],p=0,t; //**globally array declared to store integer value of whole file **//
int total(int val,int sum){return sum=sum*10+val;}
void add(int val){ a[p]=val;p++;} //**add integer in array**//
int getint(int val){int k=0;for(int i=48;i<val;i++){ k++;}return k; } //**return integer resides in file**//
void string_detect(char s[100],int n)
{
   int sum=0,flag=0;
   for(int i=0;i<n;i++)
   {
       if(isdigit(s[i]))
       {
           int no=int(s[i]);
           int value=getint(no);
           sum=total(value,sum);
           flag=1;
       }
       if((isspace(s[i+1])&&flag==1)||!isdigit(s[i+1])&&flag==1){add(sum);   sum=0;flag=0;}
   }
}
int main()
{
ifstream fin;
fin.open("tejas.txt");
char s[100]; //**maximum length of line is 100 character**//
while(!fin.eof())
{
       fin.getline(s,100); //**line by line scanning**//
       string_detect(s,strlen(s));
}
for(int i=0;i<p;i++){ //**sort the array**//
   for(int j=i;j<p;j++) //**maximaum value will move at the end of array**//
   {
       if(a[i]>a[j]){t=a[i];a[i]=a[j];a[j]=t;}
   } }
   return a[p-1]; //**return last value of array**//
}


Related Solutions

(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Write a program that reads in characters until end of file. The program should count and...
Write a program that reads in characters until end of file. The program should count and print the number of characters, printable characters, vowels, digits, and consonants in the input. Use functions to check whether a character is a vowel, a consonant, or a printable character. Define and use macros to test if a character is a digit or a letter.
Write a loop that reads positive integers from standard input, printing out those values that are...
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, and that terminates when it reads an integerthat is not positive. The values should be separated by single blank spaces. Declare any variables that are needed. PLEASE ANSWER IN C
Write a loop that reads positive integers from standard input, printing out those values that are...
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, and that terminates when it reads an integer that is not positive. The values should be separated by single blank spaces. Declare any variables that are needed. (In C language please).
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Code is in C Write a program that reads integers from stdin. Once it reaches the...
Code is in C Write a program that reads integers from stdin. Once it reaches the * end of the input, it prints the smallest absolute value among those * of the numbers it read. * * For example, if * 4, 6 -3, 3, -2, 13, -4 * are read from stdin, the program should print 2. * * If the end of file is reached before any integer is seen, the * number printed should be INT_MAX (defined...
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
IN C++ Write a program that reads in int values from the user until they enter...
IN C++ Write a program that reads in int values from the user until they enter a negative number like -1. Once the user has finished entering numbers, print out the highest value they’ve entered, the lowest value they’ve entered, and the total number of numbers they’ve entered. The negative number they entered should not be taken as one of the values entered.
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, 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 Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT