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

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. Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
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 Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
(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....
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT