do {} while ( x && y) exits for x or y

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.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  /* 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>

using namespace 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;
    }
    else if (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;
    }
    else if (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;
    }
    else if ((reply.find("Loxobot") == 0) && (reply.find("stop") != 0))
    {
      cout << "I have found 'Loxobot' and did not find 'stop' in your reply." << endl;
    }
    else if ((reply.find("Loxobot") != 0) && (reply.find("stop") == 0))
    {
      cout << "I have not found 'Loxobot' but did find 'stop' in your reply." << endl;
    }
    else if ((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

http://www.cplusplus.com/reference/string/string/find/

http://www.cplusplus.com/reference/cstddef/size_t/
Last edited on
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.

http://www.cplusplus.com/reference/string/string/find/
closed account (j3Rz8vqX)
Look at the & section:
http://www.cplusplus.com/doc/boolean/

Hopefully this helps
if not... something to think about

Pseudo:
a=0;b=1;
c=a&b;
c==0;

I may be off, but I'm quite sure, this is what's got you stumped.

Good luck.
Last edited on
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;
int intro(1);
string reply;
signed int result1mk1 = reply.find("Loxobot ");
signed int result2mk1 = reply.find("stop");
signed int proceed1 = 0;
signed int proceed2 = 0;
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;
    if (reply.find("Loxobot ") == 0)
    {
      proceed1 = 1;
     // cout << "Proceed 1 passed." << endl;
    }
    if (reply.find("stop") == 8)
    {
      proceed2 = 1;
      // cout << "Proceed 2 passed" << endl;
    }
  } while ((proceed1 != 1) || (proceed2 != 1));
return 0;
}


Thank you for your help, I think this accomplishes what I was after.
Topic archived. No new replies allowed.