Buffalo Soldier
October 29th, 2005, 03:52 PM
ATTN: This is for newbies to programming. I guess the pros must have known this all along.
I'm taking a subject called Programming Principles and Techniques. Basically it's an intro to programing class using C languange. The IDE installed at the computer lab and classroom is MS Visual C++. And after each lecture, I went back home at tried the lesson on Ubuntu using Anjuta. All went fine, until the lecturer teaches about fflush (stdin). Here's what I found out.
This is the problem that my lecturer ask us to solve:
#include <stdio.h>
int main()
{
int n;
char string[80];
for (n=0; n<5; n++)
{
printf ("Enter some words: ");
scanf ("%s", string);
printf ("The first word you entered is : %s\n", string);
}
return 0;
}
This is the suggested solution:
#include <stdio.h>
int main()
{
int n;
char string[80];
for (n=0; n<5; n++)
{
printf ("Enter some words: ");
scanf ("%s", string);
printf ("The first word you entered is : %s\n", string);
fflush (stdin);
}
return 0;
}
To my surprise, it only works in Windows. In Linux, it ran the same with and without fflush (stdin). After searching the net, I have found that:
Why fflush(stdin) is wrong (http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1052863818&id=1043284351), and
Things to Avoid in C/C++ -- fflush(stdin) (http://www.gidnetwork.com/b-57.html).
According to Things to Avoid in C/C++ -- fflush(stdin) (http://www.gidnetwork.com/b-57.html), it is best to avoid using scanf. But that's the only input method my lecturer has taught so far, and I wish not to confuse myself (and my classmates that I'm gonna have to share this with later) with other input methods.
Further search on the net lead me to these solution that works both in GNU/Linux and Windows:
#include <stdio.h>
int main()
{
int n;
char string[80];
for (n=0; n<5; n++)
{
printf ("Enter some words: ");
scanf ("%s", string);
printf ("The first word you entered is : %s\n", string);
while (getchar() != '\n') continue;
}
return 0;
}
#include <stdio.h>
void clear_kb(void);
int main()
{
int n;
char string[80];
for (n=0; n<5; n++)
{
printf ("Enter some words: ");
scanf ("%s", string);
printf ("The first word you entered is : %s\n", string);
clear_kb();
}
return 0;
}
void clear_kb(void)
{
char junk[255];
fgets (junk,255,stdin);
}
Why can't MS just use the standard (ANSI) C???
I'm taking a subject called Programming Principles and Techniques. Basically it's an intro to programing class using C languange. The IDE installed at the computer lab and classroom is MS Visual C++. And after each lecture, I went back home at tried the lesson on Ubuntu using Anjuta. All went fine, until the lecturer teaches about fflush (stdin). Here's what I found out.
This is the problem that my lecturer ask us to solve:
#include <stdio.h>
int main()
{
int n;
char string[80];
for (n=0; n<5; n++)
{
printf ("Enter some words: ");
scanf ("%s", string);
printf ("The first word you entered is : %s\n", string);
}
return 0;
}
This is the suggested solution:
#include <stdio.h>
int main()
{
int n;
char string[80];
for (n=0; n<5; n++)
{
printf ("Enter some words: ");
scanf ("%s", string);
printf ("The first word you entered is : %s\n", string);
fflush (stdin);
}
return 0;
}
To my surprise, it only works in Windows. In Linux, it ran the same with and without fflush (stdin). After searching the net, I have found that:
Why fflush(stdin) is wrong (http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1052863818&id=1043284351), and
Things to Avoid in C/C++ -- fflush(stdin) (http://www.gidnetwork.com/b-57.html).
According to Things to Avoid in C/C++ -- fflush(stdin) (http://www.gidnetwork.com/b-57.html), it is best to avoid using scanf. But that's the only input method my lecturer has taught so far, and I wish not to confuse myself (and my classmates that I'm gonna have to share this with later) with other input methods.
Further search on the net lead me to these solution that works both in GNU/Linux and Windows:
#include <stdio.h>
int main()
{
int n;
char string[80];
for (n=0; n<5; n++)
{
printf ("Enter some words: ");
scanf ("%s", string);
printf ("The first word you entered is : %s\n", string);
while (getchar() != '\n') continue;
}
return 0;
}
#include <stdio.h>
void clear_kb(void);
int main()
{
int n;
char string[80];
for (n=0; n<5; n++)
{
printf ("Enter some words: ");
scanf ("%s", string);
printf ("The first word you entered is : %s\n", string);
clear_kb();
}
return 0;
}
void clear_kb(void)
{
char junk[255];
fgets (junk,255,stdin);
}
Why can't MS just use the standard (ANSI) C???