I am writing a program using an Array called words, when the user enters the word "END" the Array should output all the words pryor to that but for some reason all I get is the word "END" one time. Can anyone help me with this?
#include <iostream>
using namespace std;
int main( )
{
//Drill 4.
cout<<"THIS IS DRILL 4 "<< endl;
const int SIZE=3;
string words[SIZE];
string END;
for(int i=0;i<SIZE;i++)
{
cout<<"Enter a list of words: "<< endl;
cin>>words[i];
if(words[i]== END)
{
cout<<words[i]<<", ";
}
}
return 0;
}
You made a string called END. However no data was set, so the string is blank. Use this string END = "END"; to set END to the value you were expecting.
You'll notice some different behavior now, but it isn't right just yet.
Take the cout out of the for loop and place it later in the program. Get all of your inputs before dealing with the outputs. It helps you to segment your code. Find a way to "break" out of the for loop imidiately when the "END" string is entered. What could you use? How about break?
Anyways, my code above should help you from there.