please tell me error in this proogram

+3 Deepak Panday · November 24, 2015
#include<stdio.h>
#include<conio.h>

main()
{

   float a;
   char b[3],c[]="am",d[]="pm";
   printf("enter the time=");
   scanf("%f",&a);
   printf("enter am or pm=");
  scanf("%s",b);
   if(b==c||b==d)
   {
       if(a<12.60&&a>=1&&b==c)
      {
          printf("good morning");
      }

       else if(a>=12&&a<=12.60||a>=1&&a<=3.60&&b==d)
    {
        printf("good after noon");
    }

       else if(a>=4&&a<=6.60&&b==d)
     {
         printf("good evening");
     }

       else

      {
          printf("good night");
      }
   }

   else
   {
       printf("time invlaid");
       }
   getch();

}




in this program after i enter am or pm it always give time invalid output....i don't know why it not checking the condition....have i entered the wrong string syntax?

Post a Reply

Replies

Oldest  Newest  Rating
0 Deepak Panday · November 28, 2015
no bro i am not saying to do my work...what i want is that...i write this program in other way.....in which one program work and other not....please take a look at replay 6...u will know it what i mean
+1 Dan P · November 28, 2015
Hi Deepak,

Can I suggest that you do study it then?

We have shown you a good way to implement it, and now you need to go through each part and do
your own research to understand why. You are quite welcome to ask us about each part, but we cannot
do the work for you.

Dan
0 Deepak Panday · November 28, 2015
i haven't studied ctypre and string data type....also i don't know what enum means here...
0 c student · November 28, 2015
perhaps you should review the language.  it might also help if you used some kind of data structure to hold the units of time instead of using a floating point variable since it's pretty hacky and isn't reliable.  same goes for your storing of the meridian.

here is my code.  you can copy it if you want but if you don't understand it then there's no point.
Spoiler:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

enum meridian {
    AM,
    PM
};

// data structure to hold time information
struct time {
    int hour;
    int minutes;
    enum meridian m;
};

// initializer for time structure
struct time initTime (void) {
    struct time t;
    t.hour = 0;
    t.minutes = 0;
    t.m = AM;
    
    return t;
}

// error message
void fatal (char *s) {
fprintf (stderr, "%s\n", s);
exit (EXIT_FAILURE);
}

// function to print time
void printTime (struct time t) {
    if (t.minutes < 10) {
        printf ("The time is: %d:0%d ", t.hour, t.minutes);
    } else {
        printf ("The time is: %d:%d ", t.hour, t.minutes);
    }
    
    if (t.m == AM) {
        printf ("AM\n");
    } else if (t.m == PM) {
        printf ("PM\n");
    } else {
        fatal ("Meridian error");
    }
}

int main (void) {
    struct time Time = initTime();
    char mer[3] = {0};    // to hold "am" or "pm"
    
    // repeat scan until correct format
    do {
        printf ("Enter the time (HH:MM AM/PM): ");
    } while (scanf ("%d%*c%d%*c%s", &Time.hour, &Time.minutes, mer) != 3);
    putchar ('\n');
    
    // error checking for correct input
    if (Time.hour < 1 | Time.hour > 12) {
        fatal ("Incorrect hour");
    } else if (Time.minutes < 0 | Time.minutes > 59) {
       fatal ("Incorrect minutes");
    }
    
    mer[0] = tolower (mer[0]);
    mer[1] = tolower (mer[1]);
    
    // get am or pm
    if (strncmp (mer, "am", 2) == 0) {
        Time.m = AM;
    } else if (strncmp (mer, "pm", 2) == 0) {
        Time.m = PM;
    } else {
        fatal ("Incorrect meridian");
    }
    
    // print time
    printTime (Time);
    
    return EXIT_SUCCESS;
}

0 Deepak Panday · November 28, 2015
THIS PROGRAM GIVES THE CORRECT OUTPUT-
#include<stdio.h>
#include<conio.h>

main()
{

    float a;
    char b;
    printf("enter the time=");
    scanf("%f",&a);
    printf("enter am or pm");
   scanf("%c",&b);
   if(b=='a'||b=='p')
    {
        if(a<12.60&&a>=1&&b=='a')
       {
           printf("good morning");
       }

        else if(a>=12&&a<=12.60||a>=1&&a<=3.60&&b=='p')
     {
         printf("good after noon");
     }

        else if(a>=4&&a<=6.60&&b=='p')
      {
          printf("good evening");
      }

        else

       {
           printf("good night");
       }
    }

    else
    {
        printf("time invlaid");
        }
    getch();

}


BUT THIS PROGRAM DO NOT GIVE CORRECT OUTPUT....IT ALWAYS GIVES TIME INAVLID-

#include<stdio.h>
#include<conio.h>

main()
{

    float a;
    char b;
    printf("enter the time with am or pm=");
    scanf("%f%c",&a,&b);
    if(b=='a'||b=='p')
    {
        if(a<12.60&&a>=1&&b=='a')
       {
           printf("good morning");
       }

        else if(a>=12&&a<=12.60||a>=1&&a<=3.60&&b=='p')
     {
         printf("good after noon");
     }

        else if(a>=4&&a<=6.60&&b=='p')
      {
          printf("good evening");
      }

        else

       {
           printf("good night");
       }
    }

    else
    {
        printf("time invlaid");
        }
    getch();

}


WHY IS THAT Mr.Dan THE ONLY DIFFERENCE IS HOW I AM ENTERENIG CHARACTER.....so please tell me why the second proegram is not giving the output
0 Deepak Panday · November 27, 2015
the think is that i am still a learner of c , and i haven't yet studies control string of %u...so i can't tell the error above....
and yes the shorter the programmming code the better it is...
still according to me the error that the code u gvn me is am=1....so it can not compare in if else condition..
+1 Dan P · November 27, 2015
Hi Deepak,

Of course, there are lots of ways to write code.

For instance, as you know that it can only be am or pm you could say....

#include 

int main() {
        enum amOrPm {am=1, pm} amOrPm;

        printf("Enter am or pm\n1)am\n2)pm\n");
        scanf("%u", &amOrPm);

        if(amOrPm == am) {
                printf("Am\n");
        }
        else {
                printf("Pm\n");
        }
}



Try to make your code easy to read and you will be able to see the errors more easily.

You will also be able to think about what you could optimise in the future if that is something that you needed to do, because
the code will be clear.

Now my code still has some issues in it, it would be a good idea for you to review the code and see if you can find those problems and
come up with ways to fix them.

Dan
0 Deepak Panday · November 27, 2015
thank u for ur replay Mr. Dan P ....so is there any other way of doing this program...?
+2 Dan P · November 25, 2015
Hi Deepak,

You cannot compare strings with ==.

This is because C doesn't have a string type, it has the char type which is a single character.

You can create array of type char which represents a string.

When you are comparing b == c you are comparing the memory address of b and c.
For further explanation of this, you should look at what and how arrays are defined, and also into pointers. 

You can use the function strcmp() to compare 2 strings.

Dan
  • 1

C

107,203 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator