Practice with File Handling
Site: | Saylor Academy |
Course: | CS107: C++ Programming |
Book: | Practice with File Handling |
Printed by: | Guest user |
Date: | Friday, February 4, 2022, 5:18 PM |
Description
Complete these activities to practice reading from and writing to files.
Assignments
Assignment A-13.1
What is the difference between scanf( )
and gets( )
?
Assignment A-13.2
Write a program that reads two command line arguments. The first one is a string and the second one a file name. The program then should search the file, printing all lines that contain the string. Use fgets( )
rather than get( )
. Hint: use the function strstr( )
.
Assignment A-13.3
Find the error(s) in the following program segment;
int main () { int* fp; int, k; fp = fopen ("pizza"); for(k =0; k <30; k++) printf ("Jill likes pizza.\n", fp); fclose ("pizza"); return0; }
Assignment A-13.4
Suppose we have these statements in a program
FILE *fp1, *fp2;
char ch;
fp1 = fopen ("file1", "r");
fp2 = fopen ("file2", "w");
Assuming that both files opened successfully, supply the missing arguments in the following function calls:
a) fscanf (___, "%c", &ch);
b) fprintf (___, "%c\n", ch);
c) fclose (___); /* close the file for writing */
Assignment A-13.5
What is the output of the following program? Include in your answer the contents of any files created.
int main() { FILE *fp; int k; char ch; fp = fopen ("file.txt","w"); if(fp == NULL) { printf("Error opening file.\n"); return1; } else { for(k =2; k <20; k+=2) fprintf (fp,"%3d", k); fclose (fp); fp = fopen ("file.txt","r"); while((ch = fgetc(fp))!= EOF) { if(ch ==' ') fputc ('*', stdout); elseif(ch !='1') fputc (ch, stdout); else { putchar ('\n'); putchar (ch); } } } return0; }
Assignment A-13.6
Based on the statement below:
fp = fopen ("names.dat", "w");
a) What is the statement to declare this file pointer?
b) Write a statement that writes the string "Rock my heart" to the file.
c) Write a statement that closes the file.
Assignment A-13.7
With the following declarations:
double x;
int n;
char ch, str[40];
Indicate the contents of the variables after each of the following input operations are performed. Assume that the file accessed by fp consists of the data given and that each statement below occurs at the beginning of the program immediately after a statement that opens the file.
123 3.145 xyz<newline>35 z<newline>
a) fscanf (fp, "%d%lf%s%c", &n, &x, str, &ch);
b) fscanf (fp, "%d%lf", &n, &x);
fscanf (fp, "%s %c", str, &ch);
c) fscanf (fp, "%lf%d%c%s", &x, &n, &ch, str);
d) Write the statement that opens this file.
Assignment A-13.8
What does this program do?
int main() { FILE *fp; char str[80]; fp = fopen ("file.txt","w"); if(fp == NULL) { printf("Error opening file.\n"); return1; } do { printf ("Enter a string (ENTER to quit): "); gets (str); strcat(str,"\n"); fputs (str,fp); }
while(*str !='\n'); return0; }
Assignment A-13.9
What does this program do? Modify it so that it does the same thing for a file rather than for what is printed to the screen.
#define STOP '|' int main() { char c; int n_chars, n_lines, n_words; int wordflag =0; n_chars = n_lines = n_words =0; while((c = getchar())!= STOP) { n_chars++; if(c =='\n') n_lines++; if(c !=' '&& c !='\n'&& c !='\t'&& wordflag ==0) { wordflag =1; n_words++; } if((c ==' '|| c =='\n'|| c =='\t')&& wordflag ==1) wordflag =0; } n_lines++; printf ("characters = %d, words = %d, lines = %d\n", n_chars, n_words, n_lines); return0; }
Assignment A-13.10
Based on the file data.txt, create a program that reads the file and outputs its content to the screen, with the appropriate headers (ex: Name, ID, Quiz 1, Quiz 2, Quiz 3, Quiz 4, Exam). Allow the user to view the data in "chunks", asking him to press any key in order to see the next "chunk". At the end, print a statistics table, showing the average, lowest, and highest scores on each quiz/exam.
Assignment A-13.11
What is the output of the following program segment?
char*p; p = strchr ("this is a test",'s'); printf ("%s\n",p);
Assignment A-13.12
What is the output of the following program segment?
char*p; p = strstr ("this is a test","is"); printf ("%s\n",p);
Assignment A-13.13
What is the output of the following program segment?
int len; len = strspn ("this is a test","isht"); printf ("%d\n",len);
Assignment A-13.14
What is the output of the following program segment?
char*p; p = strtok ("The summer soldier, the sunshine patriot"," "); printf ("%s\n",p); do { p = strtok (NULL,","); if(p) printf ("%s\n",p); } while(p);
Assignment A-13.15
What is the output of the following program segment?
char s1[10]="abcdefghi"; char s2[10]="ace"; char s3[10]="defg"; char s4[10]; strcpy (s4, s2); strcat (s4, s3); strncat (s4, s1,sizeof(s4)-1- strlen (s2)); printf ("%s\n", s4);
Assignment A-13.16
What is the output of the following program segment?
int d1, d2, d3, d4; char s1[10]="abcdefghi"; char s2[10]="ace"; char s3[10]="defg"; d1 = strspn (s1, s2); d2 = strspn (s1, s3); d3 = strcspn (s1, s2); d4 = strcspn (s1, s3); printf ("%d %d %d %d\n", d1, d2, d3, d4);
Assignment A-13.17
What is the output of the following program?
int main() { struct gas { float distance; float gals; float mpg; } trip[3]={{1000.0,40.0,0},{400.0,15.0,0},{500.0,20.0,0}}; struct gas *ps = trip; int cnt =1; charadd[5]; while(ps <=&trip[2]) { ps -> mpg = ps -> distance / ps -> gals; switch(cnt) { case1: strcpy (add,"st"); break; case2: strcpy (add,"nd"); break; case3: strcpy (add,"rd"); } printf ("The %d%s calculated field is: %f\n", cnt,add, ps -> mpg); ps++; cnt++; } return0; }
Assignment A-13.18
Given the following declaration, what is the value of *x
, *(x+1)
,
and *(x+4)
?
char *x = "Life is beautiful"
Assignment A-19
Would the value of the integer dif
on the program segment below be positive, negative or zero?
char s1[] = "xyzt";
char *s2 = "xyAt";
int dif;
dif = strcmp (s1, s2);
Assignment A-13.20
What is the output of the following program segment?
char s1[50]="abcd"; char*s2 ="efghijklmno"; char*s3,*s4,*s5,*s6; s3 = s1; s4 = s2; strcat (s1, s2); s5 = strchr (s1,'d'); s6 = strrchr (s2,'n'); printf ("%s\n%s\n%s\n%s\n", s3, s4, s5, s6);
Assignment A-13.21
What is the output of the following program segment?
char*s1 ="uabefgnpanm"; char s2[]="ab"; char*s3 ="pan"; char s4[]="bef"; char*s5 ="panam"; char*s6,*s7,*s8,*s9; s6 = strstr (s1, s2); s7 = strstr (s1, s3); s8 = strstr (s1, s4); s9 = strstr (s1, s5); printf ("%s\n%s\n%s\n%s\n", s6, s7, s8, s9);
Assignment A-13.22
What is the output of the following program segment?
char*s1 ="abefgnpanm"; char s2[]="ab"; char*s3 ="pan"; char s4[]="bef"; char*s5 ="panam"; int d1, d2, d3, d4; d1 = strspn (s1, s2); d2 = strspn (s1, s3); d3 = strspn (s1, s4); d4 = strspn (s1, s5); printf ("%d\n%d\n%d\n%d\n", d1, d2, d3, d4);
Assignment A-13.23
What is the output of the following program segment?
char*a[5]={"cool","groovy","sweet", "dreading","awesome"}; printf ("%s\n", a[0]); printf ("%s\n",*(a+2)); printf ("%c\n",*(a[2]+2)); printf ("%s\n", a[3]); printf ("%c\n",*(*(a+3)+4));
Assignment A-13.24
Modify the following code to use a string for the password. Use the function getch( )
to keep the
password from showing up on the screen, instead type asterisks to the screen. You will need to read the characters into a character array, character by character.
int main() { int storedPass =11862; int numTries =0; int maxTries =3; int userPass; while(numTries < maxTries) { printf ("Enter the password: "); scanf ("%d",&userPass); numTries++; if(userPass == storedPass) { printf ("Welcome to the FBI file system.\n"); break; } else { printf ("Wrong password!"); if(numTries == maxTries) { printf ("Locked out!\n"); exit(0); } elseif(numTries ==(maxTries -1)) printf ("%d more try.\n", maxTries - numTries); else printf ("%d more tries.\n", maxTries - numTries); } } return0; }
Source: Kishori Mundargi, https://www.peoi.org/Courses/Coursesen/cprog/cprog13x.html This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.
Solutions
A 13.1
What is the difference between scanf( )
and gets( )
?
Answer: scanf( )
only reads a single word, it reads characters until it encounters white space;
gets( )
reads an entire string until '\n' is entered
A 13.2
Write a program that reads two command line arguments. The first one is a string and the second one a file name. The program then should search the file, printing all lines that contain the string. Use fgets( )
rather than get( )
. Hint: use the function
strstr( )
.
Answer:
int main()
{
char a[100], fileName[100], b[100];
FILE *fp;printf ("Enter some text: ");
gets (a);
printf ("Enter the name of the file: ");
gets (fileName);
fp = fopen (fileName, "r");if (fp == NULL)
{
printf("Error opening file.\n");
return 1;
}
else
{
while (!feof(fp))
{
fgets (b, sizeof (b), fp);
if (strstr (b,a) != NULL)
printf ("%s", b);
}
}fclose (fp);
return 0;
}
A 13.3
Find the error(s) in the following program segment;
int main ( )
{
int * fp;
int, k;
fp = fopen ("pizza");
for (k = 0; k < 30; k++)
printf ("Jill likes pizza.\n", fp);
fclose ("pizza");
return 0;
}
Answer:
int main ( )
{
FILE * fp;
int, k;
fp = fopen ("pizza"); // MISSING ACCESS CODE
for (k = 0; k < 30; k++)
fprintf (fp, "Jill likes pizza.\n");
fclose (fp);
return 0;
}
A 13.4
Suppose we have these statements in a program
FILE *fp1, *fp2;
char ch;
fp1 = fopen ("file1", "r");
fp2 = fopen ("file2", "w");
Assuming that both files opened successfully, supply the missing arguments in the following function calls:
a) fscanf (___, "%c", &ch);
Answer: fp1
b) fprintf (___, "%c\n", ch);
Answer: fp2
c) fclose (___); /* close the file for writing */
Answer: fp2
A 13.5
What is the output of the following program? Include in your answer the contents of any files created.
int main()
{
FILE *fp;
int k;
char ch;fp = fopen ("file.txt", "w");
if (fp == NULL)
{
printf("Error opening file.\n");
return 1;
}
else
{
for (k = 2; k < 20; k+=2)
fprintf (fp, "%3d", k);
fclose (fp);
fp = fopen ("file.txt", "r");while ((ch = fgetc(fp)) != EOF)
{
if (ch == ' ')
fputc ('*', stdout);
else if (ch != '1')
fputc (ch, stdout);
else
{
putchar ('\n');
putchar (ch);
}
}
}
return 0;
}
Answer:
Output Screen
**2**4**6**8*
10*
12*
14*
16*
18*
Output File
2 4 6 8 10 12 14 16 18
A 13.6
Based on the statement below:
fp = fopen ("names.dat", "w");
a) What is the statement to declare this file pointer?
Answer: FILE *fp;
b) Write a statement that writes the string "Rock my heart" to the file.
Answer: fprintf (fp, "Rock my heart");
c) Write a statement that closes the file.
Answer: fclose (fp);
A 13.7
With the following declarations:
double x;
int n;
char ch, str[40];
Indicate the contents of the variables after each of the following input operations are performed. Assume that the file accessed by fp consists of the data given and that each statement below occurs at the beginning of the program immediately after a statement that opens the file.
123 3.145 xyz<newline>35 z<newline>
a) fscanf (fp, "%d%lf%s%c", &n, &x, str, &ch);
R:
n = 123
x = 3.145
str = xyz
ch = '\n'
b) fscanf (fp, "%d%lf", &n, &x);
fscanf (fp, "%s %c", str, &ch);
R:
n = 123
x = 3.145
str = xyz
ch = 3
c) fscanf (fp, "%lf%d%c%s", &x, &n, &ch, str);
R:
n = 3
x = 123
str = 145
ch = '.'
d) Write the statement that opens this file.
answer:
FILE *fp;
fp = fopen (_______, r);
A 13.8
What does this program do?
int main()
{
FILE *fp;
char str[80];
fp = fopen ("file.txt", "w");if (fp == NULL)
{
printf("Error opening file.\n");
return 1;
}
do
{
printf ("Enter a string (ENTER to quit): ");
gets (str);
strcat(str, "\n");
fputs (str,fp);
}while (*str != '\n');
return 0;
}
Answer: Concatenates the string entered by the user with a <newline>. Writes str to the file until the user presses ENTER.
A 13.9
What does this program do? Modify it so that it does the same thing for a file rather than for what is printed to the screen.
#define STOP '|'
int main()
{
char c;
int n_chars, n_lines, n_words;
int wordflag = 0;
n_chars = n_lines = n_words = 0;
while ((c = getchar()) != STOP)
{
n_chars++;
if (c == '\n')
n_lines++;
if (c != ' ' && c != '\n' && c != '\t' && wordflag ==0)
{
wordflag = 1;
n_words++;
}if ((c == ' ' || c == '\n' || c == '\t') && wordflag ==1)
wordflag = 0;
}n_lines++;
printf ("characters = %d, words = %d, lines = %d\n", n_chars, n_words, n_lines);return 0;
}
R: Counts characters, words, and lines.
int main()
{
FILE *fp;
char c;
int n_chars, n_lines, n_words;
int wordflag = 0;fp = fopen ("file.txt", "r");
n_chars = n_lines = n_words = 0;while (!feof(fp))
{
c = getc(fp);
n_chars++;
if (c == '\n')
n_lines++;
if (c != ' ' && c != '\n' && c != '\t' && wordflag ==0)
{
wordflag = 1;
n_words++;
}if ((c == ' ' || c == '\n' || c == '\t') && wordflag ==1)
wordflag = 0;
}n_lines++;
printf ("characters = %d, words = %d, lines = %d\n", n_chars, n_words, n_lines);return 0;
}
A 13.10
Based on the file data.txt, create a program that reads the file and outputs its content to the screen, with the appropriate headers (ex: Name, ID, Quiz 1, Quiz 2, Quiz 3, Quiz 4, Exam). Allow the user to view the data in "chunks", asking him to press any key in order to see the next "chunk". At the end, print a statistics table, showing the average, lowest, and highest scores on each quiz/exam.
R:
#include <stdio.h>
#include <conio.h>typedef struct
{
char fname[10];
char lname[15];
int id;
int quiz1, quiz2, quiz3, quiz4, exam;
}STUDENT;
int findLow(STUDENT*, int, int);
int findHigh(STUDENT*, int, int);int main()
{
STUDENT students[50];
FILE *fpr;
int i = 0;
int cnt = 0;
int sum[5] = {0};
float avg[5] = {0};
int low[5] = {0};
int high[5] = {0};fpr = fopen("data.txt","r");
if (fpr == NULL)
{
printf("Error opening file!!\n");
}
else
{
printf("----- CLASS INFO -----\n\n");
printf(" Name\t\t ID\t Quiz1 Quiz2 Quiz3 Quiz4 Exam\n\n");while (!feof(fpr))
{
fscanf(fpr, "%s%s%d%d%d%d%d%d", &students[i].fname,
&students[i].lname, &students[i].id,
&students[i].quiz1, &students[i].quiz2,
&students[i].quiz3, &students[i].quiz4,
&students[i].exam);printf("%s %s\t %4d %3d %3d %3d %3d %3d\n",
students[i].fname, students[i].lname,
students[i].id, students[i].quiz1,
students[i].quiz2, students[i].quiz3,
students[i].quiz4, students[i].exam);i++;
cnt++;if ((i%16) == 0)
{
printf("\nPress any key to see the rest of the data\n\n");
getch();
}
}for (i = 0; i < cnt; i ++)
{
sum[0] += students[i].quiz1;
sum[1] += students[i].quiz2;
sum[2] += students[i].quiz3;
sum[3] += students[i].quiz4;
sum[4] += students[i].exam;
}
printf("\n\nSTATISTICS\n\n");
printf("\t\tQuiz1 Quiz2 Quiz3 Quiz4 Exam\n\n");
//AVERAGES
printf("Average:\t");
for (i = 0; i < 5; i ++)
{
avg[i] = ((float)sum[i] / cnt);
printf("%5.1f\t", avg[i]);
}printf("\n");
//LOWEST GRADES
printf("Lowest :\t");
for (i = 0; i < 5; i ++)
{
low[i] = findLow(students, i, cnt);
high[i] = findHigh(students, i, cnt);
}for (i = 0; i < 5; i ++)
{
printf("%3d\t", low[i]);
}
printf("\n");
//HIGHEST GRADES
printf("Highest:\t");
for (i = 0; i < 5; i ++)
{
printf("%3d\t", high[i]);
}
printf("\n");printf("\n----- END OF REPORT -----\n");
}
fclose(fpr);return 0;
}int findLow(STUDENT *stu, int x, int c)
{
int small;
int i;
if (x==0)
{
small = stu[0].quiz1;
for (i = 1; i < c; i ++)
{
if(small >= stu[i].quiz1 && stu[i].quiz1 > 0)
small = stu[i].quiz1;
}
return small;
}
else if (x==1)
{
small = stu[0].quiz2;
for (i = 1; i < c; i ++)
{
if(small >= stu[i].quiz2 && stu[i].quiz2 > 0)
small = stu[i].quiz2;
}
return small;
}
else if (x==2)
{
small = stu[0].quiz3;
for (i = 1; i < c; i ++)
{
if(small >= stu[i].quiz3 && stu[i].quiz3 > 0)
small = stu[i].quiz3;
}
return small;
}
else if (x==3)
{
small = stu[0].quiz4;
for (i = 1; i < c; i ++)
{
if(small >= stu[i].quiz4 && stu[i].quiz4 > 0)
small = stu[i].quiz4;
}
return small;
}
else if (x==4)
{
small = stu[0].exam;
for (i = 1; i < c; i ++)
{
if(small >= stu[i].exam && stu[i].exam > 0)
small = stu[i].exam;
}
return small;
}return 0;
}int findHigh(STUDENT *stu, int x, int c)
{
int big;
int i;
if (x==0)
{
big = stu[0].quiz1;
for (i = 1; i < c; i ++)
{
if(big <= stu[i].quiz1)
big = stu[i].quiz1;
}
return big;
}
else if (x==1)
{
big = stu[0].quiz2;
for (i = 1; i < c; i ++)
{
if(big <= stu[i].quiz2)
big = stu[i].quiz2;
}
return big;
}
else if (x==2)
{
big = stu[0].quiz3;
for (i = 1; i < c; i ++)
{
if(big <= stu[i].quiz3)big = stu[i].quiz3;
}
return big;
}
else if (x==3)
{
big = stu[0].quiz4;
for (i = 1; i < c; i ++)
{
if(big <= stu[i].quiz4)
big = stu[i].quiz4;
}
return big;
}
else if (x==4)
{
big = stu[0].exam;
for (i = 1; i < c; i ++)
{
if(big <= stu[i].exam)
big = stu[i].exam;
}
return big;
}return 0;
}
A 13.11
What is the output of the following program segment?
char *p;
p = strchr ("this is a test",'s');
printf ("%s\n",p);
R: s is a test
A 13.12
What is the output of the following program segment?
char *p;
p = strstr ("this is a test","is");
printf ("%s\n",p);
R: is is a test
A 13.13
What is the output of the following program segment?
int len;
len = strspn ("this is a test","isht");
printf ("%d\n",len);
R: 4
A 13.14
What is the output of the following program segment?
char *p;
p = strtok ("The summer soldier, the sunshine patriot"," ");
printf ("%s\n",p);do
{
p = strtok (NULL,",");
if (p)
printf ("%s\n",p);
}
while (p);
R: will print out all the words, one per line.
A 13.15
What is the output of the following program segment?
char s1[10] = "abcdefghi";
char s2[10] = "ace";
char s3[10] = "defg";
char s4[10];
strcpy (s4, s2);
strcat (s4, s3);
strncat (s4, s1, sizeof(s4) - 1 - strlen (s2));
printf ("%s\n", s4);
R: acedefgabcdef
A 13.16
What is the output of the following program segment?
int d1, d2, d3, d4;
char s1[10] = "abcdefghi";
char s2[10] = "ace";
char s3[10] = "defg";
d1 = strspn (s1, s2);
d2 = strspn (s1, s3);
d3 = strcspn (s1, s2);
d4 = strcspn (s1, s3);
printf ("%d %d %d %d\n", d1, d2, d3, d4);
R: 1 0 0 3
A 13.17
What is the output of the following program?
int main()
{
struct gas
{
float distance;
float gals;
float mpg;
}trip[3] = {{1000.0, 40.0, 0}, {400.0, 15.0, 0}, {500.0, 20.0, 0}};
struct gas *ps = trip;
int cnt = 1;
char add[5];while (ps <= &trip[2])
{
ps -> mpg = ps -> distance / ps -> gals;
switch (cnt)
{
case 1:
strcpy (add, "st");
break;
case 2:
strcpy (add, "nd");
break;
case 3:
strcpy (add, "rd");
}printf ("The %d%s calculated field is: %f\n", cnt, add, ps -> mpg);
ps++;
cnt++;
}return 0;
}
R:
The 1st calculated field is: 25.0
The 2nd calculated field is: 26.66
The 3rd calculated field is: 25.00
A 13.18
Given the following declaration, what is the value of *x, *(x+1), and *(x+4)?
char *x = "Life is beautiful"
R:
*x = L
*(x+1) = i
*(x+4) = <space>
A 13.19
Would the value of the integer dif on the program segment below be positive, negative or zero?
char s1[] = "xyzt";
char *s2 = "xyAt";
int dif;
dif = strcmp (s1, s2);
R: positive
A 13.20
What is the output of the following program segment?
char s1[50] = "abcd";
char *s2 = "efghijklmno";
char *s3, *s4, *s5, *s6;
s3 = s1;
s4 = s2;
strcat (s1, s2);
s5 = strchr (s1, 'd');
s6 = strrchr (s2, 'n');
printf ("%s\n%s\n%s\n%s\n", s3, s4, s5, s6);
R:
abcdefghijklmno
efghijklmno
defghijklmno
no
A 13.21
What is the output of the following program segment?
char *s1 = "uabefgnpanm";
char s2[] = "ab";
char *s3 = "pan";
char s4[] = "bef";
char *s5 = "panam";
char *s6, *s7, *s8, *s9;
s6 = strstr (s1, s2);
s7 = strstr (s1, s3);
s8 = strstr (s1, s4);
s9 = strstr (s1, s5);
printf ("%s\n%s\n%s\n%s\n", s6, s7, s8, s9);
R:
abefgnpanm
panm
befgnpanm
<null>
A 13.22
What is the output of the following program segment?
char *s1 = "abefgnpanm";
char s2[] = "ab";
char *s3 = "pan";
char s4[] = "bef";
char *s5 = "panam";
int d1, d2, d3, d4;
d1 = strspn (s1, s2);
d2 = strspn (s1, s3);
d3 = strspn (s1, s4);
d4 = strspn (s1, s5);
printf ("%d\n%d\n%d\n%d\n", d1, d2, d3, d4);
R:
2
1
0
1
A 13.23
What is the output of the following program segment?
char *a[5] = {"cool", "groovy", "sweet", "dreading", "awesome"};
printf ("%s\n", a[0]);
printf ("%s\n", *(a+2));
printf ("%c\n", *(a[2]+2));
printf ("%s\n", a[3]);
printf ("%c\n", *(*(a+3)+4));
R:
cool
sweet
e
dreading
d
A 13.24
Modify the following code to use a string for the password. Use the function getch( ) to keep the password from showing up on the screen, instead type asterisks to the screen. You will need to read the characters into a character array, character by character.
int main()
{
int storedPass = 11862;
int numTries = 0;
int maxTries = 3;
int userPass;while (numTries < maxTries)
{
printf ("Enter the password: ");
scanf ("%d", &userPass);
numTries++;if (userPass == storedPass)
{
printf ("Welcome to the FBI file system.\n");
break;
}
else
{
printf ("Wrong password!");if (numTries == maxTries)
{
printf ("Locked out!\n");
exit(0);
}
else if (numTries == (maxTries - 1))
printf ("%d more try.\n", maxTries - numTries);
else printf ("%d more tries.\n", maxTries - numTries);
}
}return 0;
}
R:
int main()
{
char storedPass[] = "123456";
char userPass[15], c;
int numTries = 0;
int maxTries = 3;
int cnt = 0;while (numTries < maxTries)
{
printf ("Enter the password: ");while (cnt < sizeof(userPass))
{
c = getch();
if (c != '\r') //user presses Enter
{
userPass[cnt] = c;
printf ("*");
cnt++;
}
else
break;
}
userPass[cnt] = '\0';
cnt = 0;
numTries++;
printf ("\n");if (strcmp(storedPass, userPass) == 0)
{
printf ("Welcome to the FBI file system.\n");
exit(0);
}
else
{
printf ("Wrong password! ");
if (numTries == maxTries)
{
printf ("Locked out!\n");
exit(0);
}
else if (numTries == (maxTries - 1)) printf ("%d more try.\n", maxTries - numTries);
else printf ("%d more tries.\n", maxTries - numTries);
}
}
return 0;
}