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

  • SEARCH
  • Home
  • 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 8343555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:06:00+00:00 2026-06-09T06:06:00+00:00

I am writing a function for a program that asks the user to input

  • 0

I am writing a function for a program that asks the user to input a “student id number” and stores it in the array. Before storing the function has to check if we don’t already have that number in the array because the student numbers must be unique. It also includes a pointer to an int that represents how many student numbers have been stored so far.
I have written some code but it is not working 🙁 would anyone please shed some light?
This is my code:

void update_student_id(int a[], int*pnum)
{
        int temp,h;
        for (h=0;h<=*pnum;h++){
                printf(">>>Student ID:");
                scanf("%d",&temp);
                        if (temp==a[h]){
                                printf("ERROR:%d has already been used!\n",temp);
                                h=*pnum+1;
                        }
                        else
                            h=*pnum+1;
            }
        a[*pnum]=temp;
        *pnum++;

Ok, new version with 2 for loops, improving but not working yet 🙁

void update_student_id(int a[], int*pnum)
{
    int temp,h,i;
    for (h=0;h<=*pnum;h++){
            printf(">>>Student ID:");
            scanf("%d",&temp);

            for(i=0;i<=*pnum;i++)
                    if (temp==a[i]){
                            printf("ERROR:%d has already been used!\n",temp);
                            i=*pnum+1;
                    }
                    else    i++;
            }
        a[*pnum]=temp;
        (*pnum)++;
}

Problem solved with Dennis’ help, final code:

void update_student_id(int a[], int*pnum)
{
    int temp,h,i,canary;
    for (h = 0; h <= *pnum; h++) {
            printf(">>>Student ID:");
            scanf("%d", &temp);

    canary = 0;
    for (i = 0; i < *pnum; i++) {
            if (temp == a[i]) {
            printf("ERROR:%d has already been used!\n",temp);
            canary = 1;
            break;
            }
    }
    if (canary == 0) {
            a[*pnum] = temp;
            (*pnum)++;
            break;
        }
}

return;}
  • 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-09T06:06:01+00:00Added an answer on June 9, 2026 at 6:06 am

    So, the main thing I see is that after you scan the number, you’re not actually checking to see if the number is already in the array; you’re only checking to see if it’s at a particular index.

    Being more specific, you need more than that if statement inside your for loop; perhaps another loop that looks through the indices used up so far.

    EDIT: The biggest problem is that in the else clause, don’t increment i. Second, you need some canary variable to let you know if you’ve made that call to printf.

    For example, here’s my idea for those loops:

    for (h = 0; h < *pum; h++) {
        printf(">>>Student ID:");
        scanf("%d", &temp);
    
        canary = 0; // assuming you initialize this at beginning of function
        for (i = 0; i < *pnum; i++) {
            if (temp == a[i]) {
                printf("ERROR:%d has already been used!\n",temp);
                canary = 1;
                break;
            }
            // DON'T INCREMENT i, LOOP IS ALREADY DOING IT
        }
    
        // if canary is still 0, we know we haven't called that printf
        // and can add in the element. Only then do we increment the count.
        if (canary == 0) {
            a[*pnum] = temp;
            (*pnum)++;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a function for my Clojure program that reads user input from
I'm writing a program that sends an array through a function (upperhess) that will
I'm writing a program that gathers user input for two file names, and opens
I'm writing a program that involves calculating the maximum of a user defined function,
I'm creating a program that allow a user introduce a lambda function only writing
I am writing a program for class that manages a Hotel. This Report1 function
I am writing a program that has to access a MySQL database. To do
I am writing a DLL function called ADAuthenticate that will authenticate a user against
I am writing a program that will call a function from an external library,
Library liba defines a certain function f. When writing a C program that uses

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.