Thread: Checking parts of a structure

  1. #1
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Question Checking parts of a structure

    I am wondering is it possable to check what the values of a structure are with if statements? I have a strcuture that has three different values in it, and I would like to check each one individually if possable. When I try to do it with what I thought the if statement should be, I am getting an incompatability issue with type. I want to do some simple value checking to ensure that the values in the structure meet my requirement. But perhaps I am thinking about doing this the wrong way. Can someone shed some light on this?

    Thanks much,
    DD
    "aut vincere aut mori"

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    What you are trying to do is right. Just remember to use strcmp() with strings.

    If you'd prefer a straight answer as to why your code isn't working, then you'd need to post it.

  3. #3
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32
    Well my current code is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
    typedef struct
    	{
    	int hr;
    	int min;
    	int sec;
    	} TIME;
    
    TIME sTime;
    TIME fTime;
    TIME tTime;
    
    printf("Calculate total time between start and finish. Format for data is hh:mm:ss\n");
    
    printf("\nEnter a start time: ");
    scanf("%d:%d:%d", &sTime.hr, &sTime.min, &sTime.sec);
    
    printf("\nEnter a finish time: ");
    scanf("%d:%d:%d", &fTime.hr, &fTime.min, &fTime.sec);
    
    tTime.hr = fTime.hr - sTime.hr;
    tTime.min = fTime.min - sTime.min;
    tTime.sec = fTime.sec - sTime.sec;
    
    printf("\n\n\nThe total time:  %2d hr(s),%2d min(s), and%2d sec(s)\n", tTime);
    		
    }
    Which works, but I am trying to clear up some data entry problems so that no matter what the user enters it will give the suer the correct information. My first step is to fix the problem with the fact that if they enter 12:01:01 as a strt time and end time of 01:01:01 the get a -11 total time instead of 1.

    What I was thinking I would like to do is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
    typedef struct
    	{
    	int hr;
    	int min;
    	int sec;
    	} TIME;
    
    TIME sTime;
    TIME fTime;
    TIME tTime;
    
    printf("Calculate total time between start and finish. Format for data is hh:mm:ss\n");
    
    printf("\nEnter a start time: ");
    scanf("%d:%d:%d", &sTime.hr, &sTime.min, &sTime.sec);
    
    printf("\nEnter a finish time: ");
    scanf("%d:%d:%d", &fTime.hr, &fTime.min, &fTime.sec);
    
    tTime.hr = fTime.hr - sTime.hr;
    tTime.min = fTime.min - sTime.min;
    tTime.sec = fTime.sec - sTime.sec;
    if tTime.hr < 0
    	tTime.hr = tTime.hr + 12
    
    printf("\n\n\nThe total time:  %2d hr(s),%2d min(s), and%2d sec(s)\n", tTime);
    		
    }
    But I get a syntax error : identifier 'tTime'. I am still trying to get a grip on structures, but am confused as to exactly how they work I guess. I figure once I get this down, I can do some similar stuff to ensure minute and seconds are not >60 and that everything is numeric and not an alpha character. Any help is appreciated.


    Thanks,
    DD
    "aut vincere aut mori"

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps
    if ( tTime.hr < 0 )

    Gotta remember the ()

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Code:
    if (tTime.hr < 0)
         tTime.hr += 12;

  6. #6
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Talking Thanks

    I swear I would forget my head if I did not keep it attached most days. Well this is why I keep practicing, so perhaps I will get to a point to remember this kind of stuff.

    Thanks guys I appreciate it. Now I will see if I can get the rest of the pieces to work.

    DD

    "aut vincere aut mori"

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Tip:
    Compile often - like every few lines.
    If you get some errors, the problem is almost certainly in the few lines you just added, and therefore much easier to figure out where to start looking for a problem.

  8. #8
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Question Something new added

    OK I have my first bit of error checking working, I can ensure users are no entering numbers that are to high for a clock. But I am trying to also ensure they do not enter data that is a letter. I am trying to use isalpha, assuming that if they do enter a letter isalpha will be a true. My code is compiling without error, but when I try to run it no matter what I do I get exited out. So I am thinking something in my arguments logic is off in this case. Because it seems that my isalpha statement is catching everything, not just the letters as I hoped.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <conio.h>
    
    int main (void)
    {
    typedef struct
    	{
    	int hr;
    	int min;
    	int sec;
    	} TIME;
    
    TIME sTime;
    TIME fTime;
    TIME tTime;
    
    printf("Calculate total time between start and finish. Format for data is hh:mm:ss\n");
    
    printf("\nEnter a start time: ");
    scanf("%d:%d:%d", &sTime.hr, &sTime.min, &sTime.sec);
    
    if (sTime.hr >24 || sTime.min >60 || sTime.sec >60)
    		{
    		printf("\aInvalid Option\n");
    		printf("\nPress any key to return to the main menu.\n");
    		getch();
    		return 0;
     		}
    
    isalpha (sTime.hr || sTime.min || sTime.sec);
    		{
    		printf("\aInvalid Option\n");
    		printf("\nPress any key to return to the main menu.\n");
    		getch();
    		return 0;
     		}
    
    printf("\nEnter a finish time: ");
    scanf("%d:%d:%d", &fTime.hr, &fTime.min, &fTime.sec);
    
    if (fTime.hr >24 || fTime.min >60 || fTime.sec >60)
    		{
    		printf("\aInvalid Option\n");
    		printf("\nPress any key to exit.\n");
    		getch();
    		return 0;
     		}
    
    tTime.hr = fTime.hr - sTime.hr;
    tTime.min = fTime.min - sTime.min;
    tTime.sec = fTime.sec - sTime.sec;
    if (tTime.hr < 0)
    	tTime.hr += 12;
    
    printf("\n\n\nThe total time:  %2d hr(s),%2d min(s), and%2d sec(s)\n", tTime);
    
    return 0;		
    }
    Anyone able to tell me where I am off on this? or able to help me fix it?

    Thanks,
    DD
    "aut vincere aut mori"

  9. #9
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    look at the flow of your code there. is there an if statement? no, then it will always step into the "invalid option" code.
    hello, internet!

  10. #10
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Question OK, add the if

    Thanks moi, I have added an if, and corrected the problem with if defaulting to that isalpha option. I also thought I set the is option to run if it is true by adding !=0 after each variable in the structure, but now it does not catch this option at all.

    Current code is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <conio.h>
    
    int main (void)
    {
    typedef struct
    	{
    	int hr;
    	int min;
    	int sec;
    	} TIME;
    
    TIME sTime;
    TIME fTime;
    TIME tTime;
    
    printf("Calculate total time between start and finish. Format for data is hh:mm:ss\n");
    
    printf("\nEnter a start time: ");
    scanf("%d:%d:%d", &sTime.hr, &sTime.min, &sTime.sec);
    
    if (sTime.hr >24 || sTime.min >60 || sTime.sec >60)
    		{
    		printf("\aInvalid Option\n");
    		printf("\nPress any key to return to exit.\n");
    		getch();
    		return 0;
     		}
    
    if (isalpha (sTime.hr !=0 || sTime.min !=0  || sTime.sec !=0))
    		{
    		printf("\aInvalid Option\n");
    		printf("\nPress any key to return to exit.\n");
    		getch();
    		return 0;
     		}
    
    printf("\nEnter a finish time: ");
    scanf("%d:%d:%d", &fTime.hr, &fTime.min, &fTime.sec);
    
    if (fTime.hr >24 || fTime.min >60 || fTime.sec >60)
    		{
    		/* Inform the user */
    		printf("\aInvalid Option\n");
    		printf("\nPress any key to exit.\n");
    		getch();
    		return 0;
     		}
    
    tTime.hr = fTime.hr - sTime.hr;
    tTime.min = fTime.min - sTime.min;
    tTime.sec = fTime.sec - sTime.sec;
    if (tTime.hr < 0)
    	tTime.hr += 12;
    
    printf("\n\n\nThe total time:  %2d hr(s),%2d min(s), and%2d sec(s)\n", tTime);
    
    return 0;		
    }
    So I have the if statement, but am still not getting the isalpha to work right. I want it to take this path if the user enters a letter instead of a number. Where else did I go wrong in this logic?

    Thanks,
    DD
    "aut vincere aut mori"

  11. #11
    Sayeh
    Guest
    You're trying to condense it to much. isalpha() performs the comparison, but only on one thing at a time. Your boolean and tests just aren't right--

    Code:
    if (isalpha (sTime.hr !=0 || sTime.min !=0  || sTime.sec !=0))
    Try this instead:

    Code:
    if(isalpha(sTime.hr) || isalpha(sTime.min) || isalpha(sTime.sec))

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Also, see the answers here.

    Note that in the example code, you used scanf() to get a digit (%d), then you used isalpha() to check if the digit was a number. A pointless, imo! The program design needs correcting.

    And again, always check the return code from scanf()!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Reference to a Structure
    By boschow in forum C Programming
    Replies: 3
    Last Post: 03-28-2008, 02:37 PM
  3. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  4. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM