Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[H]How to make new or repeat an Input without quitting the main program using loop?

loop

  • Please log in to reply
3 replies to this topic

#1 madmhan84

madmhan84

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 27 December 2011 - 02:48 AM

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
:confused:

#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", total);

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


	} while (ans != 'N');


getch();

return(0);

}

Thanks again!
  • 0

#2 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 27 December 2011 - 06:41 AM

This is a fairly common question in C/C++; the issue is that \n stays in input buffer when you're reading num1 and num2 and when program reaches scanf for ans, it gets fed by that newline from buffer. You can search the internet, or even tutorials here on codecall, and there will be several pages debating how to flush input stream.

There are several code snippets available, but what I tried was instead of single character ans I created an array, read user's choice (Add another number?) as string and compared first character (index 0) to n || N. It seems to be working nicely on my system, but can't guarantee it will work everywhere.
  • 0

The roots of education are bitter, but the fruit is sweet.


#3 madmhan84

madmhan84

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 27 December 2011 - 10:04 PM

This is a fairly common question in C/C++; the issue is that \n stays in input buffer when you're reading num1 and num2 and when program reaches scanf for ans, it gets fed by that newline from buffer. You can search the internet, or even tutorials here on codecall, and there will be several pages debating how to flush input stream.

There are several code snippets available, but what I tried was instead of single character ans I created an array, read user's choice (Add another number?) as string and compared first character (index 0) to n || N. It seems to be working nicely on my system, but can't guarantee it will work everywhere.


thanks for the reply sir

  • 0

#4 Muted

Muted

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 80 posts

Posted 28 December 2011 - 03:11 AM

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
:confused:

Thanks again!


Hello, madmhan84.

Here's the working code (one of many potential solutions) for your program:
#include <stdio.h>

int main() {
    
    int num1 = 0, num2 = 0, total = 0;
    char ans = 0;

    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("Add another number? [Y/N]: ");
        scanf(" %c",&ans);
    } while (ans != 'N');
    
    return 0;
}

That is probably the most straight-forward method of using scanf(), in my opinion (' 'is eaten by the second pass, instead of the '\n').
Here's a small post on Stack Overflow that you can read: parsing input with scanf in C - Stack Overflow

Welcome. :)
  • 0
“You may be disappointed if you fail, but you are doomed if you don't try.”
- Beverly Sills





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download