Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy isn't this while loop exiting?
    primarykey
    data
    text
    <p>I wrote a console program to help me test a function library I coded. Part of it is this piece of code:</p> <pre><code>char insertChoice[2] = {'9'}; while (insertChoice[0] != '0') { cout &lt;&lt; "\nEnter a string:\n"; char insertStringInput[256]; cin.getline(insertStringInput, 255); char insertChoice[2]; insertChoice[0] = '9'; cout &lt;&lt; "\nWhere would you like to insert the substring?\n\n 1) At the beginning of the string\n 2) At the end of the string\n\nInput: "; cin &gt;&gt; insertChoice; cin.ignore(); while (insertChoice[0] != '1' &amp;&amp; insertChoice[0] != '2') { cout &lt;&lt; "\nInvalid input.\nWhere would you like to insert the substring?\n\n 1) At the beginning of the string\n 2) At the end of the string\n\nInput: "; cin &gt;&gt; insertChoice; cin.ignore(); } cout &lt;&lt; "\nEnter the substring you would like to insert: "; char insertSubstring[256]; cin.getline(insertSubstring, 255); std::string used = "", substr = ""; used += insertStringInput; substr += insertSubstring; char insertOutputChoice[2]; insertOutputChoice[0] = '1'; if (insertChoice[0] == '1') insertOutput(insertInBeginning(used, substr)); else insertOutput(insertInEnd(used, substr)); cin &gt;&gt; insertOutputChoice; cin.ignore(); if (insertOutputChoice[0] == '1') { ofstream outfile("logfile.txt", ios::app); outfile &lt;&lt; "Test type: Insert Substring\n"; outfile &lt;&lt; "Test carried out on: " &lt;&lt; __DATE__ &lt;&lt; "; " &lt;&lt; __TIME__ &lt;&lt;"\n"; outfile &lt;&lt; "PARAMETERS:\n"; outfile &lt;&lt; "usedString: \"" &lt;&lt; insertStringInput &lt;&lt; "\"\n"; outfile &lt;&lt; "insertString: \"" &lt;&lt; insertSubstring &lt;&lt; "\"\n"; outfile &lt;&lt; "function used: " &lt;&lt; (insertChoice[0]=='1'?"insertInBeginning":"insertInEnd") &lt;&lt; "\nOUTPUT:\n"; outfile &lt;&lt; "\"" &lt;&lt; (insertChoice[0]=='1'?insertInBeginning(used, substr):insertInEnd(used, substr)) &lt;&lt; "\"\n\n"; outfile.close(); cout &lt;&lt; "\nWould you like to do another string insertion test? [y/n]: "; char insertConfirm[2]; insertConfirm[0] = ' '; while (tolower(insertConfirm[0]) != 'y' &amp;&amp; tolower(insertConfirm[0] != 'n')) { cin &gt;&gt; insertConfirm; cin.ignore(); if (tolower(insertConfirm[0]) != 'y' &amp;&amp; tolower(insertConfirm[0] != 'n')) cout &lt;&lt; "\nInvalid input. Would you like to do another string insertion test? [y/n]: "; } if (insertConfirm[0] == 'n') insertChoice[0] = '0'; } } </code></pre> <p>However, the <code>while (insertChoice[0] != '0')</code> loop does not exit when the user types in <code>insertOutputChoice</code> as 1, regardless of whether the user types in <code>insertConfirm</code> as <code>y</code> or <code>n</code> even though it is supposed to exit when <code>insertConfirm</code> is typed in as <code>n</code>.</p> <p><code>insertOutput</code> looks as looks as follows:</p> <pre><code>void insertOutput(std::string substrOut) { cout &lt;&lt; "\nThe new string generated is:\n"; cout &lt;&lt; substrOut; cout &lt;&lt; "\n\n1) Generate a log file of this test\n"; cout &lt;&lt; "2) Insert another substring into a string\n\n"; cout &lt;&lt; "0) Finish testing string insertion\n\n\n"; cout &lt;&lt; "Input: "; } </code></pre> <p>Please excuse the messy, unoptimized code. My first priority is to get this done, and I usually leave optimization until last.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload