Help with simple program

en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
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.
0
haru31773's Avatar, Join Date: Mar 2010
Light Poster
so it should continue printing until the user presses E?
0
rekha_sri's Avatar, Join Date: Feb 2010
Light Poster
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;
}
}
0
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Quote:
Originally Posted by haru31773 View Post
so it should continue printing until the user presses E?
Yes
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
Quote:
Originally Posted by rekha_sri View Post
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).
0
lipun4u's Avatar, Join Date: Jan 2010
Light Poster
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.
0
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Quote:
Originally Posted by rekha_sri View Post
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.
0
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Yup exactly guys could any one help me with this.How to go about it.
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
in which operation system?
0
thapchi's Avatar, Join Date: Mar 2010
Go4Expert Member
Well the main thing u r missing is

Scanf

its user input?

where will user enter?