3

having problem with scanf character...when running program dont let me enter a character an program when i enter the integer prints the printf and go to last else ...

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

#define EG 0.23
#define AG 0.70
#define TG 0.15

main() {
    int posothta;
    char eidos;
    float poso;

    printf("Dwse posothta grammatosimwn: ");
    scanf("%d",&posothta);
    printf("Dwse to eidos grammatoshmou: ");
    scanf("%c",&eidos);

    if(eidos=='E' || eidos=='e'){
        poso=posothta*EG;
        printf("To poso pou plirwnoume einai: %f",poso);
    }else if(eidos=='A' || eidos=='a'){
        poso=posothta*AG;
        printf("To poso pou plirwnoume einai: %f",poso);
    }else if(eidos=='T' || eidos=='t'){
        poso=posothta*TG;
        printf("To poso pou plirwnoume einai: %f",poso);
    }else{
        printf("Kapou exei gine kapoio la9os");
    }   

    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
vdemon001
  • 31
  • 1
  • 1
  • 2

2 Answers2

4

When you do a scanf() it's taking the value you ask for only.. for example:

scanf("%d",&posothta);

Let's say I enter 5 here. Really, stdin got 2 characters: '5' and '\n' (because I had to hit the enter key and that generates a newline character).

So into posothta goes the 5, but that pesky newline is left sitting there. The next scanf() now is looking for a character, and since the newline character ('\n') is indeed a character, the program doesn't ask questions, it simply picks up that newline and moves on.

Change your code to:

scanf(" %c",&eidos);

Will skip tell scanf() that "I want you to skip any whitespace characters, then grab the next one". To scanf() a white space character includes not only spaces, but newlines as well.

Mike
  • 47,263
  • 29
  • 113
  • 177
  • This wrong answer! it's maybe work but your have problem that you don't flash you keyborad buffer .....see my answer – One Man Crew Mar 04 '13 at 18:47
  • @Grijesh Chauhan So why to add space before the char? what is the logic here? – One Man Crew Mar 04 '13 at 18:50
  • 1
    @OneManCrew A whitespace in the format string (plain space, newline, tab, ...) matches any sequence of whitespace in the input. So adding a space to the front of the format here makes the `scanf` skip leading whitespace, in particular the newline left in the buffer from the previous scan. – Daniel Fischer Mar 04 '13 at 18:52
  • @OneManCrew – Daniel Fischer answered,,Give it a try now! – Grijesh Chauhan Mar 04 '13 at 18:52
  • @OneManCrew - I clarified the answer, should be understandable now. BTW flushing the `stdin` buffer leads to undefined behavior. – Mike Mar 04 '13 at 18:56
  • undefined behavior!?!?!?!?this is the proper of flushall(); to clear the buffer after you read from it! – One Man Crew Mar 04 '13 at 18:58
0

You Need To flash the buffer:

printf("Dwse posothta grammatosimwn: ");
scanf("%d",&posothta);
flushall();
printf("Dwse to eidos grammatoshmou: ");
scanf("%c",&eidos);

flushall() Function:

#include <stdio.h>
int flushall( void );

Description:

The flushall() function clears all buffers associated with input streams, and writes any buffers associated with output streams. A subsequent read operation on an input file causes new data to be read from the associated file or device.

Calling the flushall() function is equivalent to calling the fflush() for all open stream files.

Returns:

The number of open streams. When an output error occurs while writing to a file, the global variable errno is set.

One Man Crew
  • 9,420
  • 2
  • 42
  • 51
  • What is `flushall()`? Where is it defined and what does it do? – Daniel Fischer Mar 04 '13 at 18:49
  • @Daniel Fischer I add Description to this function in my answer. – One Man Crew Mar 04 '13 at 18:55
  • @DanielFischer - AFAIK it's `flushall()` is not POSIX, it's tied to Microsoft's IDEs (I saw it once with Visual Studio, but it doesn't compile using gcc standard options). One Man Crew, correct me if I'm wrong on that. – Mike Mar 04 '13 at 19:00
  • The question where it is defined remains. It's not a function from the standard library, nor - afaik - POSIX. Is it a Windows specialty? – Daniel Fischer Mar 04 '13 at 19:00
  • Basically, by default MS’s C++ buffers stream output, and the the flushing causes the output stream to empty. – One Man Crew Mar 04 '13 at 19:02
  • @Daniel Fischer just fflush(NULL); flushes all open output streams (in stdio.h) – One Man Crew Mar 04 '13 at 19:05
  • @OneManCrew But the problem is that something needs to be removed from an input stream. `fflush(NULL)` is certainly not portable, although it _may_ flush input streams too in some implementation. – Daniel Fischer Mar 04 '13 at 19:16