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();
7
  • The code doesn't really make any sense. And is there a reason you don't use e.g. std::array (or std::vector) with std::string? Commented Mar 8, 2015 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
    Commented Mar 8, 2015 at 18:22
  • I've changed it back to a previous iteration, hope it makes more sense. @BaseZen
    – Coder77
    Commented Mar 8, 2015 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
    Commented Mar 8, 2015 at 18:24
  • 1
    @Coder See here how usage of std::map consists simplifies greatly what you're trying to do : ideone.com/D6Qev4 Commented Mar 8, 2015 at 18:55

2 Answers 2

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;
  }
}
7
  • 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
    Commented Mar 8, 2015 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
    Commented Mar 8, 2015 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
    Commented Mar 8, 2015 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
    Commented Mar 8, 2015 at 19:18
  • The code being: friendslist[list_index][friend_index] = friends[friend_index]; and friendslist[list_index][friend_index] = timechat[friend_index];
    – Coder77
    Commented Mar 8, 2015 at 19:19
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.