?
Solved

loop problem

Posted on 2003-03-20
6
Medium Priority
?
216 Views
Last Modified: 2010-04-15
Here is my code ,
#include <stdio.h>
main(int argc,char *argv[])
{

int size=0;
int input[30];
printf("Enter the number of terms");
scanf("%d ",&size);
int i=0;
for( i=0;i<size;i++)
{
        printf("%d =",i);
        scanf("%d \n" ,&input[i]);

}
for (i=0;i<size;i++)
{
printf("%d \n",input[i]);
}
}
hen I run this the output is
Enter the number of terms3
5
0=6
1=7
2=8
5
6
7
The loop does not 8 inside its loop.instead it takes 5 into its loop.I want  to it to be
0=5
1=6
2=7
0
Comment
Question by:purtutu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:ecw
ID: 8177337
after printf, use fflush(stdout).  if writing to a tty, stdout is by default line buffered.  ie. it outputs after a newline.
0
 
LVL 6

Accepted Solution

by:
gj62 earned 120 total points
ID: 8177348
Remove the space in your scanf:

scanf("%d ",&size);

should be

scanf("%d",&size);
0
 
LVL 6

Expert Comment

by:gj62
ID: 8177362
ecw - he had a space in his scanf format control string - you don't need to flush stdout... unless you were going to do a gets or something similar after the scanf...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:purtutu
ID: 8177664
Thanks a bunch .Its working now.
0
 

Author Comment

by:purtutu
ID: 8177695
Hi again,Could you kindly tell me what happens when i keep space in scanf()
0
 
LVL 6

Expert Comment

by:gj62
ID: 8179075
As I understand it, scanf reads, but doesn't store, the whitespace character (defined as blank, tab *or* newline) if it is part of the format control string.  The expected space gets left there until your next entry of something other than a whitespace.

That's also why fflush wouldn't fix the problem either.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question