While Loop Doesn't execute update statement

Dec 8, 2017 at 5:36am

I'm trying to initialize a while loop to a single number, going off the Boolean logic, by letting my block code run if the entry is 1. However, when i try to read the update while statement at the end of my code, it won't run it, it skips over it and loops the code forever.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
int main(){
	
	char a[50] = { ' ' }; int x;
	cout << "Enter 1 to test for string, 0 otherwise." << endl;
	
	while (cin>>x){
	
		cout << "Enter characters (maximum 50 characters, when done press ctrl+z and enter-key): " << endl; int counter = 0;
		for (int i = 0; i < 50; i++) {
			cin >> a[i];
			counter++;
			if (cin.eof()) break;
		}counter--;
		if ((a[counter - 1] == 'H') && (a[counter - 2] == 'O')) cout << "Entered string is a hydroxide composite." << endl;
		else cout << "Not a hydroxide composite." << endl;
		for (int k = 0; k < counter; k++)cout << a[k];//check
		cout << endl;
		cout << "Enter 1 to test for another string, 0 otherwise." << endl;
	}
	return 0;
}

sorry if I missed something, this is my first time posting
Last edited on Dec 8, 2017 at 5:36am
Dec 8, 2017 at 8:53am
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string formula;
   cout << "Enter a molecular formula: ";   cin >> formula;
   cout << "This " << ( formula.find( "OH" ) != string::npos ? "contains" : "does not contain" ) << " a hydroxyl group\n";
}

Enter a molecular formula: C2H5OH
This contains a hydroxyl group




If you want it to repeat the query then you can use something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string formula;
   while ( true )
   {
      cout << "Enter a molecular formula (or empty string to end): ";
      getline( cin, formula );
      if ( formula == "" ) break;
      cout << formula << ( formula.find( "OH" ) != string::npos ? " contains" : " does not contain" ) << " a hydroxyl group\n\n";
   }
}



Enter a molecular formula (or empty string to end): C2H5OH
C2H5OH contains a hydroxyl group

Enter a molecular formula (or empty string to end): C6H6
C6H6 does not contain a hydroxyl group

Enter a molecular formula (or empty string to end): CH3COOH
CH3COOH contains a hydroxyl group

Enter a molecular formula (or empty string to end):
Last edited on Dec 8, 2017 at 9:13am
Dec 8, 2017 at 2:51pm
That's interesting, thanks! I totally get how that'll work not to mention how it's way less clunkier than mine. The only part I don't understand is the formula.find( "OH" ) != string::npos ? " contains" : " does not contain" part.

I'm only starting out and still not familiar with the type of functions there are. I'm assuming that's a function, at least the formula.find() part, but I don't understand the code after it.

I'm still interested in knowing what my mistake was and why the loop would ignore the cin commands and loop infinite times...
Dec 8, 2017 at 3:02pm
Yes, formula.find("OH") is a function. It is a member of the std::string class.
http://www.cplusplus.com/reference/string/string/find/?kw=string%3A%3Afind
It returns string::npos if the argument string was not found.

The rest of the statement is a ternary operator that returns one of two string literals based on whether find() returned string::npos (not found) or not.
http://www.cplusplus.com/forum/articles/14631/

" contains" and " does not contain" are both const char * pointers. The value of the ternary expression becomes one or the other of these pointers. cout has an overload for const char * pointers so the the result of the ternary expression prints the desired text.
Last edited on Dec 8, 2017 at 3:17pm
Dec 8, 2017 at 3:14pm
formula is a string here.
find() is a member function of the string class, and so
formula.find( "OH " ) will return the index of the first occurrence of "OH" (i.e. position of letter O here) if "OH" is in the formula. If it isn't found, it will return a characteristic number (which may differ from system to system) but which can always be found as string::npos. Thus
formula.find( "OH" ) != string::npos
will be true if it does genuinely find a hydroxyl group in the formula.

The second part of your issue is the "ternary operator" (google it) which boils down to
result = test ? outcome_if_test_is_true : outcome_if_test_is_false
and it is always shorthand for an if...else block. The first code is equivalent to the longer form
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string formula;
   cout << "Enter a molecular formula: ";
   cin >> formula;

   if ( formula.find( "OH" ) != string::npos )   // if the result of .find() was not equal to the not-found sentinel (string::npos)
   {                                             
      cout << "This contains a hydroxyl group\n";
   }
   else
   {
      cout << "This does not contain a hydroxyl group\n";
   }
}


Your code is a bit obscure, I'm afraid: you would find it much easier to use a std::string than a character array. Testing for eof is highly unreliable as well.

I'll post this, but I can see that I'm such a slow typist that I've been beaten to it!
Dec 10, 2017 at 3:16pm
Thank you both for the explanation, clears up so much!
Topic archived. No new replies allowed.