通过按ENTER中的C 中的断点语句
break statement in c++ by pressing enter
#include <iostream>
using namespace std;
int main()
{
int n,t=0,k=0;
cin>>n;
char data[n][100];
int num[n];
for(int i=0;i<n;i++)
{
while(1)
{
cin>>data[i][t];
cout<<data[i][t]<<endl;
if(data[i][t]=='n') break;
k++;
if(k%2==1) t++;
}
cout<<i;
num[i]=(t-2)/2;
k=0;
t=0;
}
for(int i=0;i<n;i++)
{
while(1)
{
cout<<data[i][t];
if(t==num[i]) break;
t++;
}
t=0;
}
}
这是我用C 编写的代码,该代码从用户给出的每个单词的起点中均匀编号的字符,但是当按下输入输入时,请输入循环,但循环不会破坏
while(1)
{
cin>>data[i][t];
cout<<data[i][t]<<endl;
if(data[i][t]=='n') break;
k++;
if(k%2==1) t++;
}
默认情况下使用"输入"操作员>>
跳过白空间,而newline是一个白空间字符。因此,发生的事情是>>
操作员只需等待输入一些非白色空间输入。
要告诉输入不要跳过白空间,您必须使用std::noskipws
操纵器:
cin>>noskipws>>data[i][t];
有一些方法可以在C 中实现OP试图做的事情。我会开始避免使用可变长度阵列,该阵列不在标准中,而是使用std::string
s和std::vector
s。
一个选项是使用std::getline
从输入中读取整个行,然后处理结果字符串,以仅保留偶数的前半部分:
#include <iostream>
#include <string>
#include <vector>
int main() {
using std::cin;
using std::cout;
using std::string;
cout << "How many lines?n";
int n;
cin >> n;
std::vector<string> half_words;
string line;
while ( n > 0 && std::getline(cin, line) ) {
if ( line.empty() ) // skip empty lines and trailing newlines
continue;
string word;
auto length = line.length() / 2;
for ( string::size_type i = 1; i < length; i += 2 ) {
word += line[i];
}
half_words.push_back(word);
--n;
}
cout << "nShrinked words:nn";
for ( const auto &s : half_words ) {
cout << s << 'n';
}
return 0;
}
另一种是,就像Joachim Pileborg在答案中所做的那样,通过使用std::noskipws
Manipolator的格式输入功能禁用领先的空格,然后一次阅读一个char:
// ...
// disables skipping of whitespace and then consume the trailing newline
char odd, even;
cin >> std::noskipws >> odd;
std::vector<string> half_words;
while ( n > 0 ) {
string word;
// read every character in a row till a newline, but store in a string
// only the even ones
while ( cin >> odd && odd != 'n'
&& cin >> even && even != 'n' ) {
word += even;
}
// add the shrinked line to the vector of strings
auto half = word.length() / 2;
half_words.emplace_back(word.begin(), word.begin() + half);
--n;
}
// ...
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 为什么是0;C++中的有效语句
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 无法删除指针,已触发断点
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 我似乎对if/else的基本语句有问题:/
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 条件断点在不应该触发时触发
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 通过按ENTER中的C 中的断点语句
- 如何在return语句上全局设置条件断点
- 是否有一种方法可以使编译器在catch子句中自动命中语句(断点)
- Visual c++ if语句.当前不会命中该断点