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
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