My array question

This is a discussion on My array question within the C++ Programming forums, part of the General Programming Boards category; Hello, im trying to write a program that takes the names of people, on a ranked external list, from there ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    28

    My array question

    Hello, im trying to write a program that takes the names of people, on a ranked external list, from there if a user types in a name if its on the list it displays it, with its rank. I think using an array would be best. but im not quite sure how to start tackling this.

    Im not really good with filing handling, im trying to figure out how to have it search the code for the specific letters or names typed in.

    this is all in C++ Thanks!

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    well if the data is already stored in the file formatted like
    "name rank
    name rank
    name rank
    ....etc"

    then you can easily create a class or struct that encapsulates that data, then store it in a vector of that type. To output based on the input criteria you can use the for_each algorithm to output only those that match the given input

    http://www.cppreference.com/cppalgorithm/for_each.html

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    An array or vector would probably be a good storage model. Vector has the advantage of you not having to worry about the size.

    Or, if you use std::map<>, you could use the name as a key, and the rank as the second element, thus avoiding you having to make a search yourself.

    As general advice: Don't try to write the whole program at once. Portion up the work, so you do (for example) the code to read the names + ranks into an array [or vector, map or some such]. Once you have that working, start working on asking the user for a name, then work on the searching and listing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    28
    Thank you for the reply s im going to start working on this in sections, typically its chaotic because i write it all, it half works and then i debug it.

    #include <fstream>
    using std::ifstream;
    using std:fstream;
    using std:stream;

    int main()
    {

    int Nums[999], x=0; ifstream inFile("babynames2004.txt"); //Open text file
    while(inFile >> Nums[x] && x<50)

    x++;

    inFile.close(); return 0;

    }
    the txt file contains a list of integers, this code will read them into the array. okay so there are a 1000 names in this file. So this makes it a little easier.

    My question is: 1) how do i make it so it read the entire line into the array?
    2) how to make the search for it? based on user input.
    Last edited by kordric; 05-01-2008 at 09:48 PM. Reason: I had a few questions.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    28
    Now that i think about this it wont work cause i need people to search for names and have it display the name and its rank. For example heres is 1 -10 rank names

    1 Jacob Emily
    2 Michael Emma
    3 Joshua Madison
    4 Matthew Olivia
    5 Ethan Hannah
    6 Andrew Abigail
    7 Daniel Isabella
    8 William Ashley
    9 Joseph Samantha
    10 Christopher Elizabeth

    So when they type in a name, say its ethan or something, then it would output
    ethan is ranked 5 in popularity among boys.
    ethan is not ranked among the girls.

    So would this be the right way to read each word in a line?

    Code:
        int number;
        char boy, girl;
        ifstream names_txt;
        names_txt.open ( "babynames2004.txt" );
        
        names_txt >> number;
        names_txt >> boy;
        names_txt >> boy;
    Still need to figure out how to have it search
    Last edited by kordric; 05-01-2008 at 10:34 PM.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    why to allocate 999 ints if you plan to read only 50?
    why not to use std::vector?
    use getline to read a string (better into std::string )
    you can use std::stringstream to parse the string if you need it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    28
    Yeah im not using that method at all, it doesnt make sense to me, i edited my other post.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    char boy will held 1 character, are you sure it is enogh?
    use std::string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    28
    Code:
        int number;
        char boy[20], girl[20];
        ifstream names_txt;
        names_txt.open ( "babynames2004.txt" );
        
        names_txt >> number;
        names_txt >> boy;
        names_txt >> girl;
    Thanks for catching that. but is this right way to read each line?
    Last edited by kordric; 05-01-2008 at 10:42 PM.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    i would write it like
    Code:
    int number;
    std::string boy, girl;
    std::ifstream names_txt( "babynames2004.txt" );
    
    names_txt >> number;
    names_txt >> boy;
    names_txt >> girl;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    28
    Okay, and that would store the entire name no matter what length? so for example lets look at the first line

    1 Jacob Emily

    it would see from
    names_txt >> rank; the first number 1.
    then names_txt >> boy; would see Jacob
    and names_txt >> girl; would see Emily?

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    would see
    will contain - yes, and yes size is no matter here
    operator >> will read till the wightspace
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    28
    Okay good, so far i have that right. Now i need to build around this a way for a user to input data so maybe something like

    Code:
    cin >> boy, girl;
    This so if they enter a name it will look at both those strings.

    So i probally should use a loop to contiune running the code
    Code:
        names_txt >> number;
        names_txt >> boy;
        names_txt >> girl;
    Then if it finds a match output it.

    What do you think?

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    cin >> boy, girl;
    no this will not work...

    something like
    Code:
    cin >> name;
    if ( name == boy)
    {
    // it's a boy name
    }
    else if (name == girl)
    {
    //it's a girl name
    }
    else
    {
    //it's a wrong name
    }
    and if you plan to search several names one after anouther - I world make a struct
    Code:
    struct Record
    {
     int index;
     std::string boy;
     std::string girl;
    };
    and fill the
    std::vector<Record>
    with data from file - then I'll be able to search for the name in this vector without rereading the file again
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    28
    Honestly i never used a vector for anything, i have no clue how to use them.

    How would i fill the std::vector<Record>?

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

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM

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