How to display contents of Char array line by line

closed account (NADG3TCk)
Hie. Please can i know how i can display the words typed in by the user into a char array. Here is my code.

cout << "How many words should i look for : ";
cin >> numWords;
cout << "\nEnter the " << numWords << " words: " << endl;

for (k = 0; k <= numWords; k++)
{
cin >> showWords[k];
}

cout << "WORDS " << endl << "*****" << endl;

for (int i = 0; i < numWords; i++)
{
cout << showWords[i];
}

The user should input the words (each on a new line) for example:
Cat
animal
dog
maize
grains

and then the program should output exactly the same way the user has input the values

you never created an array to store anything into.

Also vectors are a better option than arrays unless your instructions specifically say to use an array.
closed account (NADG3TCk)
Here is my array, sorry i had ommited it. showWords[100]. The instructions say that i should use arrays only.
your for first for loop should only be < not <=

You are displaying the contents of your array here:
1
2
3
4
for (int i = 0; i < numWords; i++)
{
cout << showWords[i];
}


if you post your entire code i can test it to see if it works myself.

Also is this array suppose to be dynamically allocated? Because if the user enters a number bigger than 100 you'll have more words than array elements to store them in.
Last edited on
closed account (NADG3TCk)
#include <iostream>
using namespace std;

//Function Prototypes
int sizeOfArray(int);
void DisplayChars(char [][15], int);
int main()
{
int size, numWords, i, j, k;
char showChars[15][15], showWords[20]

cout << "How many rows and columns: ";
cin >> size;

while (sizeOfArray(size) != 1)
{
cout << "Please enter a valid range between 1 and 15. " << endl;
cin >> size;
}

cout << "Enter the characters, " << size << " per row: ";
cout << endl;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
cin >> showChars[i][j];
}
}
cout << endl;

cout << "How many words should i look for : ";
cin >> numWords;
cout << "\nEnter the " << numWords << " words: " << endl;

for (k = 0; k <= numWords; k++)
{
cin >> showWords[k];
}
cout << endl;
cout << "HIDDEN WORDS PUZZLE " << endl << "****************" << endl;
DisplayChars(showChars, size);

cout << endl;
cout << "WORDS " << endl << "*****" << endl;

for (int i = 0; i < numWords; i++)
{
cout << showWords[i];
}








system("PAUSE");
return 0;

}

/**
* Function <code>sizeOfArray</code> Verifies that the size of the 2D array is in
* the correct range.
* <BR>
* @param Size The size of the 2D array.
*/
int sizeOfArray(int Size)
{
while (Size > 0 && Size <= 15)
{
return 1;
}
return 0;
}

/**
* Function <code>DisplayChars</code> Displays the characters of the 2D array entered by
* the user.
* <BR>
* @param Show The 2D Array of characters to be filled in.
* @param n The size of the 2D array.
*/
void DisplayChars(char Show[][15], int n)
{
int i, j;

sizeOfArray(n);

for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << Show[i][j];
}
cout << endl;
}
return;
}


It's basically a word search game
closed account (NADG3TCk)
I havn't created the boolean function for searching for the words yet. I am still doing it step by step.
Topic archived. No new replies allowed.