Can you check if it is right? my code is below. I just need to check if I am doi
ID: 3804754 • Letter: C
Question
Can you check if it is right? my code is below. I just need to check if I am doing it right since I do not know how to compile. Please provide screenshot. I will make sure to thumbs up. Thank you!
#include <stdio.h>
#include <stdlib.h>
void conversion(char *words, char *phoneNo)
{
int amt;
int result;
for(amt=0; words[amt]!=''; amt++)
{
char ch=words[amt];
if ('A' <= ch && ch <= 'C')
// 50= number 2 in ascii
result = 50;
if ('D' <= ch && ch <= 'F')
// 51= number 3 in ascii
result = 51;
if ('G' <= ch && ch <= 'I')
// 52= number 4 in ascii and so forth
result= 52;
if ('J' <= ch && ch <= 'L')
result = 53;
if ('M' <= ch && ch <= 'O')
result =54;
if ('P' <= ch && ch <= 'S')
result = 55;
if ('T' <= ch && ch <= 'V')
result = 56;
if ('W' <= ch &&ch <= 'Y')
// 57= number 9 in ascii
result = 57;
//puts the ascii number in the position in array
phoneNo[amt] = result;
}
// adds null terminator to last position in array
phoneNo[amt]='';
}
int main()
{
FILE *read,*write;
char words[8];
char phoneNo[8];
//reads phone_list.txt
read = fopen("phone_list.txt", "r");
//creates phone_list.txt.cvt in writing mode
write = fopen("phone_list.txt.cvt", "w");
if (read == NULL)
printf("No file");
else
{
// while loop until end of file is reached
while (!feof(read))
{
// scans from read, in string format, and into words
fscanf(read, "%s",words);
//calls the conversion function
conversion(words,phoneNo);
// prints from write pointer and prints phone number
fprintf(write,"%s ",phoneNo);
}
}
/* closing Input */
fclose(read);
/* closing Output */
fclose(write);
//terminates
return 0;
}
phone_list.txt is the following:
TAKEOUT
HAIRCUT
THEBOSS
BEERCAN
PETCARE
CARWASH
MATHHLP
ABAGAIL
GROOMER
CLEANER
DAYCARE
AIRPORT
NUMBERS
ROSELYN
LETTERS
MUSEUMS
PROGRAM
RUDOLPH
LYNDSAY
and the output is supposed to be:
echo 'Expected:'
echo 'Enter file name: phone_list.txt'
echo 'Output file name: phone_list.txt.cvt'
echo '8253688'
echo '4247288'
echo '8432677'
echo '2337226'
echo '7382273'
echo '2279274'
echo '6284457'
echo '2224245'
echo '4766637'
echo '2532637'
echo '3292273'
echo '2477678'
echo '6862377'
echo '7673596'
echo '5388377'
echo '6873867'
echo '7764726'
echo '7836574'
Explanation / Answer
I assume you are asking about how to fix the compile time errors. It's very simple. All you have to do is pass the filename to the fopen() method in the main() function in double quotes (" ") like this:
read = fopen("phone_list.txt", "r");
write = fopen("phone_list.txt.cvt", "w");
This will solve all your compile time errors. If it is about some run time problem, I'd like to request you to repost the question with a sample data from "phone_list.txt". As that would help the experts to understand your program better. Following is the revised code with the changes made for the above two statements:
#include <stdio.h>
#include <stdlib.h>
void conversion(char *words, char *phoneNo)
{
int amt;
int result;
for(amt=0; words[amt]!=''; amt++)
{
char ch=words[amt];
if ('A' <= ch && ch <= 'C')
// 50= number 2 in ascii
result = 50;
if ('D' <= ch && ch <= 'F')
// 51= number 3 in ascii
result = 51;
if ('G' <= ch && ch <= 'I')
// 52= number 4 in ascii and so forth
result= 52;
if ('J' <= ch && ch <= 'L')
result = 53;
if ('M' <= ch && ch <= 'O')
result =54;
if ('P' <= ch && ch <= 'S')
result = 55;
if ('T' <= ch && ch <= 'V')
result = 56;
if ('W' <= ch &&ch <= 'Y')
// 57= number 9 in ascii
result = 57;
//puts the ascii number in the position in array
phoneNo[amt] = result;
}
// adds null terminator to last position in array
phoneNo[amt]='';
}
int main()
{
FILE *read,*write;
char words[8];
char phoneNo[8];
//reads phone_list.txt
read = fopen("phone_list.txt", "r"); // Name of the file in double quotes(" ")
//creates phone_list.txt.cvt in writing mode
write = fopen("phone_list.txt.cvt", "w"); // Name of the file in double quotes(" ")
if (read == NULL)
printf("No file");
else
{
// while loop until end of file is reached
while (!feof(read))
{
// scans from read, in string format, and into words
fscanf(read, "%s",words);
//calls the conversion function
conversion(words,phoneNo);
// prints from write pointer and prints phone number
fprintf(write,"%s ",phoneNo);
}
}
/* closing Input */
fclose(read);
/* closing Output */
fclose(write);
//terminates
return 0;
}
Hope this helps. Have a good time!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.