0

I have created two arrays, friends and timechat. Instead of writing long code that manually puts each piece of data into the 2d array I want to do it with a for loop. I have created a 2D array, 2 columns and 5 rows. One column must have all the names of the friends the other the times. Where am I going wrong?

Code:

string **friendslist;
friendslist = new string*[10];

for (int i = 0; i < 10; i++)
    friendslist[i] = new string[10];


string friends[5] = {"Bob","Rob","Jim","Hannah","James"};
string timechat[5] = {"12:00", "5:00", "22:00", "18:30", "11:45"};

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 2; j++)
    {
        friendslist[j][i] = friends[i];
        cout << friendslist[j][i] << " ";
    }
    cout << endl;
}
cin.get();
Coder77
  • 2,203
  • 5
  • 20
  • 28
  • The code doesn't really make any sense. And is there a reason you don't use e.g. [`std::array`](http://en.cppreference.com/w/cpp/container/array) (or [`std::vector`](http://en.cppreference.com/w/cpp/container/vector)) with [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string)? – Some programmer dude Mar 08 '15 at 18:22
  • Can you explain your use of the triply nested init loop? I see a `y` counter that is not referenced within. Is this the dead husk of a previous iteration of the code? It only obfuscates the problem. – BaseZen Mar 08 '15 at 18:22
  • I've changed it back to a previous iteration, hope it makes more sense. @BaseZen – Coder77 Mar 08 '15 at 18:24
  • 1
    I think you've been trying a lot of different things and have ended up with the worst of everything. Take a fresh pass through every line and see if it's what you really meant. Another head scratcher is the inner `for` loops that count from 0 to 0. (`for int j = ...`) – BaseZen Mar 08 '15 at 18:24
  • Ignore the previous code and check the current :P – Coder77 Mar 08 '15 at 18:25
  • Looks like what you need here is a `std::map`, the key being friend names, the value being timechat values. – Christian Hackl Mar 08 '15 at 18:42
  • 1
    @Coder See here how usage of std::map consists simplifies greatly what you're trying to do : http://ideone.com/D6Qev4 – PaulMcKenzie Mar 08 '15 at 18:55

2 Answers2

1

I have de-garbled everything and put it in recommended novice style with extra-explicit variable names ... something very important for you at this stage. I have purposefully ignored your timechat so you can master array mechanics and loops first. The suggestions about better leveraging the std:: library with arrays, vectors, and maps are good but should come later. First make sense of this and why/how it's different than yours:

#include <iostream>
#include <string>

using namespace std;

const int NUMBER_OF_LISTS_OF_FRIENDS = 2;
const int NUMBER_OF_FRIENDS_IN_ONE_LIST = 5;

int main(int argc, const char *argv[]) {
  // put your constant data at top
  string friends[NUMBER_OF_FRIENDS_IN_ONE_LIST] = {"Bob","Rob","Jim","Hannah","James"};

  string **friendslist;
  friendslist = new string*[NUMBER_OF_LISTS_OF_FRIENDS]; // Two lists of friends

  // Allocate your storage
  for (int init_list_index = 0; init_list_index < NUMBER_OF_LISTS_OF_FRIENDS; init_list_index++) {
    // each friend list is of length 5
    friendslist[init_list_index] = new string[NUMBER_OF_FRIENDS_IN_ONE_LIST];
  }


  // Initialize the storage with useful contents
  for ( int list_index = 0; list_index < NUMBER_OF_LISTS_OF_FRIENDS; list_index++ ) {
    for (int friend_index = 0; friend_index < NUMBER_OF_FRIENDS_IN_ONE_LIST; friend_index++ ) {
      friendslist[list_index][friend_index] = friends[friend_index];
    }
  }

  // output all the values in a clear format as an initialization check
  for ( int list_index = 0; list_index < NUMBER_OF_LISTS_OF_FRIENDS; list_index++ ) {
    for (int friend_index = 0; friend_index < NUMBER_OF_FRIENDS_IN_ONE_LIST; friend_index++ ) {
      cout << "list " << list_index << ", friend index " << friend_index << ": "
           << friendslist[list_index][friend_index] << "\t";
    }
    cout << endl;
  }
}
BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • Okay, that does help me understand with how much memory I should be allocating. However, I already understood how to initialize the storage with useful contents from 1 array of data, just not 2 arrays of data, the second being timechat. – Coder77 Mar 08 '15 at 19:03
  • But this example should help you do that very simply. If you know how to create one array, you know how to create another. A for loop can have multiple statements within it. Right now you there's one statement that's sticking contents into `friendslist` from `friends`. It follows that a second statement can stick contents into `timechats_list` from `timechats`. You can re-use the index variables `list_index` and `friend_index` as much as you want, this time in the second array. They're just integers. – BaseZen Mar 08 '15 at 19:07
  • [And if this code is painful, then yes, ultimately, you should be grouping together a friend's name and her chat time, so you don't have all this parallel, repetitive code: in a `struct` (C-style) or `std::map` (more idiomatic C++ with STL)] – BaseZen Mar 08 '15 at 19:10
  • See what I don't quite understand it, if you just simply add another statement to that for loop that adds data to the array from timechat and friends. Then the second line will just overwrite the data that has been written from the first line? – Coder77 Mar 08 '15 at 19:18
  • The code being: `friendslist[list_index][friend_index] = friends[friend_index];` and `friendslist[list_index][friend_index] = timechat[friend_index];` – Coder77 Mar 08 '15 at 19:19
  • Right, you need to declare, allocate, and initialize a *separate* array that holds the chat time strings! The answer is simpler than you think. But once again this makes us call out desperately for organizational concepts like structs, classes, and maps. Are you familiar with those or just getting your feet wet? – BaseZen Mar 08 '15 at 19:23
  • I am comfortable with declaring, allocating and initializing a separate array. I'm okay with basic structs, but the book I am reading has not yet covered classes or maps. – Coder77 Mar 08 '15 at 19:26
0

Your loop counters don't make a whole lot of sense. For example, you use:

for (int j = 0; j < 1; j++)

This effectively iterates once, with j == 0. Additionally, you have a nested loop:

for (int y = 0; y < 1; y++)

This again iterates once, but you don't even reference y

Dan Paulat
  • 152
  • 6