Struggling with end of array for program

May 10, 2008 at 3:06am
Add code to read 5 words from the Console (user input). Store these values into an array of string []. Make sure the string[] array is large enough to hold at least 6 values. Store the string constant “end_of_array” into the last element of the array. Using a do…while() loop, print out the 1st and 3rd letters of each word (two letters per line) using the substring function.

Any guidance would be greatly appreciated.



May 10, 2008 at 7:07am
What have you done so far??
May 10, 2008 at 5:01pm
This is what I have done far:

//First, write a simple program that will read 5 numbers from the Console (user input). Store these values into an array of int []. Using the length of the array in a for() loop and decrementing the loop counter, write the array values in reverse order to the screen.


#include <iostream>
#include <string>

using namespace std;

int main()

{
int array[5];
int counter;
int i = 0;
int string[6];
char word;




cout << "Please enter five numbers: ";


for (counter = 0; counter < 5; counter++)
{
cin >> array[counter];

}

cout << endl;


cout << "Counting down the numbers: ";

for(counter = 0; counter < 5; counter = counter++)
cout << array[counter] << " ";

cout << endl;



cout << "The numbers in reverse order are: ";

for (counter = 4; counter >= 0; counter--)
cout << array[counter] << " ";

cout << endl;


//Next, add code to read 5 words from the Console (user input). Store these values into an array of string []. Make sure the string[] array is large enough to hold at least 6 values. Store the string constant “end_of_array” into the last element of the array. Using a do…while() loop, print out the 1st and 3rd letters of each word (two letters per line) using the substring function.

cout << "Please enter five words: ";

for (counter = 0; counter < 6; counter++)
{
cin >> string[counter];

}

cout << endl;
do
{
cout << "Print out 1st letter for each word: " << string << endl;

}while(


return 0;

}

May 11, 2008 at 6:17am
A quick solution in the spirit of your original code;

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
#include <iostream>
#include <string>

using namespace std;


int main()
{
	
	int array[5];
	int counter;
	string words[6];
	const string endWord("end_of_array");

	cout << "Please enter five numbers: ";


	for (counter = 0; counter < 5; counter++)
	{
		cin >> array[counter];
	}

	cout << endl;


	cout << "The numbers are: ";

	for(counter = 0; counter < 5;counter++)
	{
		cout << array[counter] << " ";
	}

	cout << endl;



	cout << "The numbers in reverse order are: ";

	for (counter = 4; counter >= 0; counter--)
	cout << array[counter] << " ";

	cout << endl;


	//Next, add code to read 5 words from the Console (user input). 
	//Store these values into an array of string []. 
	//Make sure the string[] array is large enough to hold at least 6 values. 
	//Store the string constant “end_of_array” into the last element of the array. //
	//Using a do…while() loop, print out the 1st and 3rd letters of each 
	//word (two letters per line) using the substring function.

	//clean any otstanding stuff from standard input
	char trash[256];
	cin.getline(trash,256,'\n');
	
	
	cout << "Please enter five words: ";

	for (counter = 0; counter < 5; counter++)
	{
		cin >> words[counter];

	}
	words[5]=endWord;

	
	counter=0;
	cout << "Print out 1st and 3rd letter of each word: " << endl;

	do
	{
		cout <<  words[counter].substr(0,1);
		cout << words[counter].substr(2,1);
		cout << endl;
		counter++;

	}while( words[counter]!=endWord);

	
	return 0;
}

May 12, 2008 at 12:01am
Thank you so much for your help I just wish the book would show examples like this so I could understand it better.
Topic archived. No new replies allowed.