c program(multi-character character constant)

Discussion in 'Programmer's Corner' started by TAKYMOUNIR, Oct 30, 2012.

  1. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    Code ( (Unknown Language)):
    1.   #include<stdio.h>
    2. #include<ctype.h>
    3. void work(void);
    4.   double fah_2_cels(double fahr );
    5.   double cels_2_fahr(double cels);
    6.   int main(void)
    7.   {
    8.     char more;
    9.     puts("\n enter the temperature followed by f for fahr and c for cels");
    10.     do{
    11.       work();
    12.       puts("\n do you want to continue(Y/N)?");
    13.       scanf("%c" ,&more);
    14.       more =toupper(more);
    15.     }while(more!='N');
    16.     return 0;
    17.   }
    18.   void work(void)
    19.   {
    20.     double temp;
    21.     //char ch[6];
    22.     double num,fahr,cels;
    23.     printf("enter the temperature");
    24.     scanf("%lg %c",&num,&num);
    25.     if (num='70')
    26.     {
    27.       temp=fah_2_cels(num);
    28.       printf("temperature in cel is %g",temp);
    29.  }
    30.     else
    31.       temp=cels_2_fahr(num);
    32.       printf("temperature in fahr is %g",temp);
    33.   }//closes void work()
    34.   double fah_2_cels(double fahr)
    35.   {
    36.   return ((5.0/9.0)*(fahr-32.0));
    37.   }
    38.   double cels_2_fahr(double cels)
    39.   {
    40.     return ((9.0/5.0)*cels +32.0); //If you find that the results are off look at these functions
    41.   }
    42.  
    this program is working ok but it give me warninig (multi character character constant in line 12 ) how can i fix this
    thanks
     
    Last edited: Oct 31, 2012
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,873
    996
    What is the line causing the error? Which line is 12???
     
    TAKYMOUNIR likes this.
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,873
    996
    This is probably the line

    *ptr <='127' ||*ptr >= '0')

    I am guessing you want

    *ptr <=127 ||*ptr >= 0)
     
    TAKYMOUNIR likes this.
  4. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    thanks very much it works perfect now ,i have another question how can i do this program without using pointers
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Just use what ptr points to: the text[] array.
     
    TAKYMOUNIR likes this.
  6. WBahn

    Moderator

    Mar 31, 2012
    17,731
    4,789
    I don't see this in the code at all.

    But I do see this:

    You have told scanf that you will supply the addresses of a double and a char. Yet you supply the addresses of two doubles. Bad karma.

    On top of that, you supply the same address for both. This invokes unspecified behavior (I'm pretty sure). Do YOU know which value ends up in num? Why that one and not the other one?

    Then, you are comparing num (a variable of type double) to the constant value '70'. But what the heck is the value '70'? '7' would be the ASCII code (assuming your implementation used ASCII) for the character that looks like a 7. But there is no ASCII code for the character that looks like a 70 because there is no such character. You have a character constant that has more than one character. The compiler is probably using the first character and throwing a warning.
     
  7. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,873
    996
    OP has already solved the issue and has updated the OP for some reason. Why respond with an answer when the OP has solved the issue?
     
  9. WBahn

    Moderator

    Mar 31, 2012
    17,731
    4,789
    I don't see where the issue I was talking about has been solved at all. If that code is now "running perfectly" it is only by pure luck because of the issues I raised that I don't see mentioned anywhere else in this thread.

    Now, after posting my response pointing out things that are NOT pointed out in this thread, someone points out that the OP has posted this same code in some other thread and that the issues I raised are discussed in that thread.

    How the hell am I supposed to know that?!

    I guess since there's always the chance that any issue has already been solved in some other thread, I should just not bother providing any answers at all.

    Fine. Goodbye.
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,873
    996

    Yeah sorry really the OPs fault. Ge did mention in the post above that the issue is solved.

    I wonder sometime, after reading some of these post like the OP's , if they have ever used a forum before. Rules are pretty much the same anywhere not to mention common sense.
     
  11. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
    need a ranking or reputation system so each post (question or reply) can be graded.
     
  12. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,873
    996
    Agreed. While stackoverflow bites the bit one as far as usability is concerned they do have sort of a ranking.

    The other nice feature they have is to mark a post as the answer.


    Instead of number of posts to post in certain forums, you should have to have a certain ranking to earn the privilege.

    Oh and you should get points knocked off bug tie for not using punctuation or capitalizing sentences. :)
     
Loading...