restar program

C

Carramba

hi!

if I have for exemple program

#include <stdio.h>
main() {
printf("Enter number:\n");
scanf("%d", Num);
printf("Number is %d!\n", Num);
fflush(stdin);
getchar();
}
how do I start program over so that after printf("Number is %d!\n", Num);
I can get line like "press c to continue q to quite" and if c is entered
so program starts from begining?


--
Thanx in advance!

;-)

______________________________________
I se the lightat the end, but every time I take a step it's get dim.
 
W

Walter Roberson

:if I have for exemple program

:#include <stdio.h>
:main() {

Non-standard. main is defined to return an int. If you intend your
program to be C89 or later, you should also be putting in explicit
arguments to main -- argc and argv if you need them, void otherwise.

: printf("Enter number:\n");

The output from the printf is not necessarily going to appear before
the next statement unless you specifically flush the output queue
or you specifically set your buffering mode to unbuffered or line
buffered. The default is to unbuffered *only* if the OS can establish
for sure that the output is going to a terminal.

: scanf("%d", Num);

Where is the variable Num defined? How do you know if the scanf was
successful or not?

: printf("Number is %d!\n", Num);
: fflush(stdin);

fflush() against stdin is not defined.

: getchar();

And getchar() may require that an entire line be entered (with
carriage return) unless you have used the appropriate OS-dependant
facility to set input to be character by character. POSIX has
standardized interfaces to those facilities via tcgetattr() and
tcsetattr().

: }
:how do I start program over so that after printf("Number is %d!\n", Num);
:I can get line like "press c to continue q to quite" and if c is entered
:so program starts from begining?

Use any of the looping constructs. The best one for the situation
should be relatively obvious once you look at the short list of the
available looping constructs.
 
E

Eric Sosman

Walter said:
:if I have for exemple program
:[...]
: printf("Enter number:\n");

The output from the printf is not necessarily going to appear before
the next statement unless you specifically flush the output queue
or you specifically set your buffering mode to unbuffered or line
buffered. The default is to unbuffered *only* if the OS can establish
for sure that the output is going to a terminal.

You have it backwards, or perhaps skewed. 7.19.3/7:

"[...] As initially opened, [...] the standard input
and standard output streams are FULLY BUFFERED [emphasis
mine] if and only if the stream can be determined NOT
[emphasis mine] to refer to an interactive device."

Thus, stdout to "a terminal" will be either unbuffered or line
buffered, but will certainly[*] not be fully buffered.

[*] Except on the DeathStation 9x00, where "terminal device"
takes on an entirely different meaning; see

http://dialspace.dial.pipex.com/town/green/gfd34/art/

The DS 9x00's keyboard, mouse, trackball, joystick, data glove,
touchscreen, and retinal scanner are considered "recalcitrant,"
not "interactive." The only "interactive" devices are the
front-panel switches, which respond to erroneous input with a
400,000-volt discharge from the offended switch into the
offending finger, making fflush() superfluous.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top