-3

I´m completely new to programming and just started some tutorials from the internet. One of them suggested this code for an easy question for the user:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int input;

printf("Please enter a number between 1 and 4:\n");
scanf("%d",&input);
fflush(stdin);

switch(input)
{
case 1: prinft("You entered 1\n");
break;
case 2: prinft("You entered 2\n");
break;
case 3: printf("You entered 3\n");
break;
case 4: printf("You entered 4\n");
break;
default: printf("Unknown Input, please try again\n");
}

system("PAUSE");
return 0;
}

I use Dev-C++ and can´t even compile the code. the "case 1:"-row gets highlighted and the error-message says: 'printf' undeclared (first use this function). Can anybody tell me how to fix this problem?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
themoleofdoom
  • 13
  • 1
  • 3
  • 1
    Throw this tutorial far away. `fflush(stdin)` is undefined behavior. –  Sep 02 '13 at 16:25
  • @H2CO3; This [site](http://msdn.microsoft.com/en-us/library/9yky46tz.aspx) also suggest `fflush(stdin)`! – haccks Sep 02 '13 at 16:41
  • @haccks That site sucks too. (I hope you don't imply that whatever Microsoft suggests should be correct! It's often exactly the other way around. See [this question/answer](http://stackoverflow.com/questions/2187474/i-am-not-able-to-flush-stdin).) –  Sep 02 '13 at 16:45
  • @haccks As I also learned recently, MS chose to make `fflush(stdin)` a documented valid construct, but it **is an extension** valid only for MS compilers (and runtimes, maybe). As H2CO3 said: it is UB according to the standard (and MS on that site wasn't *kind* enough to point this out). If you use this idiom on platforms other than Windows chaos can ensue! – LorenzoDonati4Ukraine-OnStrike Sep 02 '13 at 17:10
  • @H2CO3: [That site](http://msdn.microsoft.com/en-us/library/9yky46tz.aspx) explicitly says that "fflush on input stream is an extension to the C standard", which is perfectly correct. The behavior of `fflush(stdin)` is not defined by the C standard; apparently it is defined by Microsoft's implementation. (Even so, `fflush(stdin)` is a silly way to accomplish what that sample program is trying to do; it should simply read and discard characters until it sees a newline.) – Keith Thompson Sep 02 '13 at 17:19
  • @KeithThompson I'm not saying that it's not a Microsoft extension - I'm saying that it's a silly and wrong thing to 1. rely on undefined behavior, 2. rely on platform-specific behavior when a problem could be solved more easily, correctly and portably. I think it's quite appropriate to suggest OP to throw that tutorial away, indeed. –  Sep 02 '13 at 17:22
  • 1
    The error message didn't refer to the `switch` statement. Now that you know what the problem was, of course you know that "switch case doesn't work" is not an appropriate title -- but you *should* have known that before. Also, whenever possible, you should copy-and-paste any error messages, *not* re-type them. – Keith Thompson Sep 02 '13 at 17:23
  • @KeithThompson Often people have absolutely no idea what they are doing. Nor do they make any effort at least *trying* to understand the error message; instead, they automatically dismiss it because "cryptic compiler errors are only to be read and understood by ninja expert magicians who know everything". (Scary and sad, isn't it?) –  Sep 02 '13 at 17:25
  • @H2CO3: You also didn't bother to mention that it *is* a Microsoft extension. The C standard explicitly permits extensions. If you say "`fflush(stdin)` is undefined behavior" without any further explanation, it's perfectly reasonable to point to the Microsoft documentation and say "No, it says right here that it works". Instead, I prefer to explain that (a) the behavior is not defined *by the C standard*, (b) it's a Microsoft extension whose use makes your code less portable, and (c) here's why you shouldn't use it even in Microsoft-specific code. There's much more to the story than UB. – Keith Thompson Sep 02 '13 at 17:27
  • @H2CO3: I find it neither scary nor sad. It's a learning opportunity. – Keith Thompson Sep 02 '13 at 17:28
  • @All: Well, as I said, I´m completely new to programming and just downloaded the compiler and the tutorial a few days ago. so, calm down guys, I´m really trying. Anyway, thx for the hints. – themoleofdoom Sep 02 '13 at 18:16
  • 1
    This question appears to be off-topic because it is about a typo – chue x Sep 02 '13 at 19:10
  • Keith Thompson is right.I am completely agreed with him. – haccks Sep 02 '13 at 20:14

2 Answers2

4

Typo:

prinft()

should be:

printf()

printf is short for "print formatted".

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
1

Small mistakes in your code can confuse you, but you got to be persistent. This community, is generally harsh to beginners. Anyway, best of wishes in you programming adventures.

prinft(); 

should actually be

printf();

Some advice: you can change int main(int argc, char *argv[]) to

int main(int a, char *b[]) and whatever else you want. You don't have to confuse yourself.

user3328692
  • 99
  • 1
  • 2
  • 8