Thread: How to add and delete items in a structure?

  1. #31
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    Good luck since you refuse to take any advice given there is no reason I should continue to offer assistance.

    Jim
    Ok, thank you for all your support, is appreciated.

  2. #32
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by JorgeChemE View Post
    Jim, I know how to use fgets but we are required to submit this project to be evaluated, in class we only studied scanf and gets for this purpose, we also studied getch, getchar, getche, puts and maybe something more I almost forget. The code I am using is the one the instructor expects according to what we are learning, otherwise, she would think we are cheating.
    This isn't cheating. This is C in the year of 2017!
    getch() was in the standard of 89/90, was deprecated in the standard of 99 and is no longer part of the standard from 2011.
    Your instructor should learn that the programing language C is under a development process, like all other languages.
    There is no advantage if student learn old and outdated practices. Rather, we have in the future programs with buffer overflow and undefined behavor and when we look why this happens, the programer from tomorrow will say: 'we have learn it this way'.
    I mean, all instructors should look at the last standard and all schoolbooks should be updated to reflect the new standard.
    The instructor can show you the old functions, but only that students know it and this functions can be in old code.
    New code should allways be written to be accordant to the new standard.


    Also, please consider what we have told you:
    avoid global variables, hold the scope of variables as small as possible
    avoid magic numbers, instead use #define
    don't use fflush() on an input stream
    don't use deprecated or outdated functions, instead use the new and safer functions


    I think other members of this forum will complete this list, because i am a self learning hobbyist and not a full trained programer.
    Other have classes, we are class

  3. #33
    Registered User
    Join Date
    May 2017
    Posts
    101
    This is a good and polite manner to say things and I appreciate it. Just remember because I told you lots of times: this is not my field of knowledge, this is not what I must do to earn a salary and I am starting learning. If I asked you is because I wanted to follow the course that I took just to gain a new skill not because I will be a developer, is ridiculous! if I wanted to be a developer I should have studied computer sciences. As you know and if you don't know I tell you, at least in my country almost every engineering degree has a subject about programming. I almost forgot everything about this subject + in an engineering degree you don't really understand anything but loops and barely syntaxis. I just want to understand some basics about a language, nothing more like this, is not my field. If I have time, surely I will keep on studying further from this course. But is just a course, you know? I will have a job interview this friday, I am unemployed and this fact is 1000000000000000 times more important than passing a course, even if I fail this course and don't get a "diploma" but just have even a single job interview I will be the happiest guy around the world. Anyway, tomorrow we will have 20 minutes to ask about the project, the only exercise we must submit to be evaluated. Out of 6 functions I failed modifying, I am trying to solve it and I know I have a memory leak but that's ok... If I learn something it worths the money. Understanding all the concepts will take more time, I do not have the practice that most of you surely have. I am not happy with this course because apparently you must have some knowledge of programming to pass it and finally was out of my possibilities since went faster than what I can understand but at least I understood something in case I need it in a future job, not related with programming.

  4. #34
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    784
    Quote Originally Posted by WoodSTokk View Post
    getch() was in the standard of 89/90, was deprecated in the standard of 99 and is no longer part of the standard from 2011.
    I think you meant gets() rather than getch(), as the latter never was part of any C standard (it comes from the DOS world, AFAIK).

  5. #35
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by christop View Post
    I think you meant gets() rather than getch(), as the latter never was part of any C standard (it comes from the DOS world, AFAIK).
    Ooops, yes, i mean gets, sorry.

    @JorgeChemE

    Your search will never end.
    Here is a skeleton that should work.
    Code:
    #define NAME_MAX 100
    
    void modificar_mascota() {
        char nombre_mascota[NAME_MAX];
        tmascota *temp;
    
        if (primero == NULL) return; // check if we have a valid pointer
    
        printf("\nIntroduzca el nombre de la mascota a modificar: ");
        fflush(stdout);
        fgets(nombre_mascota, NAME_MAX, stdin);
    
        temp = primero;
        while (temp) { // same as 'temp != NULL'
            if (strcmp(temp->nombre_mascota, nombre_mascota) == 0) break;
            temp=temp->siguiente;
        }
    
        if (temp) {
            // edit the pet where 'temp' points to
        }
        else {
            printf("Sorry, no pet found\n");
        }
    }
    You don't need to allocate memory for a new struct, because you want to edit one.
    The while-loop will break if the pet was found, otherwise it runs until there is no more pets in the list and temp become NULL.
    But this is a quick and dirty solution, because if the name of the pet is 'Charly' and you enter 'charly', the pet will not be found (case sensitive).

    PS: this code is untested, i have it written out of my head.
    Last edited by WoodSTokk; 06-01-2017 at 05:13 AM.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-25-2013, 05:09 AM
  2. using delete on a structure pointer
    By spongefreddie in forum C++ Programming
    Replies: 9
    Last Post: 07-06-2011, 12:12 PM
  3. Delete records in Structure
    By Myrren in forum C Programming
    Replies: 6
    Last Post: 11-19-2010, 02:15 AM
  4. Delete All Items In A Linked List
    By Khellendros in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2009, 01:22 AM
  5. Delete duplicate items in an array
    By chaos in forum C Programming
    Replies: 4
    Last Post: 06-16-2005, 02:51 PM

Tags for this Thread