2
#define f(x) (x*(x+1)*(2*x+1))/6 void terminate(); main() { int n,op; char c; printf("Enter n value\n"); scanf("%d",&n); op=f(n); printf("%d",op); printf("want to enter another value: (y / n)?\n"); scanf("%c",&c); // execution stops here itself without taking input. getch(); if(c=='y') main(); else terminate(); getch(); } void terminate() { exit(1); } 

In the program above , I want to take input from the user until he enters an n value. For this I'm trying to call main() function repeatedly . If it is legal in C , I want to know why the program terminates at the scanf("%c",&c) as shown in commented line. Someone , please help.

2
  • OT: It's int main(void) at least. And assuming terminate() is not meant to take any parameters, it should be void terminate(void). Commented Feb 21, 2014 at 17:26
  • @alk - made the changes , still the problem persists. Commented Feb 21, 2014 at 17:29

3 Answers 3

5

You should never call main from within your program. If you need to run it more then once use a while loop inside it.

Your execution stops because by default stdin in a terminal is line buffered. Also you are not using the return value from getch.

int main() { int n,op; char c; do { printf("Enter n value\n"); scanf("%d",&n); op=f(n); printf("%d",op); printf("want to enter another value: (y / n)?\n"); scanf("%c",&c); } while (c == 'y') return 0; } 
Sign up to request clarification or add additional context in comments.

4 Comments

I'm guessing the getch call is to get and discard the newline left by the scanf call in the buffer. Which is not needed in this case (as you show in your code snippet).
@JoachimPileborg He is only calling scanf which by default discards all whitespace characters. No need for that.
Not when scanning for a character.
@JoachimPileborg Yeah. The next scanf call scans for an integer though, so it's fine. I would have used fgets and then sscanf from buffer though.
3

You first have

scanf("%d",&n); 

which you have to press the Enter key for it to accept the number.

Later you have

scanf("%c",&c); 

There is a problem here, which is that the first call to scanf leaves that Enter key in the input buffer. So the later scanf call will read that.

This is easily solved, by changing the format string for the second scanf call just a little bit:

scanf(" %c",&c); /* ^ */ /* | */ /* Note space here */ 

This tells the scanf function to skip leading whitespace, which includes newlines like the Enter key leaves.

3 Comments

@haccks It's my avatar yes, not animated though. :) Got it as a reward for a Kickstarter pledge. :)
In fact its very much looks like you (as I seen in your old avatar) and that's why I asked you. :)
@haccks Yeah, the artist really "caught me" :)
1

It's legal, but you'll have a STACKOVERFLOW after a while (pun intended). What you need is a loop:

while (1) { printf("Enter n value\n"); scanf("%d",&n); op=f(n); printf("%d",op); printf("want to enter another value: (y / n)?\n"); scanf("%c",&c); // execution stops here itself without taking input. getch(); if(c != 'y') break;; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.