I am brand new to C++ programming, and am completely stuck on a program.
In the program I am supposed to create a string array that stores 5 user input words, and the string constant "END_OF_ARR AY' in the last element. Then using the substr() display the first and third letter of each element. What I have now gets five word from the user, then it just displays the 1st and 3rd letter of the constant "END_OF_FIL E" I don't know if the problem is in the array itself or in the loop used to display the letters. Any help would be great!
In the program I am supposed to create a string array that stores 5 user input words, and the string constant "END_OF_ARR AY' in the last element. Then using the substr() display the first and third letter of each element. What I have now gets five word from the user, then it just displays the 1st and 3rd letter of the constant "END_OF_FIL E" I don't know if the problem is in the array itself or in the loop used to display the letters. Any help would be great!
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word;
string words[6]; //initializes array
words[5] = "END_OF_ARRAY"; // sets "END_OF_ARRAY" to las element
int i;
cout << "Type 5 words" << endl;
//Loop to get 5 words from user
for(i = 0; i < 5; i ++)
cin >> word;
word = words[i];
cout << endl;
//Loop to display data from array
do
{
cout << words[i].substr(0,1) << " " << words[i].substr(2,1) << endl;//substr() to get 1st and 3rd letter
}while(i = 0, i < 5, i ++);
cin.get();
cin.get();
return 0;
}
Comment