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!

Recommended Answers

All 3 Replies

scanf("%c",&ans);

at line 27 use %s instead of %c

commented: I don't think thats gonna help instead complicates the problem. -1
scanf("%c",&ans);

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

scanf(" %c",&ans);

You might remove

getch();

.
Hope to help you.

thanks again people! ^_^ it works now..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.