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

Write a program that prompts the user for two numbers, begin and end. then reads

ID: 3769121 • Letter: W

Question

Write a program that prompts the user for two numbers, begin and end. then reads the file data.txt. writing to a new file those values whose indexes arc between begin and end (inclusive). The total of the amounts for the matching indexes should also be written to the file. Additional requirements: Each line of data.txt consists of the following values, separated by (dash): An integer which shows the index A name with at most 20 characters A integer which shows the amount Nothing should be printed to the screen other than to prompt the user for begin and end. Your program's results should be written to a file. Your output file must be named output.txt. Example of prompting user: Enter the begin: 5 Enter the end: 10 Example ofouput.txt file: 5- Poppy-273 6- Ava-171 7- lsabclla-22 8- Jcssica-995 9- Lily-965 10- Sophic-905 Total is: 3331

Explanation / Answer

Program:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int begin,end;
int total,index,cost;
char name[15];
char filename[100],c;
FILE *fptr1, *fptr2;
printf("Enter the begin:");
scanf("%d",&begin);
printf("Enter the end:");
scanf("%d",&end);
printf("Enter the filename to open for reading: ");
scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s ", filename);
exit(0);
}
printf("Enter the filename to open for writing: ");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s ", filename);
exit(0);
}
while (fscanf (fptr1, "%d-%s-%d", &index, &name, &cost) != EOF)
{
if(index>=5 && index<=10)
{
fprintf(fptr2,"%d- ", index);
fprintf(fptr2,"%s- ",name);
fprintf(fptr2,"%d- ",cost);
}
for(index=5;index<=10;i++)
{
total=total+cost[index];
printf("Total=%d",total);
}
fclose(fptr2);
fclose(fptr1);
}
return 0;
}

data.txt:

1-John-45
2-Jack-50
3-Peter-70
4-Johnson-100
5-Poppy-273
6-Ava-171
7-Isabella-22
8-Jessica-995
9-Lily-965
10-Sophie-905

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote