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

I need my program in the language C fixed. I have the output file the way it nee

ID: 3687672 • Letter: I

Question

I need my program in the language C fixed. I have the output file the way it needs to be but I am getting errors when I try to compute the sum of all the numbers in the output file. Below I am posting my program, the image outlining the assignment, and the names for namesnum.txt Thank you!

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include <stdlib.h>

//Main function
int main()
{
FILE *fp, *fpin;
char fullName[80];
char firstName[80];
char number[80];
char lastName[80];

char ch;
int i=0,j=0,k,len;

//Opening files
fp = fopen("namesnum.txt","r");
fpin = fopen("secretnums.txt","w");

if(fp!=NULL)
{
//Processing files
printf(" File namesnum.txt has opened for reading...");
printf(" Processing the file.....");
printf(" Writing data to file secretnums.txt .....");

//Reading each line
while(fscanf(fp, "%s", fullName) != EOF)
{
i=0;j=0;
len = strlen(fullName);

//Constructing first name
ch = fullName[i];
while(!isdigit(ch))
{
firstName[j] = ch;
i++;
ch = fullName[i];
j++;
}
firstName[j]='';

//Constructing number
j=0;
ch = fullName[i];
while(isdigit(ch))
{
number[j] = ch;
i++;
ch = fullName[i];
j++;
}
number[j]='';

//Constructing last name
j=0;
ch = fullName[i];
while(i<len)
{
lastName[j] = ch;
i++;
ch = fullName[i];
j++;
}
lastName[j]='';

//Writing to file
fprintf(fpin, " %s %s %s ", firstName, lastName, number);
}
}
printf(" Data has been successfully written........");
//Closing files
fclose(fpin);
fclose(fp);
printf(" ");
return 0;
}

Benjamin18791Allen
Talal7711Alnakhli
Vinicius12066Zanchini
Steven19468Baker
Jordan33004Beebe
Elisa7580Bergmeier
Jack24963Bruno
Matchima9372Buddhanoy
Devon18165Bullock
Joseph6039Clark
Megan4702Clum
Andrew14720Conn
Tiffiny11037Cooper
Anthony14013Delisio
Connor34178Dickerson
Evan19496Elliott
Ethan27042Emery
Zachary23136Flanigan
Thomas27090Gauerke
Kyle27519George
Ryan28919Gilligan
Joseph6931Goll
Eleni22448Hatzis
Mikael12264Hinton
Lexie13320Holtzman
Daniel32007Ivory
Nicholas18984Kibby
Austin15098Kress
Brian21835Lee
Andrea27689Lorek
Haotian32477Lu
Kevin30458McKay
Connor30347McMann
Nathaniel24055McNees
Logan26804Mers
Tyler21020Miethke
John14707Mollica
Samuel13606Morales
Jelena8514Mrvos
Joel29049Munoz
Paul15581Mureiko
Hayden17141Novosel
Vinicius34038Pinheiro
Olivia6096Rees
David8920Schultz
Austin33359Scott
Alexandra26118Semposki
David15356Stewart
Josiah27515Swann
James26787Tirbaso
Nicholas33306Vogel
Shen2873Wang
Jordan12397Worthington
Yao26756Yu
Jordan9915Zenisek
Hayden21271Zimmerman

Once again we will be working with a list of names that includes everyone in our class, BUT this time each person's name has embedded in the middle asecret number, so each line in the file looks like Benjamin18791Allen Notice that there are no spacesseparating the names or the numbers. The programthat you write is to read in every line of the file "namesnum.txt" and to separate the secretnumber from each of the names, and then to print it at the end of the two names, with a space separating the names. So the outputfile, called secretnums.txt, will look something like Benjamin Allen 18791 And nowthe.real fun, If we all work together we can open up the vault, and the key that opens the vault is actually the sum of everyone's secret numbers. The method Read a line into a single string Using two counters walk through that string, copying one character at a time into the first name string until you reach a digit (For this you can use the isdigit function from ctype.h-ittakesa single char argument(e.g.isdigit (fullname]):)and returns truthfulness.) Put the '10, at the end of the string you have built. Reset one of your counters and copy all the characters to another string in a loop, until you encounter a non-digit. Put the 0, at the end of the string you have built. Reset one of the counters and copy all the characters to the end of the original string into still another string. Put the 10' at the end of the string you have built. Use the function atoi, which takes a single string as an argument, and returns the integer represented by thatstring to extractthe number from the second string you constructed. This function is in stdlib.h. Add that number to a sum. Outputthe strings and the number for each studentto a file, with the number following the name The sum is the secret number. Do not share this number with your friend. Instead you use this number to unlock the vault to get your treasure in my directory Depending on which kind of machine you're on. It will ask you for your name, and please use your real name,the vault keeps track of who is logging in.

Explanation / Answer

you need to add the following code to get sum of secret numbers

//here you need to convert the number and add it to sum
           //printf(" num = %s",number);
           sum += atoi(number);

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include <stdlib.h>

//Main function
int main() {
   FILE *fp, *fpin;
   char fullName[80];
   char firstName[80];
   char number[80];
   char lastName[80];

   char ch;
   int i = 0, j = 0, k, len;

   int sum = 0;
//Opening files
   fp = fopen("namesnum.txt", "r");
   fpin = fopen("secretnums.txt", "w");

   if (fp != NULL) {
//Processing files
       printf(" File namesnum.txt has opened for reading...");
       printf(" Processing the file.....");
       printf(" Writing data to file secretnums.txt .....");

//Reading each line
       while (fscanf(fp, "%s", fullName) != EOF) {
           i = 0;
           j = 0;
           len = strlen(fullName);

//Constructing first name
           ch = fullName[i];
           while (!isdigit(ch)) {
               firstName[j] = ch;
               i++;
               ch = fullName[i];
               j++;
           }
           firstName[j] = '';

//Constructing number
           j = 0;

           ch = fullName[i];
           while (isdigit(ch)) {
               number[j] = ch;
               i++;
               ch = fullName[i];
               j++;
           }
           number[j] = '';


//Constructing last name
           j = 0;
           ch = fullName[i];
           while (i < len) {
               lastName[j] = ch;
               i++;
               ch = fullName[i];
               j++;
           }
           lastName[j] = '';

//Writing to file
           fprintf(fpin, " %s %s %s ", firstName, lastName, number);
           //here you need to convert the number and add it to sum
           //printf(" num = %s",number);
           sum += atoi(number);
       }
   }
   printf(" Data has been successfully written........");
   printf(" Secret sum : %d",sum);
//Closing files
   fclose(fpin);
   fclose(fp);
   printf(" ");
   return 0;
}


------------output----------------

File namesnum.txt has opened for reading...

Processing the file.....

Writing data to file secretnums.txt .....

Data has been successfully written........
Secret sum : 1108053