Hello, I'm just starting to get my head around cpp. My second do {} while () is exiting the loop when either one condition is met rather than both. I've found posts with similar issues but not one that I could relate well enough to make sense of.
/* 12/22/2013 Loxobot is my first C++ program written from scratch. It's
intended design is to query the user, then use the solicited response to
decide which action(s) is/are appropriate and execute. */
/* 12/22/2013 */
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int intro(1);
string reply;
bool result1mk1 = reply.find("Loxobot");
bool result2mk1 = reply.find("stop");
int main()
{
do
{
cout << "Hail, Loxo!" << endl;
intro = 0;
} while (intro == 1);
do
{
getline(cin, reply);
cout << "Looking for things in what you said..." << endl;
result1mk1 = reply.find("Loxobot");
cout << "Looking for 'Loxobot'..." << endl;
cout << "The raw result is ";
cout << reply.find("Loxobot") << endl;
cout << "boolean result: ";
cout << result1mk1 << endl;
result2mk1 = reply.find("stop");
cout << "Looking for 'stop'..." << endl;
cout << "The raw result is ";
cout << reply.find("stop") << endl;
cout << "boolean result: ";
cout << result2mk1 << endl;
if (reply.find("Loxobot") != 0)
{
cout << "I don't believe my raw finding of 'Loxobot' equals zero." << endl;
}
elseif (reply.find("Loxobot") == 0)
{
cout << "I believe my raw finding of 'Loxobot' equals zero." << endl;
}
else
{
cout << "The result of my finding of 'Loxobot' does not apply to '!= 0' or '== 0'." << endl;
}
if (reply.find("stop") != 0)
{
cout << "I don't believe my raw finding of 'stop' equals zero." << endl;
}
elseif (reply.find("stop") == 0)
{
cout << "I believe my raw finding of 'stop' equals zero." << endl;
}
else
{
cout << "The result of my raw finding of 'stop' does not apply to '!= 0' or '== 0'." << endl;
}
/* if ((reply.find("Loxobot") == 1) && (reply.find("stop") == 1))
{
cout << "I believe my finding of 'Loxobot' and 'stop' both equal one" << endl;
} // the raw result of var.find("") is always 0 for found or some giant
number for not found in the case of one word in the string. The value drops
signifigantly to consistent 7, 8, 9, and 15, if the word found follows another
word already found in the same expression such as ( condition && condition).
*/
if ((reply.find("Loxobot") != 0) && (reply.find("stop") != 0))
{
cout << "I have not found both 'Loxobot' AND 'stop' in your reply." << endl;
}
elseif ((reply.find("Loxobot") == 0) && (reply.find("stop") != 0))
{
cout << "I have found 'Loxobot' and did not find 'stop' in your reply." << endl;
}
elseif ((reply.find("Loxobot") != 0) && (reply.find("stop") == 0))
{
cout << "I have not found 'Loxobot' but did find 'stop' in your reply." << endl;
}
elseif ((reply.find("Loxobot") == 0) && (reply.find("stop") == 0))
{
cout << "I have found 'Loxobot' AND 'stop' in your reply." << endl;
}
else
{
cout << "I don't know wtf is going on here." << endl;
}
} while ((result1mk1 != 0) && (result2mk1 != 0)); /* this exits the do {}
while() loop when either condition is met (either is found, or == 0), instead
of needing both to be met to exit. It's intended to exit only when both
"Loxobot" and "stop" are found in the variable 'reply' and so both equal zero.
This behaves the same way using both the result of reply.find("") and
converting the variable to a bool and using != 0 or == 1. Not found as a bool
is 1 */
return 0;
}
You don't need your first do while loop it. It's conditions will never cause it to loop.
replace
1 2 3 4 5
do
{
cout << "Hail, Loxo!" << endl;
intro = 0;
} while (intro == 1);
with
cout << "Hail, Loxo!" << endl;
I think your problem may be that you are setting result1mk1 and result2mk1 as bool data types.
The find() function returns a value of size_t but sometimes integer can work also (size_t is just an unsigned int i think?). I'd try setting it to size_t or unsigned integer and see what happens.
.find returns an unsigned integer to the position of the first character than matches the string parameter, I'm not sure how it would behave with a bool data type.
Here are links to the find() function and info on size_t
The first loop is there as a placeholder to expound upon if I want to add other options for couts later :).
I've chopped the couts I used to make all the data types display for clarity, but in my initial attempt to debug this I assigned types raw( just the .find function), float, bool, signed int, unsigned int (outputs same as raw figure from .find), and string as values for the .find and found bool results in 1 (not found) and 0 for found. signed int returns -1 (not found) and 0 (found), and string wasn't a valid choice. after testing bool seemed the best choice; the raw value changes when a second .find is used.
My main issue is regardless of data type I assign to the .finds, even using the function itself, is the while ( x && y). If either condition is met, it exits the loop. Both words should be found in the string, but it exits the loop even if just "Loxobot" or just "stop" is found.
My main issue is regardless of data type I assign to the .finds, even using the function itself, is the while ( x && y). If either condition is met, it exits the loop.
Your main issue is that you don't understand the values you are assigning to x and y.
std::string::find does not return a boolean value, and an implicit conversion to bool will not give you what you want. A return value of 0 does not imply failure, and a return value that isn't 0 doesn't necessarily imply failure or success.
Please, read the documentation for the function you are using.