Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:31:29+00:00 2026-06-15T16:31:29+00:00

I am trying to take inputs within a loop. How do I know when

  • 0

I am trying to take inputs within a loop. How do I know when the input stops? Say for example I have an int input and I am taking in the input as long as the user wants. But I am not prompting before each entry. I’m not asking if the user has any more inputs, nor I do I know the number of inputs in advance.

How do I stop taking in the inputs?

Here is what I’ve till now.

while(1){
    scanf("%d",&input);
    if(conditionFulfilled)
        index=0;

    if(index)
        printf("%d",input);
}

Here, as long as the condition is not fulfilled, I print everything. But once the condition is fulfilled, I keep taking in the inputs, but don’t print them. How do I know when the input stops?

I’ll try to make myself more clear now.

What I want to do is, I want the user to keep entering the numbers, now I want to print those numbers in the same order, but once the user has entered a number greater than 50 I dont want to print that and any number afterwards.

Also, I dont want the user to stop entering the inputs once this above condition is achieved. Also, I can ask the user every time if he wants to enter any more number? I just want him to keep entering, and then once he is done. I should get the desired printed output.

But I just cant figure out, how do I know if the user is done with all the inputs.

I was thinking I can store the inputs as characters, since a newLine is stored in the character but not in the integer inputs. i.e. scanf("%c",&input). will store the \n character, but if it is scanf("%d",&input) will ignore all the newLines, now I can check for two consecutive newlines, and that will stop it.

Well, I am not sure if it is the right way?

Also, say if I enter 4 and then hit enter, now 4 and enter both are on the input stream, now how can I clear this input stream for the newLine character, and just take in 4 in my variable?
I hope it makes more sense now?

Edit: I guess the above solution wont work, since if I am taking in the input as char I wont be able to process 22 since It will just take the first 2 and leave the other one in buffer only.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:31:30+00:00Added an answer on June 15, 2026 at 4:31 pm

    If you want the user to do something like:

    5<enter>
    16<enter>
    3<enter>
    ...
    

    As long as they want, you can’t know when they are done. I’m assuming this is the case you’re talking about since you’re looping a single input.

    In this case you have to set the end state for the user, either by a terminating sequence such as “enter -1 to stop” or by setting the number of inputs before hand, “enter 10 values”.

    However if you want to accept any number of values all at once, such as:

    5 16 3 ... <enter>
    

    Then you can read the value into a string with scanf() and break it apart around your token character (space in this case). If you want to do this you don’t need a loop.


    So let’s say in your code conditionaFulfilled is an input of 0, and you want to break when they enter a -1:

    while(1){
    scanf("%d",&input);
    if(input == 0)
      index=0;
    else if(input == -1)
       break;  
    if(index)
    printf("%d",input);
    }
    

    Now the code will keep entering and printing numbers until they put in a 0, at this point it will keep taking numbers, but not using them until the input is -1.


    EDIT based on your edit:

    Well, I am not sure if it is the right way?

    There really isn’t a “right way”, it’s your program so it’s up to you. The first part of my answer explains this, you have to define how you want to take your input. Do you want them to enter the numbers one at a time? Do you want them all on one line? It’s up to you!

    Edit: I guess the above solution wont work, since if I am taking in the input as char I wont be able to process 22 since It will just take the first 2 and leave the other one in buffer only.

    no, your code is taking them as integer numbers %d. If you were taking them as characters %c then yes, you are correct a multi-digit number you’d have to do special work to take:

    If the user enters 22<enter> you’d have to loop taking characters until a newline and you’d have to shift the digits accordling:

    2 * 10^1 + 2 * 10 ^0 = 22
    

    Also, say if I enter 4 and then hit enter, now 4 and enter both are on the input stream, now how can I clear this input stream for the newLine character, and just take in 4 in my variable?

    If you’re only reading digits via scanf("%d", &input), then you don’t have to worry about new line characters.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to take user form input and display it back to the user,
I'm trying to create a loop than continues to take input until the input
I am trying to using the JS to take user input and modify certain
Im trying to create a method that take 2 int array as the input
I am trying to take text that the user inputs in a text view
I am trying to create a program that will take user inputs(marks) and output
I am trying to take 3 separate integer inputs (year,month,day) and taking these 3
I am trying to create a simple program using Gstreamer to take the input
I have a datagridview with 9 columns. I am simply trying take the value
I'm trying to write a tool that will take as input some C code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.