Thread: C++ Array Problem

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    5

    C++ Array Problem

    Can someone please help me with this. I'm trying to read in 10 names into an array. I want to setup three different arrays, one for the first name, one for the last name and the other for address. My input looks like the following:

    Bruce Wayne The Batcave
    Sonny Bono 10 Mayor Street
    Clint Eastwood 2020 Make My Day Drive
    Jimmy Carter 1010 Peanut Street
    Mike Tyson 2525 Robin Court
    Mickey Mouse 6000 Mickey Lane
    Abe Lincoln 900 Booth Street
    Scrooge McDuck 7011 McDuck Drive
    Ed Koch 125 E.125th Street
    Barney Rubble 1001 Old Stone Age Road

    I'm reading it from a txt file. Can someone please help me set up the array.

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Have you made any attempt to do it yourself? If so, please show us some code so we can help you.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Why do you want to use an array? This is C++ after all, we have things like vectors and std::lists and whatnot.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also, why do it as three separate arrays? A <first name, last name, address> triple are pretty strongly connected, so why not make that a thing (struct, class, whatever) and then make an array (or better, vector etc) of those?

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
        ifstream infile;
        infile.open("List.txt");
    
    
        string Mfirstname[20];
        string Mlastname [20];
        string Maddress [20];
    
        if ( !infile.is_open() ) {
            cout << endl << "ERROR: Unable to open file" << endl;
            system ("PAUSE");
            exit(1);
        }
    
    
        int i = 0;
        while (!infile.eof() && infile >> Mfirstname[i]>> Mlastname[i]>> Maddress[i])
            ++i;

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    I have to use an array to do it.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't use >> to read something with spaces in it. You'll need something like getline instead.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by abdool_raheem03 View Post
    I have to use an array to do it.
    Why do you need to use a C style string for a C++ project? Not to mention char w[20] holds 20 characters, not 20 strings.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by AndrewHunter View Post
    Why do you need to use a C style string for a C++ project? Not to mention char w[20] holds 20 characters, not 20 strings.
    Eh, those are arrays of strings, not arrays of char. Why he has 20 of each when he's only supposed to need 10, I don't know (but I always respect a little bit of paranoia).

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    38
    please give a little more background. is this for a school project? is that why you "have" to use arrays? Google getline. it helped me a lot when i was trying to teach myself something similar to this.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by tabstop View Post
    Eh, those are arrays of strings, not arrays of char. Why he has 20 of each when he's only supposed to need 10, I don't know (but I always respect a little bit of paranoia).
    Oops, missed that one. I guess I just assumed after I heard "arrays".
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by abdool_raheem03 View Post
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
        ifstream infile;
        infile.open("List.txt");
    
    
        string Mfirstname[20];
        string Mlastname [20];
        string Maddress [20];
    
        if ( !infile.is_open() ) {
            cout << endl << "ERROR: Unable to open file" << endl;
            system ("PAUSE");
            exit(1);
        }
    
    
        int i = 0;
        while (!infile.eof() && infile >> Mfirstname[i]>> Mlastname[i]>> Maddress[i])
            ++i;
    Ok, first you should group your address list into a grouping, then work with some file input keeping in mind the limitations of '>>' and post what you have come up with. We can help you from there:
    Code:
    #define maxList 20
    
    struct addressList{
    	std::string firstname;
    	std::string lastname;
    	std::string address;
    };
    
    int main(void){
    
    	struct addressList myList[maxList];
    	//.......rest of your stuff
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >> struct addressList myList[maxList];
    You can remove the "struct" part.
    Also, just a FYI... I've never heard of a convention that begins with a small letters for types. That's usually reserved for instances of objects.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Elysia View Post
    >> struct addressList myList[maxList];
    You can remove the "struct" part.
    Also, just a FYI... I've never heard of a convention that begins with a small letters for types. That's usually reserved for instances of objects.
    Haha....good catch on my C style struct declaration. Thanks for pointing that out. I will pay attention to my naming conventions, I somehow just adopted that style awhile ago while working up examples.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by Elysia View Post
    I've never heard of a convention that begins with a small letters for types. That's usually reserved for instances of objects.
    Yes you have, you use them all the time. At least, you used to use boost and the standard template library. None of those types begin with an uppercase letter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using an array, problem
    By .C-Man. in forum C++ Programming
    Replies: 24
    Last Post: 03-10-2011, 07:43 AM
  2. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  3. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  4. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  5. array problem
    By sasuke12 in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2005, 09:35 AM