Please Help!!! Project Due soon!!

This is a discussion on Please Help!!! Project Due soon!! within the C++ Programming forums, part of the General Programming Boards category; hey, here is my problem, i know it's probably simple but i'm stuck.... i'm working on a simple student record ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    Please Help!!! Project Due soon!!

    hey, here is my problem, i know it's probably simple but i'm stuck.... i'm working on a simple student record program, here is my structure...

    struct student{
    int stdnum;
    char stdname[40];
    bool active;
    };
    struct classes{
    int stdnum;
    int classnum;
    };
    struct subject{
    int classnum;
    char classname[40];
    int topic;
    };


    my job is to search and sort the info, basically just student name and student number, but the part i'm stuck on is the name. My array "array1[].stdname[]" will contain a name like "John Doe". My problem is, i have to sort by the last name, and i don't know how to extract "Doe" from the array. I'm pretty sure i need pointers, but i'm really lost... could someone please help me!!!??


    tim@timmyhall.com -emails welcome

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    I think you should just store the first and last name separate in your student structure;

    or store the last name first like Hall, Timmy

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    thanks

    i wish i could, but this is a group project, and everyone else has been using this data, so i need to find a way to extract the last name, but thanks for your help!

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    use strtok() it will do the job
    none...

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >> use strtok() it will do the job

    Ahhhh! Not strtok! You could just a pointer that points to the first letter after the white-space. You can use isalpha() to determine when you get to the first letter.

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by master5001
    >> use strtok() it will do the job

    Ahhhh! Not strtok! You could just a pointer that points to the first letter after the white-space. You can use isalpha() to determine when you get to the first letter.
    That is better, if he knows how to use pointers.

    If you want to use a pointer just read until you find a space' ', and the next letter is the first letter in the last name.
    none...

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    10
    thanks guys, i think thats what i was looking for...strtok() As long as i can locate that white space i should be fine. thanks a lot, i might be stopping by later if i run into more problems!

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    Smile

    opps, i ment i'm going to use isalpha()

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    10
    ok, i seemed to get it to work using isalpha and pointers, but now i have a new problem. Is there a way to create new variables at runtime? I don't know how many student records my function is recieving, so i planned on copying the array characters from "array1[i].stdname"...ect to a new array so i won't mess up the original data, but since i don't know how many records i'm receiving until runtime... how can i work around this problem?

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Well, the easy way is to use a vector instead of an array. You can predeclare it to a variable size at runtime, or you can just keep putting stuff into it and it'll resize itself. Existing code should require minimal changes because you can refer to the vector's elements just the same way you would the elements of an array. Search the web for STL (Standard Template Library) tutorials and information and you'll find out how to use vectors. http://www.sgi.com/tech/stl is a good place to start.

    If you really want to work with dynamically-sized arrays (<shudder>), you can declare them with the "new" keyword, any decent C++ book should discuss it in some detail. However, I feel that it is more in the spirit of C++ to use vectors.

    Oh, and convince your classmates to use strings instead of char[]'s; this is C++, not C.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    10
    i think thats a bit over my head, i hope there is another way around it

  12. #12
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    The only other way (that I can think of anyways!) is to declare the size of the array as a number greater than the maximum number of students you think this program would ever possibly use - like say 10,000. Definately not the preferred way and you'll waste a lot of memory though.

    ::waits to get slapped for teaching bad coding techniques

  13. #13
    Registered User
    Join Date
    Dec 2002
    Posts
    10
    yea, thats what i was thinking... thanks for the help, glad to see you like southpark!

  14. #14
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Honestly, I have found that using vectors is much simpler than it sounds, you really only need to know a couple of things (although fully efficient use requires a bit more knowledge). For a stopgap solution PJYelton's suggestion works, especially if your project deadline is starting to loom, but its well worth the little bit of effort to at least get on speaking terms with vectors for future projects. Any problems you may run into along the way will be cheerfully illuminated by the folks on this board!
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  15. #15
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yeah, plus once you get to know vectors you'll NEVER want to go back to arrays! They're just so much more useful!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project Help please!
    By black_hole??? in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2009, 12:55 AM
  2. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  3. MFC in an empty project
    By cunnus88 in forum Windows Programming
    Replies: 0
    Last Post: 10-09-2005, 09:19 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21