Thread: newbie help with "if" and "char"

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Question newbie help with "if" and "char"

    Hi,
    I am very new to C, so I apologise if this is a really stupid question.
    I'm trying to read a character input from the keyboard (F or C) and if the response is F calculate from Fahrenheit to Celcius, else calculate from Celcius to Fahrenheit.
    But I keep getting an error (using Visual Studio 2005)
    Code:
    error C2065: 'f' : undeclared identifier
    Is the problem something to do with char/string cannot use = or == ... or am I missing something simple.
    Any help appreciated.


    Code:
    #include <stdio.h>
    
    
    void main()
    
    {
    	float degEntered;
    	float degC;
    	float degF;
    	char Conversion;
    
    	printf("Please enter the temperature: ");
    	scanf("%f%*c",degEntered);
    	printf("F to C (enter f) or C to F (enter c): ");
    	scanf("%c%*c",Conversion);
    
    	if (Conversion == f)
    	{
    		degC = (degEntered - 32) / 1.8;
    		printf("It is %5.2f degrees C",degC);
    	}
    	else
    	{
    		degF = 1.8 * degEntered + 32;
    		printf("It is %5.2f degrees F",degF);
    	}
    scanf("%*c");
    }

  2. #2
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58
    f should b in single quote as it is a char...

    use int main .. dont use void main..

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Thumbs up

    Thanks for the quick help with the problem.

    I only noticed after reading your reply that :
    Code:
    scanf("%f%*c",degEntered);
    ..
    and
    
    scanf("%c%*c",Conversion);
    should be:
    Code:
    scanf("%f%*c",&degEntered);
    scanf("%c%*c",&Conversion);
    But the main thing is that the program now compiles & returns the correct result.

    Thanks again!!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    void main()
    INCORRECT.

    int main()
    CORRECT.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    37
    hi morgog,

    I have designed a website to make understand new comers like you the C/C++ programming language in depth. its totally different from other tutorials YOU might have found somewhere across the internet.

    the contents are being added daily and very soon you will get the complete tutorial.

    check this out and understand concept of C/C++ very easily.

    http://www.stripathi.110mb.com/Tutor..._contents.html
    Last edited by san_crazy; 02-23-2009 at 01:16 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The link is broken.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    37
    check that again

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps you better check over that tutorial again. When you see something like this...
    The remaining two will be stored somewhere else as fixed locations are not given for these.
    ...for scanf, then you know something is wrong.
    You MUST explicitly specify where to store ALL the arguments you ask for. If not, you are invoking undefined behavior.

    The same for printf. It's undefined.
    Sorry, I do NOT trust that site.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    37
    Quote Originally Posted by Elysia View Post
    Perhaps you better check over that tutorial again. When you see something like this...

    ...for scanf, then you know something is wrong.
    You MUST explicitly specify where to store ALL the arguments you ask for. If not, you are invoking undefined behavior.

    The same for printf. It's undefined.
    Sorry, I do NOT trust that site.
    hi elysia,

    I think you have misunderstood the things

    I have just given a case if a user dont give the locations for all the arguments then what may be happen

    if he write

    Code:
       scanf("%d %c %f",&a);
    its not an error, since here also user must have to enter three values from keyboard(although its not given where to store other two values) hence scanf can store remaining two anywhere else.

    how many value scanf() will read depends on the format specifiers .

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I have not misunderstood. This is undefined.
    Since you tell scanf to read 3 values, so it will. And it needs a place to store those. It will not store them "somewhere else". It will try to read some arguments you, the programmer, should have passed but have not, and likely crash.
    Did you try this?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by san_crazy
    its not an error, since here also user must have to enter three values from keyboard(although locations for two values are not given) hence scanf can store remaining two anywhere else.
    No, the C standard states very clearly: "If there are insufficient arguments for the format, the behavior is undefined."

    So, you can validly argue that it is not an error, but likewise the compiler author can validly argue that there is no bug in the compiler even though the program generated by the compiler happens to do something unexpected like attempt to erase as many of your files as it can.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jul 2008
    Posts
    37
    well.....i have tried it many times in turbo c compiler and didn't get anything you have stated

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Turbo C... of course.
    That gives me another reason not to trust the site.
    Try an up-to-date compiler, such as Visual Studio or GCC, and then try again.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by san_crazy
    well.....i have tried it many times in turbo c compiler and didn't get anything you have stated
    Since the behaviour is undefined, your compiler can do anything, including whatever you expect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Ah, the good old "I've got a test case which 'works for me', therefore my argument is proven".

    Bzzzt - wrong. Try some other operating systems and compilers (especially ones like GCC which warn you about making a mess of printf/scanf calls).

    Sooner or later, you'll get your ass handed to you on a plate in the form of a segmentation fault (or worse).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pronunciation of "char"
    By brewbuck in forum C Programming
    Replies: 10
    Last Post: 06-15-2007, 10:53 AM
  2. variable "char" problem
    By Cloud_909 in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2005, 10:49 PM
  3. Difference between "char* buf "and "char *buf"
    By smo59 in forum C Programming
    Replies: 2
    Last Post: 09-02-2004, 03:44 AM
  4. How can I copy a "char *" to a "char"...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-31-2002, 10:52 AM
  5. Converting a "Char" string to an Integer Array
    By Jazz in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2001, 01:50 PM