Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Read these instructions very carefully. Name your source file midterm.cpp. Creat

ID: 3544693 • Letter: R

Question

Read these instructions very carefully.

Name your source file midterm.cpp. Create a program that:

1. Opens a file called midtermInput.txt for reading.

2. Reads an integer and a string from each line in midtermInput.txt.

3. For each line in midtermInput.txt, reports, on standard output, if the length of the string is equal, more than, or less than the integer. Output should be in the form shown in the example run below.


Here are the contents of midtermInput.txt:

5 Howdy

12 Doody

6 Earth

7 Dinosaur

28 Antidisestablishmentarianism


Here is a sample run:

The length of "Howdy" is 5

12 is larger than the length of "Doody" which is 5

6 is larger than the length of "Earth" which is 5

7 is smaller than the length of "Dinosaur" which is 8

The length of "Antidisestablishmentarianism" is 28

To find the length of a string, use the size function, as discussed on page 356 of your text. Also, see the string class at cplusplus.com.

Explanation / Answer

#include<iostream>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
using namespace std;
int main()
{
   int k=open("midtermInput.txt",O_RDONLY);
   int ofst=1;
   char c='';
   int count=0;
   char nu[100]={''};
   int cou=0;
   while(read(k,&c,1)!=0)
   {
       int num=0;
       count =0;
       cou=0;
       nu[cou++]=c;
       while(c!=' ')
       {
           read(k,&c,1);
           nu[cou++]=c;
       }
       cou--;
       nu[cou]='';
       int i,j=1;
       for(i=cou-1;i>=0;i--)
       {
           num=num+(nu[i]-'0')*j;
           j*=10;
       }
       char jaf[1000]={''};
       while(c!=' ')
       {
           read(k,&c,1);
           jaf[count]=c;
           count++;
       }
       jaf[count-1]='';
       count--;
       if(count==num)
       printf("The length of %s is %d ",jaf,num);
       else if(count<num)
           printf("%d is larger than the length of %s which is %d ",num,jaf,count);
       else if(count > num)
           printf("%d is smaller than the length of %s which is %d ",num,jaf,count);
       ofst++;
   }
   close(k);
   return 0;
}