tags:

views:

150

answers:

4

How do I get and print a char from a user? This want do it...

#include <stdio.h> 
int main() {

    float number1;
    char mychar = ' ';

    printf("Number?\n");
    scanf("%f", &number1);

    printf("Character?\n");
    scanf("%c", &mychar);

    printf("You typed number: %f\n", number1);
    printf("You typed the char: %c\n", mychar);
}
A: 

lookup gets(), getc() etc -

Jeff
NEVER use gets. fgets, getc, fgetc, etc are fine, but NEVER use gets.
William Pursell
Yes, gets should never, never be used. Gcc will correctly complain if you try to use it: "test.c:(.text+0xa): warning: the `gets' function is dangerous and should not be used."
hlovdal
+2  A: 

As commented, you're question contains the answer already. But anyway:

If you want do the same thing in c++, you could use streams:

int mynumber;
char mychar;
cout << "Number?" << endl;
cin >> mynumber;
cout << "Character?" << endl;
cin >> mychar;
cout << "you typed number " << mynumber << " and char " << mychar << endl;

Of course, your C implementation would work just as well in C++.

(If you are developing a more serious application, I would recommend using something more sophisticated than just cin or scanf)

Isak Savo
+2  A: 

In C you don't need to use scanf to get single characters. You could use the getchar() function:

printf("Would you like a donut? [Y/N] ");
answer = getchar();

switch(answer) {
    case 'y':
    case 'Y':
        printf("Mmmm... donuts!");
        break;
    case 'n':
    case 'N':
        printf("Eugh... donuts?");
        break;
    default:
        printf("You should never ignore code offering donuts.");
        break;
}
deau
Include a break after the default printf(); it does no harm and saves grey hairs after a careless change.
Jonathan Leffler
Thanks, good habits are always welcome.
deau
+3  A: 

The problem you're seeing is that it really is reading a character, but it's just not the character you're expecting. scanf does formatted input. The first time you call it, you're telling it to expect a number. But you're really entering more than just a number:

Number?
1234.5678<enter>

When you press the enter key, it is actually inserting a character into your input stream. As you may know, we use \n to represent newline, the character you get when you press enter. So your input stream actually looks like "1234.5678\n".

So scanf does its thing and reads 1234.5678 and then it sees '\n'. It says "oh, that's not part of the number, so I'll stop." Well, your input still has the '\n'. The next time you call scanf, you tell it to read a character. The user types whatever they want, but that goes behind the '\n' from the previous scanf. So scanf tries to match the input stream with a character and says "ok, the first thing in this input stream is a character, and it's '\n', so I'll return that." The stuff the user typed is still sitting in the input stream.

So a simple way to get rid of it is to have a loop that empties all remaining characters from the input stream until it finds '\n'.

printf("Number?\n");
scanf("%f", &number1);
while( getchar() != '\n');

After this loop executes, your input stream will be empty. So the next time you call scanf, it'll wait for the user to type something and will use whatever the user typed.

Also note that scanf has a return value that you should check after calling it. Read up about scanf to see what it returns.

indiv
Ok, fantastic answer, one of the best I've ever seen.Thanks./C
Chris_45
Alternately, you can use fgets() to read the entire input line as text, then use sscanf() or strtod() to retrieve the numeric value.
John Bode