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 7090577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:04:19+00:00 2026-05-28T08:04:19+00:00

I am a math student, and I’m learning the very basics in programming in

  • 0

I am a math student, and I’m learning the very basics in programming in C. I need a program to read an input consisting in an array, the components of which must have certain requisites; I would like the program to ask the user for the components of the array. The user should then have to enter such components separating them with spaces. The details aren’t important to get the main question across; I’ll choose a simpler example then the one I am dealing with: let’s say I want an array with 6 components not to contain the number 4. So I tried:

#include <stdio.h>

int main(void) {
    int a[6];
    printf("enter components: ");
    int i;
    for (i = 0; i < 6; i++) {
        scanf("%d", &a[i]);
        if (a[i] == 4) printf(" \n\n4 is not allowed, try again\n\n");
    }
    for (i = 0; i < 6; i++) {
        printf("%d ", a[i]);
    }
}

If I compile this and run it, and for example enter:

1 2 3 4 5 6

I will get my error message, but only after having pressed enter, that is after having entered all six components (not straight after having pressed space for the fourth time). So here are my questions (I am looking for solutions which don’t make use of strings or pointers, unless it is impossible to do without them):

  1. Is there a way to get the program to read a component (and to act accordingly) straight after its subsequent space has been entered? I’m guessing there isn’t because scanf only works after the user presses enter, and not space, correct?

  2. If there isn’t, is there a way to get the program to read the components all at once after having pressed enter at the end, but letting the user pick up from the last right component? For example, with the above input, I would like the program to display something like this:

    4 is not allowed
    1 2 3 _
    

    so that the user can correct his/her input (possibly changing the first three digits as well).

Sorry if this question is too dumb! Thank you for your help!!


EDIT: Well, thanks for the great answers, you have all been very helpful! It’s a pity I can’t accept more than one.

  • 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-05-28T08:04:20+00:00Added an answer on May 28, 2026 at 8:04 am

    Please see the bellow code:

    #include <stdio.h>
    
    int main(void) {
        int a[6];
        int i;
        bool hasError = false;
        int errorIndex = 0;
        do{
            hasError = false;
            printf("enter components: ");
            for (i = 0; i < errorIndex; i++)
                printf("%d ", a[i]);
    
            for (i = errorIndex; i < 6; i++) {
                scanf("%d", &a[i]);
                if (a[i] == 4 && hasError == false){
                    printf(" \n\n4 is not allowed, try again\n\n");
                    hasError = true;
                    errorIndex = i;
                }
            }
        }while(hasError == true);
        for (i = 0; i < 6; i++) {
            printf("%d ", a[i]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate Math.exp() from java very frequently, is it possible to get
Let's assume we have this very simple table: |class |student| --------------- Math Alice Math
I suck at math. I need to figure out how to calculate a video
I'm not a math guy in the least but I'm interested in learning about
I'm a student using Xcode 3.2 and What I need to know is how
I am trying to dynamically allocate an array of base(Student) classes, and then assign
scala.math.ScalaNumber is a Java file which looks like this: public abstract class ScalaNumber extends
Math.random() in javascript is able to return 1, right? Which means if I would
Math has come on my way again and has defeated me. I need your
I have a simple POCO class that contains the student's scores. For example: Math

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.