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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:45:12+00:00 2026-05-17T20:45:12+00:00

Ok so im having a bit of an issue here. basically Im reading shared

  • 0

Ok so im having a bit of an issue here.
basically Im reading shared memory but thats not the problem.
I have a change.c function that lets me change a struct studentdata shared memory if I enter their “ID” number.

problem is, it’s showing it’s not matching, EVEN THO IT IS.

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/sem.h>
#include <pwd.h>
#include <stdlib.h>
#include "header.h"

//VOID CHANGE_STUDENT
//function that asks the user to enter the new data and stores the data
//into the appropriate student struct

void Change_Student(struct StudentInfo *ptr, int sema_set);

//BEGIN MAIN FUNCTION
main(int argc, char* argv[])
{
    int i,id;           //Id to hold shmem id
    struct StudentInfo *infoptr;    //Ptr to shmem
    int sema_set;           //ID for semaphores
    char input[30];         //Buffer for user input
    int found = 0;          //Found 'bool'
    struct StudentInfo *beginptr;   //Ptr to beginning of data
    int rcid;           //Readcount ID
    int* rcptr;         //Readcount ptr


/* get the id of the shared memory segment with key "KEY" */
/* note that this is the segment where the data is stored */
    id = shmget(KEY,SEGSIZE, 0);
    if (id <0){
            perror("change: shmget failed 1");
            exit(1);
    }

/* attach the already created shared memory segment to infoptr so the
 *    shared memory segment can be accessed through 'inforptr'
 *       */
    infoptr=(struct StudentInfo *)shmat(id,0,0);
    if (infoptr <= (struct StudentInfo *) (0)) 
    {
            perror("change: shmat failed");
            exit(2);
    }

    //Get the shmem with readcount ID
    rcid = shmget(RCKEY,READCOUNT,0);
    if(rcid < 0)
    {
        perror("Change: shmget failed");
        exit(1);
    }
    //Attach the memory to our program and set ptr to it
    rcptr = (int*)shmat(id,0,0);
    if(rcptr <= (int*)(0))
    {
        perror("Change: shmat failed");
        exit(2);
    }

    //Get semaphores with our ID
    sema_set = semget(SEMA_KEY,NUM_SEMAPHS,0);
    if(sema_set < 0)
    {
        perror("Change: shmget failed");
        exit(1);
    }


    //While the user cannot enter correct password
    while(strcmp(input,"000")!=0)
    {
        printf("\nPlease input your password: ");
        scanf("%s",input);
    }

    //Set the begin pointer
    beginptr = infoptr;
        while(1)
        {
    //Ask the user which ID to change
        printf("Please input a student ID you would like to change ('quit' to quit): ");
        scanf("%s",input);


    //If the issue quit command, exit
        if(strcmp(input,"quit")==0)
                exit(0);

    //Check for the ID in shmem
        while((strcmp(infoptr->Name,"")!=0) && (found != 1))
        {
            printf("%s\n",infoptr->ID);

        //if the name was found, print the data we are changing
                if(strcmp(infoptr->ID,input)==0)
                {
                        found = 1;

                        printf("\n%s\n",infoptr->Name);
                        printf("%s\n",infoptr->telNumber);
                        printf("%s\n",infoptr->Address);
                        printf("%s\n\n",infoptr->ID);
                }
                else
                        infoptr++;
        }

    //if never found, print error
        if(found == 0)
                printf("Record not found.\n");
    //if found, run change record function
    if(found == 1)
        Change_Student(infoptr,sema_set);

    //reset found
        found = 0;
    //Reset ptr
        infoptr = beginptr;

        }

}

void Change_Student(struct StudentInfo *ptr,int sema_set)
{

    char input[50];     //Input buffer
    Wait(sema_set,0);   //Wait on writers

    //Get the user's input to change the data
    gets(input);
    printf("Input Student Name: ");
    gets(input);
    strcpy(ptr->Name,input);

    printf("Input Student Tel. Number: ");
    gets(input);
    strcpy(ptr->telNumber,input);

    printf("Input Student Address: ");
    gets(input);

    strcpy(ptr->Address,input);

    //Sleep for testing
    sleep(10);
    //Signal the writers
    Signal(sema_set,0);

    //Confirmation message
    printf("\nRecord Updated!\n");

    exit(0);
}

when I run the program I get this (the printout if just for debugging)

Please input your password: 000
Please input a student ID you would like to change ('quit' to quit): 111223333
111223333
111223344
111223355
111223366
111223377
111224411
111224422
111224433
111224444
111224455
111224466
Record not found.
Please input a student ID you would like to change ('quit' to quit):  

Last time I checked. 111223333 and 111223333 were the same thing? So why isn’t it working? because infoptr->ID is showing the same thing in the loop?

  • 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-17T20:45:12+00:00Added an answer on May 17, 2026 at 8:45 pm

    I believe the strings really aren’t the same. Try using strncmp ?

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

Sidebar

Related Questions

No related questions found

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.