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

Hello, here is my assignment followed by my codes and two questions. 1. Write a

ID: 3628194 • Letter: H

Question

Hello, here is my assignment followed by my codes and two questions.


1. Write a program that sequentially displays on screen the contents of all of the files listed in the command line. Use argc to control a loop.

2. Write a program that creates a structure template with the following data fields. The program should then prompt the user to enter the data to populate the structure. Finally, use printf() to display the information entered by the user.
Account number
Account owner street address (string)
Account owner city/state (string)
Account owner zip code
Account balances
Account credit limit
Account name

1. #include <stdio.h>
#include <stdlib.h>

int main (int argc, char * argv[])
{
char ch; /* define a variable called ch */
int i; /* define a variable called i */
FILE *fp; /* pointer to file */

for(i = 1; i <=argc; i++) /* starting for loop */
{if((fp = fopen(argv[i],"r"))==NULL) { /* checking condition */
printf("Can't open the file. ");
exit(1);
}

while((ch = fgetc( fp )) != EOF) {
printf("%c", ch);
}
fclose(fp);
}
return 0;
}

2. #include <stdio.h>


/* maximum lenght of arrays */

#define MAXLEN 30

struct info { /* set up info template */

int number;
double balance;
double limit;
char name[MAXLEN]; /* define an array of chars called name with max lenght 30 */
char street[MAXLEN]; /* define an array of chars called street with max lenght 30 */
char citystate[MAXLEN]; /* define an array of chars called citystate with max lenght 30 */
char zipcode[MAXLEN]; /* define an array of chars called zipcode with max lenght 30 */
}; /* end of structure template */


int main (void)
{
struct info personal; /* declare personal as an info variable */

printf("Enter the account number: ");
scanf("%d",&personal.number); /* access to the number portion */
fflush(stdin);

printf("Enter your street address: ");
while (getchar() != ' '); /* flush stdin buffer */
gets(personal.street); /* access to the street portion */


printf("Enter your zip code: ");
while (getchar() != ' '); /* flush stdin buffer */
gets(personal.zipcode); /* access to the zipcode portion */


printf("Enter city/state: ");
while (getchar() != ' '); /* flush stdin buffer */
gets(personal.citystate); /* access to the citystate portion */

printf("Enter the account balance: ");
scanf("%lf",&personal.balance); /* access to the balance portion */
fflush(stdin);

printf("Enter the credit limit: ");
scanf("%lf",&personal.limit); /* access to the limit portion */
fflush(stdin);

printf("Enter the account name: ");
while (getchar() != ' '); /* flush stdin buffer */
gets(personal.name); /* access to the name portion */

printf("Account number: %d ", personal.number);
printf("Address: %s,%s ", personal.street,personal.citystate);
printf("Zipcode: %s ", personal.zipcode);
printf("Account balance: %lf ",personal.balance);
printf("Account limit: %lf ", personal.limit);
printf("Account name: %s ", personal.name);


return 0;
}

questions:
1. I don't understand exactly why in the first code I must add exit(1)
2. In code #2 the zipcode is just a number so it is possible to use the variable int, but what if the zipcode starts with a 0? As I wrote it (string of chars) it doesn't work. My professor says that it has something to do with the modulus operation but I don't understand why.

Thank you!

Explanation / Answer

1) exit is used so that you terminate the program as soon as you find that you cannot open the specified file.You could as well use a return(0); over there it would do the same. exit or return statements are the last lines that are executed in a method. 2)Instead of using gets(personals.zipcode); Try using scanf("%s",&personal.zipcode);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote