1. ## IF statement

i've got to be able to determine whether or not a number read in from a file is 5 digits or not. it can be from 00000 to 99999 in any order. as long as it is five digits.

if (number ??? && number ???)
{
}

what do i make the conditions for this?

2. Well 00000 is only going to equate to 1 digit if you convert it to an integer, so it's best to read in the number as ascii text, and then evaluate whether that text is 5 characters long.

Code:
`if(strlen(szNumber) == 5) { ... }`

3. Oh, and remember that szNumber needs to be zero terminated in order for the strlen() function to work.

4. ok, but the thing is.. i'm supposed to do it without strings. just old fashioned chars and ints..double, float.. but no string, struct, or anything

5. i tried this for each of the five numbers in the whole:

if (num_1 <= 1 && num_1 >= 9)
{
}

6. Just read the file one character at a time, then you can determine if it's a digit 0-9 or not and count up the number of consecutive digits

7. Look at the isdigit() function in ctype.h

8. Ok, but i still can't make it print out the right thing here...

#include <stdio.h>
#define FILE_NAME_SIZE 30

int get_course_record(FILE* infile, char* prefix, int* course_num, int* credits,
int* seats);

int get_student_record(FILE* infile, char* init_one, char* init_two, char* init_three, int* num_1, int* num_2, int* num_3, int* num_4, int* num_5);

int main(void)
{

char prefix;
int course_num, credits, seats, valid_status;

char init_one, init_two, init_three;
int num_1, num_2, num_3, num_4, num_5;

FILE *infile;

FILE *outfile;

char file_name[FILE_NAME_SIZE];

/************************************************** *******
(MAIN)COURSE SCHEDULE
************************************************** *******/

printf("please enter the name of the course schedule file");
scanf("%s", file_name);

infile = fopen(file_name, "r");

while (!feof (infile))

{

valid_status = get_course_record(infile, &prefix, &course_num, &credits,
&seats);
if (valid_status == 0)
{
printf("\ninvalid record\n");
}
else if (valid_status == 1)
{
printf("\n\n%c\n%d\n%d\n%d\n", prefix, course_num, credits, seats);
}

}
fclose(infile);
system("pause");

/************************************************** *****************************
(MAIN)STUDENT RECORDS
************************************************** *****************************/

printf("please enter the name of the student record file");
scanf("%s", file_name);

infile = fopen(file_name, "r");

while (!feof (infile))

{

valid_status = get_student_record(infile, &init_one, &init_two, &init_three, &num_1, &num_2, &num_3, &num_4, &num_5);

if (valid_status == 1)
{

printf("\n\n%c\n%c\n%c\n%d%d%d%d%d\n", &init_one, &init_two, &init_three, &num_1, &num_2, &num_3, &num_4, &num_5);

}
else if (valid_status == 0)
{
printf("\ninvalid record\n");
}

}
system("pause");
fclose(infile);
}

/************************************************** *****************************
(FUNC) COURSE SCHEDULE
************************************************** *****************************/

int get_course_record(FILE* infile, char* prefix, int* course_num, int* credits,
int* seats)
{
char endl;
int valid_status = 1;
if( fscanf(infile, "%s%d%d%d%c", prefix, course_num, credits, seats, &endl))
{

if (!(*prefix >= 'a' && *prefix <= 'z'))
{
valid_status = 0;
}

if (!(*course_num >= 101 && *course_num <= 499))
{
valid_status = 0;
}

if (!(*credits >= 1 && *credits <= 6))
{
valid_status = 0;
}

if (!(*seats >= 5 && *seats <= 50))
{
valid_status = 0;
}

}
else
{
printf("invalid record\n");
valid_status = 0;
}
return valid_status;
}

/************************************************** *****************************
(FUNC) STUDENT RECORDS
************************************************** *****************************/

int get_student_record(FILE* infile, char* init_one, char* init_two, char* init_three, int* num_1, int* num_2, int* num_3, int* num_4, int* num_5)
{
char endl;
int sr_count;
int valid_status = 1;
printf("%c%c%c\n%d\n", *init_one, *init_two, *init_three, *num_1, *num_2, *num_3, *num_3, *num_4, *num_5);

if( fscanf(infile, "%c%c%c%d%d%d%d%d%c", init_one, init_two, init_three, num_1, num_2, num_3, num_4, num_5, &endl))
{
if (!(*init_one >= 'A' && *init_one <= 'Z'))
{
(sr_count)++;
}
if (!(*init_two >= 'A' && *init_one <= 'Z'))
{
(sr_count)++;
}
if (!(*init_three >= 'A' && *init_one <= 'Z'))
{
(sr_count)++;
}

if (!(*num_1 >= 0 && *num_1 <= 9))
{
(sr_count)++;
}
if (!(num_2 >= 0 && *num_2 <= 9))
{
(sr_count)++;
}
if (!(*num_3 >= 0 && *num_3 <= 9))
{
(sr_count)++;
}
if (!(*num_4 >= 0 && *num_4 <= 9))
{
(sr_count)++;
}
if (!(*num_5 >= 0 && *num_5 <= 9))
{
(sr_count)++;
}
if(sr_count = 5)
{
valid_status = 1;
}
}
else
{
valid_status = 0;
}
return valid_status;
}

and its all supposed to be in C not using strings, arrays, or anything.

9. Well, it's going to be pretty difficult to get a filename from the user if you can't use strings. Your current method of getting the filename is using a string...In fact, fopen() requires that you pass a string to it, so it's impossible not to use strings in your program. So either your professor gave you an impossible assignment or you're putting unnecessary constraints on the assignment.

Once you get the filename, you can just use fgetc() on the file stream 5 times, each time checking to see if the character is a digit. If all 5 are digits then try fgetc() one more time. If that one is a digit then the number isn't 5 digits either.

10. brabner-

11. sorry. will do.

by not using strings i mean, except that one case, that he gave us the code for. other than that, no strings. we haven't covered them yet.

12. i changed it a little, but it's still returning garbage.

Code:
```
#include <stdio.h>
#define FILE_NAME_SIZE 30

int get_course_record(FILE* infile, char* prefix, int* course_num, int* credits,
int* seats);

int get_student_record(FILE* infile, char* init_one, char* init_two, char* init_three,  int* sr_num);

int main(void)
{

char prefix;
int course_num, credits, seats, valid_status;

char init_one, init_two, init_three;
int sr_num;

FILE *infile;

FILE *outfile;

char file_name[FILE_NAME_SIZE];

/*********************************************************
(MAIN)COURSE SCHEDULE
*********************************************************/

printf("please enter the name of the course schedule file");
scanf("%s", file_name);

infile = fopen(file_name, "r");

while (!feof (infile))

{

valid_status = get_course_record(infile, &prefix, &course_num, &credits,
&seats);
if (valid_status == 0)
{
printf("\ninvalid record\n");
}
else if (valid_status == 1)
{
printf("\n\n%c\n%d\n%d\n%d\n", prefix, course_num, credits, seats);
}

}
fclose(infile);
system("pause");

/*******************************************************************************
(MAIN)STUDENT RECORDS
*******************************************************************************/

printf("please enter the name of the student record file");
scanf("%s", file_name);

infile = fopen(file_name, "r");

while (!feof (infile))

{

valid_status = get_student_record(infile, &init_one, &init_two, &init_three, &sr_num);

if (valid_status == 1)
{

printf("%c %c %c %d", &init_one, &init_two, &init_three, &sr_num);

}
else if (valid_status == 0)
{
printf("\ninvalid record\n");
}

}
system("pause");
fclose(infile);
}

/*******************************************************************************
(FUNC) COURSE SCHEDULE
*******************************************************************************/

int get_course_record(FILE* infile, char* prefix, int* course_num, int* credits,
int* seats)
{
char endl;
int valid_status = 1;
if( fscanf(infile, "%c%d%d%d%c", prefix, course_num, credits, seats, &endl))
{

if (!(*prefix >= 'a' && *prefix <= 'z'))
{
valid_status = 0;
}

if (!(*course_num >= 101 && *course_num <= 499))
{
valid_status = 0;
}

if (!(*credits >= 1 && *credits <= 6))
{
valid_status = 0;
}

if (!(*seats >= 5 && *seats <= 50))
{
valid_status = 0;
}

}
else
{
printf("invalid record\n");
valid_status = 0;
}
return valid_status;
}

/*******************************************************************************
(FUNC) STUDENT RECORDS
*******************************************************************************/

int get_student_record(FILE* infile, char* init_one, char* init_two, char* init_three, int* sr_num)
{
char endl;
int sr_count;
int valid_status = 1;

if( fscanf(infile, "%c%c%c%d%c", init_one, init_two, init_three, sr_num, &endl))
{
if (!(*init_one >= 'A' && *init_one <= 'Z'))
{
valid_status = 0;
}
if (!(*init_two >= 'A' && *init_one <= 'Z'))
{
valid_status = 0;
}
if (!(*init_three >= 'A' && *init_one <= 'Z'))
{
valid_status = 0;
}

if(!(*sr_num >= 10000 && *sr_num <= 99999))
{
valid_status = 0;
}
}
else
{
valid_status = 1;
}
return valid_status;
}```

13. its returning this phantom
c
b
a
invalid

theres no cba in my program ....

14. Code:
```#include <stdio.h>
#define FILE_NAME_SIZE 30

int get_course_record(FILE* infile, char* prefix, int* course_num, int* credits,
int* seats);

int get_student_record(FILE* infile, char* init_one, char* init_two, char* init_three,  int* num_1, int* num_2, int* num_3, int* num_4, int* num_5);

int main(void)
{

char prefix;
int course_num, credits, seats, valid_status;

char init_one, init_two, init_three;
int num_1, num_2, num_3, num_4, num_5;

FILE *infile;

FILE *outfile;

char file_name[FILE_NAME_SIZE];

/*********************************************************
(MAIN)COURSE SCHEDULE
*********************************************************/

printf("please enter the name of the course schedule file");
scanf("%s", file_name);

infile = fopen(file_name, "r");

while (!feof (infile))

{

valid_status = get_course_record(infile, &prefix, &course_num, &credits,
&seats);
if (valid_status == 0)
{
printf("\ninvalid record\n");
}
else if (valid_status == 1)
{
printf("\n\n%c\n%d\n%d\n%d\n", prefix, course_num, credits, seats);
}

}
fclose(infile);
system("pause");

/*******************************************************************************
(MAIN)STUDENT RECORDS
*******************************************************************************/

printf("please enter the name of the student record file");
scanf("%s", file_name);

infile = fopen(file_name, "r");

while (!feof (infile))

{

valid_status = get_student_record(infile, &init_one, &init_two, &init_three, &num_1, &num_2, &num_3, &num_4, &num_5);

if (valid_status == 1)
{

printf("\n\n%c\n%c\n%c\n%d%d%d%d%d\n", &init_one, &init_two, &init_three, &num_1, &num_2, &num_3, &num_4, &num_5);

}
else if (valid_status == 0)
{
printf("\ninvalid record\n");
}

}
system("pause");
fclose(infile);
}

/*******************************************************************************
(FUNC) COURSE SCHEDULE
*******************************************************************************/

int get_course_record(FILE* infile, char* prefix, int* course_num, int* credits,
int* seats)
{
char endl;
int valid_status = 1;
if( fscanf(infile, "%s%d%d%d%c", prefix, course_num, credits, seats, &endl))
{

if (!(*prefix >= 'a' && *prefix <= 'z'))
{
valid_status = 0;
}

if (!(*course_num >= 101 && *course_num <= 499))
{
valid_status = 0;
}

if (!(*credits >= 1 && *credits <= 6))
{
valid_status = 0;
}

if (!(*seats >= 5 && *seats <= 50))
{
valid_status = 0;
}

}
else
{
printf("invalid record\n");
valid_status = 0;
}
return valid_status;
}

/*******************************************************************************
(FUNC) STUDENT RECORDS
*******************************************************************************/

int get_student_record(FILE* infile, char* init_one, char* init_two, char* init_three, int* num_1, int* num_2, int* num_3, int* num_4, int* num_5)
{
char endl;
int sr_count;
int valid_status = 1;
printf("%c%c%c\n%d\n", *init_one, *init_two, *init_three, *num_1, *num_2, *num_3, *num_3, *num_4, *num_5);

if( fscanf(infile, "%c%c%c%d%d%d%d%d%c", init_one, init_two, init_three, num_1, num_2, num_3, num_4, num_5, &endl))
{
if (!(*init_one >= 'A' && *init_one <= 'Z'))
{
(sr_count)++;
}
if (!(*init_two >= 'A' && *init_one <= 'Z'))
{
(sr_count)++;
}
if (!(*init_three >= 'A' && *init_one <= 'Z'))
{
(sr_count)++;
}

if (!(*num_1 >= 0 && *num_1 <= 9))
{
(sr_count)++;
}
if (!(num_2 >= 0 && *num_2 <= 9))
{
(sr_count)++;
}
if (!(*num_3 >= 0 && *num_3 <= 9))
{
(sr_count)++;
}
if (!(*num_4 >= 0 && *num_4 <= 9))
{
(sr_count)++;
}
if (!(*num_5 >= 0 && *num_5 <= 9))
{
(sr_count)++;
}
if(sr_count = 5)
{
valid_status = 1;
}
}
else
{
valid_status = 0;
}
return valid_status;
}```

Code:
```itsme@itsme:~/C\$ cat is5digits.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main(void)
{
char filename[100];
FILE *fp;
int i;
int valid = 1;

printf("Enter filename: ");
fflush(stdout);
fgets(filename, sizeof(filename), stdin);
filename[strlen(filename)-1] = '\0';

if(!(fp = fopen(filename, "r")))
{
printf("Couldn't open '%s' for reading!\n", filename);
return EXIT_FAILURE;
}

for(i = 0;i < 5;++i)
{
if(!isdigit(fgetc(fp)))
{
valid = 0;
break;
}
}

if(valid && isdigit(fgetc(fp)))
valid = 0;

puts(valid ? "Valid" : "Invalid");

return EXIT_SUCCESS;
}
itsme@itsme:~/C\$ cat 5digits.txt
03479
itsme@itsme:~/C\$ ./is5digits
Enter filename: 5digits.txt
Valid
itsme@itsme:~/C\$```