0

Hope someone could help me on this one, thanks in advanced!

My objective is to repeat an input until the user hit/press the letter 'N'

But the problem is when it comes to the part of answering the question:

"Add Another Number? [Y/N]"

the program stops!

Here's my faulty code: :(

#include<stdio.h>
#include<conio.h>

int main()

{

	int num1, num2, total;
	char ans;


clrscr();


	do {
		printf("Enter first number: ");
		scanf("%d", &num1);

		printf("Enter second number: ");
		scanf("%d", &num2);

		total = num1 + num2;

		printf("\nTotal is: %d \n", total);

		printf("\nAdd another number? [Y/N]: ");
		scanf("%c",&ans);


	} while (ans != 'N');


getch();

return(0);

}

Thanks again to all!

3
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by madmhan84
0

scanf("%c",&ans);

at line 27 use %s instead of %c

Edited by zeroliken: n/a

Votes + Comments
I don't think thats gonna help instead complicates the problem.
0
scanf("%c",&ans);

%c is preceded with a white-space character.
Like this:

scanf(" %c",&ans);

You might remove

getch();

.
Hope to help you.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.