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...
Write a c program that reads a .img file by rows and columns and prints out...
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be: 2 4 DFJSK HJ5JF HFDY5
Write a program that will read in from input file one line at a time until...
Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces, commas and periods....
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...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT