pulling charactors from an array

i have an array with 6 slots. each slot has a word in it.how do i go about Using a do while() loop to print out the 1st and 3rd letters of each word (two letters per line) using the substring function
How are words represented in your program? If you do it using std::string it's piece of cake. You don't have to use any substring function, just the [] operator.

Info on std::string -> http://cplusplus.com/reference/string/string/
Info on loops -> http://cplusplus.com/doc/tutorial/control/#loops
this is my code so far

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

using namespace std;

const int arraySize = 6;

int main()

{
int array[5];
int counter;
int i = 0;
string userIn;
string strArray[arraySize];
string str1, str2, str3, str4, str5, str6;

ifstream inData;
ofstream outData;

inData.open ("strIn.txt");
outData.open ("strOut.txt");




cout << "Please enter five numbers: " << endl;


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

}

cout << endl;


cout << "The numbers you entered are: ";

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

cout << endl;



cout << "And the numbers in reverse order are: ";

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

cout << endl;

cout << "Please enter five words : " << endl;
for (counter = 0; counter < 5; counter++)
{
cin >> strArray[counter];

}

cout << endl;
strArray[5]="end_of_array";

cout << "The words you entered are: ";

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


cout << endl;

cout << "The first and third letters of each of the words you entered are : ";

cout << endl;




system("pause");

return 0;
}
Use a for loop to get each word of your strArray. Then use the [] operator of the string to get the first and third character (yes, strArray[i][j] is valid syntax).

Take a look at the example here:http://cplusplus.com/reference/string/string/operator%5B%5D/

EDIT: Use either counter++ or counter=counter+1. counter = counter++ may cause an infinite loop.
Last edited on
the assignment is to use a do while loop. i am not asking for the code as this is a homework assignment but i just need the sytax for it. my teacher sent me to a site that explains extracting from a string but this is an array.
this is the assignment

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.

everything i have read tells how to get the charactors i need from a string but not from an array
Ok.

1
2
3
4
5
6
int i=0;
do
{
    //...
    i++;
} while (i<5);

is equivalent to

1
2
3
4
5
int i;
for (i=0; i<5; i++)
{
    //...
}

strArray is an array of strings, so strArray[i] is a string. Use the substr function of your strings.

Info on substr function -> http://www.cplusplus.com/reference/string/string/substr/
thanks
Topic archived. No new replies allowed.