looping through array of string elements

May 26, 2015 at 1:21am
I am trying to compare the variable named response which contains a user's input, with an array of acceptable input strings named request001. When it finds an acceptable string, I want to it drop out of the loop and execute the next line of code. However, it just continues looping even when the input matches one of the strings in the array.

Here is my code:

#include <iostream>
#include <string>
using namespace std;

int main ()
{
//declare variables
int i(1);
string response;
string request001 [12] { "stand", "stand up", "get up", "rise", "arise", "jump up", "leap up", "pop up", "spring up", "get to my feet", "jump to my feet", "spring to my feet" };

//Initiate dialogue with user.
cout << "\nWhat would you like to do?\n\n";

//Get user's input
getline (cin, response);

//Loops through an array named request001, comparing with a string named response.
while (request001[i] != response && i<13) {
i++;
cout << "\nI don't understand, \"" << response << ",\" please try again.\n\n";
getline (cin, response);
}

//Initiate next dialogue with user.
cout << "\nElevator opens.\n\n";

//Termnate program.
return 0;
}
May 26, 2015 at 1:58am
I suggest you output both i and request001[i] as the first command in the loop. You will find your problem
May 26, 2015 at 3:56am
Thank you, ats15.

I did as you suggested, and perhaps I am too new at this to fully understand the result. (I haven't played with C++ since the early 2000s.)

Here is what it output for i and for request001[i] the first two times after I deliberately input unacceptable responses:

1
stand up

2
get up

I can see that it is not evaluating each new input against the entire array as I had hoped. Would you be able to let me know how to accomplish this?
May 26, 2015 at 6:01am
You should have noticed that you never tested for "stand". That is because in c++ array numbering starts from 0.
Also, please use code tags (the <> button on the left of your editing box), since it will allow for nice, formatted text.
As for your problem, you need two loops. One is a while loop, that just checks if you already entered a valid answer. You can use a bool type of condition, say
1
2
3
4
5
6
7
8
9
10
bool validAnswer(false);
while(!validAnswer)
{
   //Initiate dialogue with user.
   cout << "\nWhat would you like to do?\n\n";

   //Get user's input
   getline (cin, response);
   ...
}

Instead of the dots, now you can use a for loop, to check if response is in the array. Notice again that your for loop goes from 0 to 11
for(size_t i=0;i<12;i++)
If you find a valid answer, set validAnswer to true, and exit the for loop.
The last thing in the while loop is to print the "try again" message if validAnswer is false. If false, it will then try again. If true, you exit the while loop.
May 28, 2015 at 6:55am
Alright, I am happy to report that with the help I received from ats15, (nobody else responded), I finally finished writing a short program that compares a string of user input against a preset string type array of valid inputs. Thank you for reminding me about the zero starting point in my string array. While not exactly the way you suggested, it certainly gave me enough to find my own way. The finished program is here:

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
#include <string>
#include <algorithm>
#include <iostream>
#include <cstdlib>
using namespace std;

int main ()
{
  string str1 [5] = {"stand", "stand up", "get up", "arise", "rise"};
  string str2;
  bool validInput = false;
  
  cout << "Enter command:\n\n";
  getline (cin, str2);

	while ( validInput != true )  // while validInput is not equal to zero, or null
	{
		for (int i = 0; i < 5; i++)
		{
			if (str2.compare(str1[i]) == 0)
			{
				validInput=true;
			}
		}
		if (validInput != true)
		{
		cout << "Enter another command:\n\n";
	        getline (cin, str2);
		}
	    
	}
	

  return 0;
}


Again, THANK YOU! =o)
Last edited on May 28, 2015 at 6:57am
May 28, 2015 at 10:36am
add "i=1;"behind the code "getline (cin, response);"
May 28, 2015 at 10:46am
sorry, I was mistake. should be:

while (request001[i] != response && i<12) {
i++;
if ( i >= 12 )
{
cout << "\nI don't understand, \"" << response << ",\" please try again.\n\n";
getline (cin, response);
i = 1;
}//end if ( i >= 12 )
}
Last edited on May 28, 2015 at 10:50am
May 29, 2015 at 2:45am
thanks for the help, but my code in my reply above yesterday, May 27, 2015 at 11:55 PM, works well. I was just showing the result. =o)
Topic archived. No new replies allowed.