我有一个程序可以读取用户的输入并将其打印在单独的行上。我正在尝试创建一个循环并退出循环,并在输入“退出”时终止程序。
例如,如果用户输入句子“退出该程序,程序将读取该句子并仅打印quit然后退出程序(不在单独的行上打印其余的句子)
这是我的代码:
<select class="styleclass" id="fencelist">
<option selected disabled hidden>Choose here</option>
<option value="Arrow">Arrow</option>
</select>
<select class="styleclass" id="stylelist">
<option selected disabled hidden>Choose here</option>
<option value="Fence">Fence</option>
<option value="1 Door Gate">1 Door Gate</option>
<option value="2 Door Gate">2 Door Gate</option>
</select>`
<select class="styleclass" id="panellist">
<option selected disabled hidden>Choose here</option>
<option value="Spear">Spear</option>
<option value="Triad">Triad</option>
</select>
<select class="styleclass" id="raillist">
<option selected disabled hidden>Choose here</option>
<option value="Scroll">Scroll</option>
<option value="Circle">Circle</option>
<option value="X">X</option>
</select>
答案 0 :(得分:2)
要在流中查找序列,每当一个字符与当前索引的字符匹配时,就将索引增加到触发序列。如果不匹配,请重置计数器。如果索引到达触发序列的末尾,则操作完成:
int quit_index = 0 ;
const char* quit_trigger = "quit" ;
// read up to 100 characters from stdin, print each word on a line
// until the quit trigger sequence is read.
while( quit_trigger[quit_index] != 0
n < MAXC &&
(c = getchar ()) != EOF && c != '\n')
{
if (c == ' ')
printf ("\n");
else
printf ("%c", c);
n++;
// While sequence matches the trigger sequence, increment index,
// otherwise reset to zero on mismatch.
quit_index = (c == quit_trigger[quit_index]) ? quit_index + 1 : 0 ;
}