Thread: reading a char

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    11

    reading a char

    Hi all,
    I am pretty new to C programming, I used Pascal before.
    I've tried to write a program which converts between Fahrenheit and Celsius.
    But when I ask the user to write a char, the program doesnt recognize it. I did a simple test, and I found, that my program saves the enter (\n) as well as the char. But that's not supposed to be. So how to solve this?
    I used Borland C++ Builder.
    Thanks for any help!

    Code:
    #include <stdio.h>
    
    int main (void)
    {
      char CorFahr;
      float convertable;
      printf("Convert celsius or fahrenheit? c-f");
      do
      {
    	CorFahr=getchar();
    	printf("%c",CorFahr);
    	if (CorFahr == 'c' || CorFahr == 'f') 
    	{
    	printf("write c or f");
            }
      }
      while (CorFahr != 'c' || CorFahr != 'f');
      scanf("%f",&convertable);
      if (CorFahr == 'c' )
      {
        printf("%f in celsius: ",convertable);
    	convertable-=32;
    	convertable*=5/9;
    	printf("%f\n", convertable);
      }
      if (CorFahr == 'f')
      {
    	printf("%f in fahrenheit: ",convertable);
    	convertable*=9/5;
    	convertable+=32;
    	printf("%f\n",convertable);
      }
      return 0;
    }
    Last edited by Zoiddani; 08-28-2010 at 08:05 AM. Reason: something minor

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    fflush vs gets
    I think you should read/find the c-faq before posting question.
    Because this kind of question are very common.....like once a week at least...

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    11
    EDIT: Solved!
    Last edited by Zoiddani; 08-28-2010 at 08:07 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    After each scanf(), add a getchar() line of code, to remove the newline char that scanf() leaves behind (in the keyboard buffer).

    Especially needed for char input, with scanf().

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    11
    with fflush?
    I got another problem now.
    If I enter for example 1233 for the char, the while loop will check it 4 times (bcos its 4 chars long), if it has got an 'f' or 'c' in it. I dont understand why, because its just a char, and it should only store 1 character, not 4...

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You enter a string. The string is written to the input buffer.
    getchar reads one character from the string and returns. If the char is not c or f, you loop until you find such a character.
    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
    Aug 2010
    Posts
    11
    ah.. now i understand.
    So how to get rid of the other characters?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Depends on how you want your program to behave.
    Consider the scenarios that user enters invalid input (not c or f). What do you do then?

    Also, don't edit out your posts after getting an answer. Your problems might help someone else another day.
    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
    Aug 2010
    Posts
    11
    I edited the first post because i was missing two }'s in my code.
    I edited my second one, because I wrote questions, but ~2 minutes after posting I found the answer for the questions, I dont wanted to bother you

    So actually, at one time theres only one character stored in my char variable.
    So I don't know how long the text will be, which the user gives.
    Maybe I could read the last line from stdin, check if the second character is \n or not. Is this possible?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Aye, sure.
    Use fgets to read into a char array.
    Then you use the index operator to access a specific char.
    For example:
    Code:
    char buf[1024];
    fgets(buf, sizeof(buf), stdin);
    if (buf[1] == '\n') // Arrays are 0-indexed.
        /* Do something */;
    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
    Registered User
    Join Date
    Aug 2010
    Posts
    11
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending a simple email in C++?
    By Coukapecker in forum C++ Programming
    Replies: 6
    Last Post: 04-09-2010, 12:36 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM