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.
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):
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...
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 constchar * pointers. The value of the ternary expression becomes one or the other of these pointers. cout has an overload for constchar * pointers so the the result of the ternary expression prints the desired text.
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>
usingnamespace 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!