How to display only 3 strings from an array per line?

Hey everyone,
So I need to somehow be able to display the elements I have stored in an array of the string data type but displaying them 3 to a line.

So if I had an array such as...
 
MyArray[7] = {"FD","GH","JK","LK","AS","OP","PL"};


I would want my output to look like this...
1
2
3
4
Here are your array contents:
FD GH JK
LK AS OP
PL


How would I do this?
I obviously would need a for loop to display all the elements from the array.
It's the if statement that is required that has me lost.
What condition would go in there to allow me to set it to start a newline if
that condition is met?

Here is small part of my code. I can get it to skip if there are 3 lines by doing this. But when there are 7 elements like the above example, my logic would not work. I need something based on multiples I would think.

1
2
3
4
5
6
for (int count = 0; count < size; count++)
	{
		cout << *iparr[count] << ' '; 
		if (count == 2)
			cout << endl;
	}


Must be 3 elements per line.
I may be overthinking this one...

Thanks,
MisterTams
Last edited on
There must be lots of variations, here are two possibilities:
1
2
3
4
5
6
7
8
9
    const int size = 7;
    const char * MyArray[size] = {"FD","GH","JK","LK","AS","OP","PL"};
    
    for (int count = 0; count < size; count++)
    {
        if (count > 0  &&  (count % 3) == 0)
            cout << endl;
        cout << MyArray[count] << ' '; 
    }




1
2
3
4
    for (int count = 0; count < size; count++)
    {
        cout << MyArray[count] << (((count+1) % 3) ? ' ' : '\n'); 
    }
Chervil,

That doesn't seem to work for me. Ignore the example I used at the top of my original post. Here is the program that I am creating.

I am sorting the trade symbols in alphabetical order and then trying to display them 3 strings to a line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

//function prototype

int findStock(string *, int);
void ArrSort(string *[], int );

int main()
{
	string filename;
	fstream inFile;
	int numOfStocks = 0;
	string *TradeSymbol = nullptr, *CompanyName = nullptr;
	int *numOfShares = nullptr;
	double *CurrentPrice = nullptr;
	char delimiter1 = ' ';
	char delimiter2 = '#';
	int index = 0;

	cout << "User please enter the name of the file that you would like to read." << endl;
	cout << "Example Format: CplusplusIsFun.txt" << endl;
	cout << "\nEnter: ";
	cin >> filename;

	inFile.open(filename);

	if (inFile.fail())
	{
		cout << "\nFile not found!" << endl;
		exit(-1);
	}
	else
	{
		cout << "\nFile has opened successfully!" << endl;
			inFile >> numOfStocks; //reading the first integer at top of text file
	}
	cout << "Number of different trading stocks found in file: " << numOfStocks << endl;

	TradeSymbol = new string[numOfStocks], CompanyName = new string[numOfStocks]; //dynamically allocated memory for parallel arrays
	numOfShares = new int[numOfStocks], CurrentPrice = new double[numOfStocks];

	for ( int x = 0; x < numOfStocks; x++)
	{
		inFile.ignore(100000, '\n'); // skip the previous newline 
		getline (inFile, TradeSymbol[x], delimiter1);
		getline(inFile, CompanyName[x], delimiter2);
		inFile >> numOfShares[x];
		inFile >> CurrentPrice[x];
	}

	inFile.close();

	//create array of pointers
	string **SortedArray = nullptr;
	SortedArray = new string *[numOfStocks]; //dynamically create new array of pointers

	//store address 
	for (int count = 0; count < numOfStocks; count++)
	SortedArray[count] = &TradeSymbol[count];

	ArrSort(SortedArray, numOfStocks);

	index = findStock(TradeSymbol, numOfStocks);

	cout << "\nCompany Name: " << right << setw(42) << CompanyName[index] << endl;
	cout << "Number of Shares: " << right << setw(38) << numOfShares[index] << endl;
	cout << "Current Price (per share): " << right << setw(29) << CurrentPrice[index] << endl;
	double totalValue = *(numOfShares + index) * *(CurrentPrice + index);
	cout << fixed << setprecision(2);
	cout << "Current Value: " << right << setw(41) << totalValue << endl;

	//free and release the memory
	delete [] SortedArray, delete [] CompanyName, delete [] numOfShares;
	delete [] CurrentPrice, delete [] TradeSymbol;

	system("pause");
	return 0;
}

//sort array of pointers
void ArrSort(string *iparr[], int size)
{
	int startScan, minIndex;
	string *minElem;

	for (startScan = 0; startScan < (size - 1); startScan++)
	{
		minIndex = startScan;
		minElem = iparr[startScan];

		for (int index = startScan + 1; index < size; index++)
		{
			if (*(iparr[index]) < *minElem)
			{
				minElem = iparr[index];
				minIndex = index;
			}
		}
		iparr[minIndex] = iparr[startScan];
		iparr[startScan] = minElem;
	}

	
	for (int count = 0; count < size; count++)
	{
		cout << *iparr[count] << ' '; 
		if (count > 0  &&  (count % 3) == 0)
			cout << endl;
	}
}

int findStock(string *TradeName, int index)
{
	bool found = false;
	string symbol;
	cout << endl;
	cout << "Enter the symbol: ";
	cin >> symbol;
	
	for (int i = 0; i < index; i++)
	{
		if (symbol == TradeName[i])
		{						
			found = true;
			return i;
		}
	}

	if (found == false)
	{
		cout << "Symbol not found!" << endl;
		exit(-2); //error message, exit with an error code of -2
	}
}


Sample output using similar code to the first example you provided me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
User please enter the name of the file that you would like to read.
Example Format: CplusplusIsFun.txt

Enter: Stock.txt

File has opened successfully!
Number of different trading stocks found in file: 5
BA F HD MCD
WMT
Enter the symbol: WMT

Company Name:                       Wal-Mart Stores Inc.
Number of Shares:                                     10
Current Price (per share):                         83.33
Current Value:                                    833.30
Press any key to continue . . .


I want it to look like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
User please enter the name of the file that you would like to read.
Example Format: CplusplusIsFun.txt

Enter: Stock.txt

File has opened successfully!
Number of different trading stocks found in file: 5
BA F HD 
MCD WMT
Enter the symbol: WMT

Company Name:                       Wal-Mart Stores Inc.
Number of Shares:                                     10
Current Price (per share):                         83.33
Current Value:                                    833.30
Press any key to continue . . .


Or if there were more trading symbols/stocks in the file
something like this...

1
2
3
APL BA F 
HD MCD WMT
WTW WUO WWR
Last edited on
In the code at line 108 in your program:
108
109
110
111
112
113
	for (int count = 0; count < size; count++)
	{
		cout << *iparr[count] << ' '; 
		if (count > 0  &&  (count % 3) == 0)
			cout << endl;
	}


you have re-arranged the sequence of events. Note in my code the lines are in a different order, so the newline is (conditionally) output before the value, not after.

108
109
110
111
112
113
114
	for (int count = 0; count < size; count++)
	{
		if (count > 0  &&  (count % 3) == 0)
			cout << endl;

		cout << *iparr[count] << ' '; 
	}
Chervil,

I didn't notice that upon first look. Makes sense and I appreciate you helping me!

Thanks!
Topic archived. No new replies allowed.