Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help with simple program (http://www.go4expert.com/forums/help-simple-program-t21261/)

en_7123 9Mar2010 14:34

Help with simple program
 
OK I'm trying to have a simple loop which keeps on printing each number from 1,2,... on a different line and it keeps on doing so until the user enters 'e'.
e.g
for(i=0;i<=9999999999;i++)
{
printf("%d\n",i);
}

It does not take any input from user and keeps on printing but as soon as user presses e the program exits.Any clue how to go about this.

haru31773 9Mar2010 16:10

Re: Help with simple program
 
so it should continue printing until the user presses E?

rekha_sri 9Mar2010 16:52

Re: Help with simple program
 
Use the following program for printing the integer value unless press E or e. This example will help for your requirement.

Code:


#include<stdio.h>
#include<stdlib.h>
main()
{
long i;
char c;
for(i=0;i<=99999;i++)
{
printf("%d",i);
c=getchar();
if(c == 'e' || c == 'E')
{
        break;
}
continue;
}
}


en_7123 10Mar2010 10:02

Re: Help with simple program
 
Quote:

Originally Posted by haru31773 (Post 65387)
so it should continue printing until the user presses E?

Yes

virxen 10Mar2010 13:18

Re: Help with simple program
 
Quote:

Originally Posted by rekha_sri (Post 65393)
Use the following program for printing the integer value unless press E or e. This example will help for your requirement.

Code:


#include<stdio.h>
#include<stdlib.h>
main()
{
long i;
char c;
for(i=0;i<=99999;i++)
{
printf("%d",i);
c=getchar();
if(c == 'e' || c == 'E')
{
        break;
}
continue;
}
}




with getchar you stop the execution of the program until the user press enter.
certainly not what he wants.

And no this is not a simple program.
There is not an easy way to do it(especially platform independent).

lipun4u 10Mar2010 13:56

Re: Help with simple program
 
You need thread or process creation and some signal stuff.

Create a child process which will print the numbers and the parent process will take user input.

en_7123 10Mar2010 16:00

Re: Help with simple program
 
Quote:

Originally Posted by rekha_sri (Post 65393)
Use the following program for printing the integer value unless press E or e. This example will help for your requirement.

Code:


#include<stdio.h>
#include<stdlib.h>
main()
{
long i;
char c;
for(i=0;i<=99999;i++)
{
printf("%d",i);
c=getchar();
if(c == 'e' || c == 'E')
{
        break;
}
continue;
}
}


Well this doesn't exactly help because the program does not continue printing numbers and it waits for a user input after each number is printed.

en_7123 10Mar2010 16:07

Re: Help with simple program
 
Yup exactly guys could any one help me with this.How to go about it.

virxen 10Mar2010 16:11

Re: Help with simple program
 
in which operation system?

thapchi 10Mar2010 16:22

Re: Help with simple program
 
Well the main thing u r missing is

Scanf

its user input?

where will user enter?


All times are GMT +5.5. The time now is 11:07.