0

i need to enter a character in a function, and then depending on what that character is, the function returns either A, B , P or # now. Before that function is ever called i need to enter a number. now my problem is , that the number that I enter seems to be passed to the function aswell. here's my code

char vnes(){
char pom;


printf("Vnesete vrednost za poleto: \n");

scanf("%c", &pom);
if (pom=='A') return 'A';
if (pom=='B') return 'B';
if (pom=='C') return 'C';

else return '#';


}

int main()
{
    int n, pamtia, pamtib, provb=0, provcelo=1, i;
    char pom;


    scanf("%d", &n);


    for (i=0; i<n; i++){
        pom=vnes();
//rest of code not important
}
}

the first character is always # , even if I enter A , or B

wonder
  • 75
  • 8
  • Don't use `scanf`, for anything, ever. Use `fgets`, `strtol`, and hand-written parsing code. If that sounds too tedious, instead consider using a higher-level programming language. – zwol Nov 28 '15 at 19:53
  • 3
    `scanf("%c", &pom);` --> `scanf(" %c", &pom);` skip previos white-space(newline). – BLUEPIXY Nov 28 '15 at 19:53
  • The problem is `scanf("%d")` followed by `scanf("%c")`. Most conversion specifiers skip leading whitespace. `%c` does not. So if you type `123A`, your code will work, but if you type `123 A` or `123`enter`A`, then `%c` will read the *space* or the *newline* respectively. – user3386109 Nov 28 '15 at 19:59

0 Answers0