IF statement

This is a discussion on IF statement within the C Programming forums, part of the General Programming Boards category; i've got to be able to determine whether or not a number read in from a file is 5 digits ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    10

    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. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Oh, and remember that szNumber needs to be zero terminated in order for the strlen() function to work.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    10
    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. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    10
    i tried this for each of the five numbers in the whole:

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

  6. #6
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    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. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Look at the isdigit() function in ctype.h
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    10
    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. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    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.
    Last edited by itsme86; 11-04-2004 at 10:10 PM.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    brabner-
    please read this link << !! Posting Code? Read this First !! >> and use code tags whenever you post code. It makes things much much easier to read and understand.

  11. #11
    Registered User
    Join Date
    Nov 2004
    Posts
    10
    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. #12
    Registered User
    Join Date
    Nov 2004
    Posts
    10
    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. #13
    Registered User
    Join Date
    Nov 2004
    Posts
    10
    its returning this phantom
    c
    b
    a
    invalid


    theres no cba in my program ....

  14. #14
    Registered User
    Join Date
    Nov 2004
    Posts
    10
    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;
    }

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Your program is so overly complex...you didn't follow my isdigit() or my fgetc() advice:
    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$
    If you understand what you're doing, you're not learning anything.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 06:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 01:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21