Jump to content

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

- - - - -

  • Please log in to reply
3 replies to this topic

#1
madmhan84

madmhan84

    Newbie

  • Members
  • PipPip
  • 15 posts
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!

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
madmhan84

madmhan84

    Newbie

  • Members
  • PipPip
  • 15 posts

Flying Dutchman said:

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


#4
Muted

Muted

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts

madmhan84 said:

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. :)
“You may be disappointed if you fail, but you are doomed if you don't try.”
- Beverly Sills




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users