Hello,
I need to write a program to take in a name (max 101 characters) and out store in into an array so that I can output it like a list. These 2 programs are apart of a bigger program that keeps asking if you want to add another name to the array but I can't seem to get the rows and colums matched in the program. I guess what I am trying to say is I can't make the output stop at the NULL character in the array. I need the output to say:

Current List:
Mark
Steve
John

but right now its something like
J
O

it doesn't aknowledge the previous names

char Append(char List[MAX_ROWS][MAX_NAME_LENGTH], int NumUsed)
{
    char item[101]; 
        for ( int i = 0; i <= 1; i++)
        {
            cout << "Append: ";
            cin.getline(item, 101);
            strcpy(List[i], item);
            NumUsed++;
        }


    system("CLS"); 
}



char Show(char List[MAX_ROWS][MAX_NAME_LENGTH], int NumUsed)
{
    cout << "*************\n"
         << "Current List [0/" << NumUsed << "]:" << endl;
    for ( int i = 0; i <= NumUsed; i++ )
    {
        for ( int j = 0; j <= NumUsed; j++ )
        {
            cout << List[i][j];
        }
    }    
    cout << "\n*************" << endl;
}

BY THE WAY:
Yes I do need a 2d array because there is a cursor that needs to scroll along the names.

If these are NULL terminated character strings, it seems like your Show function would be a SINGLE loop, not a double, and would display one name at a time, not one character at a time, and each iteration of the loop would contain a newline.

char Show(char List[MAX_ROWS][MAX_NAME_LENGTH], int NumUsed)
{
    cout << "*************\n"
         << "Current List [0/" << NumUsed << "]:" << endl;
    for ( int i = 0; i <= NumUsed; i++ )
    {
        cout << List[i] << "\n";
    }    
    cout << "\n*************" << endl;
}

[EDIT]
In addition, consider your loop boundaries. Do you want < or <= for your condition test?

Edited 4 Years Ago by VernonDozier

The problem I am having is how do I take a in a 2D array. When I try to set up a for loop for the input I dont see how I can make it so that a after a name gets inputted it goes to the next row for the next name.

Edited 4 Years Ago by ShEeRMiLiTaNt

The problem I am having is how do I take a in a 2D array. When I try to set up a for loop for the input I dont see how I can make it so that a after a name gets inputted it goes to the next row for the next name.

Not sure what you are saying here. Can you give an example of the exact input and how the input should be stored in the 2-D array?

So for expample how can I modify this program to make it so that it take in 5 names of 10 characters and outputs them.

#include <cstdlib>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char l[5][10];
    char name[10];

    for(int j = 0; j < 1; j++)
    {
        for(int i = 0; i < 1; i++)
        {
            cout << ": ";
            cin.getline(name,10);
            strcpy(l[i],name);
        }
    }
    for (int j = 0; j < 5; j++)
    {
        cout << l[j][];
}
}

Instead of

for ( int i = 0; i <= NumUsed; i++ )
{
    for ( int j = 0; j <= NumUsed; j++ )
    {
        cout << List[i][j];
    }
}    

try

for ( int i = 0; i <= NumUsed; i++ )
{
        cout << List[i];
}    

@ np complete thanks it worked, but now if I were to try and ask the user after every input whether or not they want to add another name how would that look like. I got this but I think I got the concept wrong. K is just a counter of how many names.

#include <cstdlib>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    int i, j, k=0;
    char l[5][10];
    char name[10], ans;
    do
    {
        for(i = 0; i < (k+1); i++)
        {
            cout << (k);
            cout << ": ";
            cin.getline(name,10);
            strcpy(l[i],name);
            //cin.ignore(500,'\n');
            k++;
        }
        cout << "k= "  << k << endl;
        for (j = 0; j <= (k); j++)
        {
            cout << l[j] << endl;
        }
        cout << "Would you like to add another? ";
        cin >> ans;
    }while (ans == 'y' || ans == 'Y');
}

No need for the i-loop. You have a do-while loop that iterates for as long as the user wants to add names. The structure of that do-while loop looks good. Seems to me that the printing should perhaps occur AFTER the do-while loop.

As for the i-loop, as mentioned, I see no reason for it. The do-while handles things. You have a variable k that keeps track of how many names have been entered. You might want to add a test of that k variable to your do-while test to prevent array overflow. Get rid of the i variable and lines 14, 15, and 22. On line 19, replace the variable i with k.

Again, look at whether you want < or <= for your condition test in the printing for-loop.

This question has already been answered. Start a new discussion instead.