在下面的代码中,我是否可以接受多个输入,进行一些计算(比如最后一个字符)并在最后打印......然后再次接受输入直到 5 次?
#include <iostream>
using namespace std;
int main ()
{
char name;
int i=0;
while(i != 5){
while(!(name != '<' || name != '>')){
cin>>name;
//do some calculation with the inputs
//cout<<name;
}
i++;
cout<<name;
//print the result of calculation this loop
}
}
出于某种原因,我不允许使用string
,或者array
,或者break
,除了之外没有任何库iostream
。是否可以使用循环?什么是替代品?
EDIT:: 在上面的代码中,我想确定最后输入的是什么。如果我输入 asdf>
然后我得到 >>>>>
。我希望它打印 >>
并返回循环并要求我再拍一次。
最佳答案
后内while
终止 name
持有 <
或 >
并且在下一次遇到内部 while
之前不会重置,立即终止为 name
还是<
或 >
.只需重置 name
内部之前 while
或轻微重组:
while (cin >> name && !(name != '<' || name != '>'))
{
}
关于c++ - 使用循环多次连续输入字符串等字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15505486/