Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

what is this program doing?

Status
Not open for further replies.

lcs81

Member level 3
Member level 3
Joined
Aug 2, 2005
Messages
57
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,683
1 main (){
2 int no, i, j, k;
3 printf("Enter three values: ");
4 no=scanf("%d %d %d",&i, &j, &k);
5 while (no<3){

7 while (getchar()!= '\n'){}

8 printf("you only type %d value correctly\n", no);
9 printf("Try again:");
10 num_input=scanf("%d %d %d", &i, &j, &k);

11 } }


When line 7 doesnt exits, the program will loop infinitely.
I just dont really undestant why just cannot ignore line 7.
Can any one explain what line 7 doing?

Is line 4 common in used?What is means?

Thanks
 

Your program won't compile because line 10 "num_input" is undeclared. I assume you meant "no".

If scanf encounters unexpected text that doesn't match your format string, it leaves that text in the input stream, so if you repeat the scanf, it hits the same text again (and again, and again, ...). Your while loop removes the unexpected text from the input stream.

You are learning that it's awkward to use scanf with error recovery. Instead, many programmers use fgets (to fetch an entire input line) followed by sscanf to parse the line. Try it!

Code:
#include <stdio.h>

int main (void)
{
  int i, j, k;
  char buf[100];

  while (1)
  {
    printf("Enter three values: ");
    if (NULL == fgets(buf, sizeof(buf), stdin))
    {
      printf("End of input data!\n");
      break;
    }
    if (3 == sscanf(buf, "%d %d %d", &i, &j, &k))
    {
      printf("Success: i=%d j=%d k=%d\n", i, j, k);
      break;
    }
    printf("Try again\n");
  }
  return 0;
}
 

----------------previous program--------------------------
1 main (){
2 int no, i, j, k;
3 printf("Enter three values: ");
4 no=scanf("%d %d %d",&i, &j, &k);
5 while (no<3){

7 while (getchar()!= '\n'){}

8 printf("you only type %d value correctly\n", no);
9 printf("Try again:");
10 no=scanf("%d %d %d", &i, &j, &k);

11 } }
-----------------------------------------------------------------

QUESTION:
When line 7 doesnt exits, the program will loop infinitely.
I just dont really undestant why just cannot ignore line 7.
Can any one explain what line 7 doing?

Is line 4 common in used?What is means?

The line 10 is 'no' not 'num_input'.
By the way, this program is from one of the book which didnt explain much!
but i try this program, it works.
However, as i say, when line 7 is ignore, the program will execute infinite loop.
Isnt the program abouve similar to below?

----------------program 2----------------
int counter,num;
counter=0;

while (counter <3) {
printf("please key in ur number:\n");
scanf("%d",&num);
counter=counter +1;
}

--------------------------------------------


Thanks
 

In fact I think that the line 7 is just a manner to stand by untill the user don't type the enter key. To be sure that the user has entred less than 3 values.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top