C++作业(循环终止时)
C++ homework (while loop termination)
- 编写一个由 while-loop 组成的程序,该循环(每次围绕循环)读取两个整数,然后打印它们。输入终止的"|"时退出程序。
你怎么写这个?
当我检查任何一个 int 是否等于 |
他们永远不会这样做,因为如果输入 |
,它们的值为零。如果输入|
,我的程序当前将永远重复。我不知道如何使用 ints 访问非 int |
的正确值,也不知道如何阻止它永远重复。
1 - #include <iostream>
2 -
3 - using namespace std;
4 -
5 - int main()
6 - {
7 - int val1=0, val2=0;
8 - while(val1 != '|' && val2 != '|')
9 - {
10- cout << "Enter 2 integers, terminate with "|"" << endl;
11- cin >> val1;
12- if (val1 == '|')
13- {
14- return 0;
15- }
16- cin >> val2;
17- if (val2 == '|')
18- {
19- return 0;
20- }
21- cout << val1 << " " << val2 << "nn";
22- }
23-
24- return 0;
25- }
你有两个问题。首先,您不是在"启动泵",其次您正在尝试将管道转换为int,因此这将是一个问题。不要在开头将 val1 和 val2 转换为整数,而是将它们键入为字符串。由于您只是打印值而不进行任何数学运算,因此这无关紧要。从那里,您需要先阅读一次输入,然后再放入 while 语句。你这样做,所以如果你立即得到一个管道,程序将停止。
int main()
{
string val1=0, val2=0;
//grab the values first, before checking at the head of the loop
cin >> val1;
cin >> val2;
while(val1 != '|' && val2 != '|')
{
//You may want to check for empties in your while as well
cout << "Enter 2 integers, terminate with "|"" << endl;
cout << val1 << " " << val2 << "nn";
//recharge val1 and val2 before looping next time.
cin >> val1;
cin >> val2;
}
return 0;
}
不是直接从stdin
读取val1
和val2
,而是阅读一行文本。如果该行不以 |
开头,则使用 sscanf
或 istringstream
从该行中提取数字。如果该行以 |
开头,则断开while
循环。
#include <stdio.h>
#include <iostream>
using namespace std;
const int LINE_SIZE = 200; // Make it large enough.
int main()
{
char line[LINE_SIZE] = {0};
int val1=0, val2=0;
// Break out of the loop after reading a line and the first character
// of the line is '|'.
while( true )
{
cout << "Enter 2 integers, teminate with "|"" << endl;
// Read the entered data as a line of text.
cin.getline(line, LINE_SIZE);
if ( !cin )
{
// Deal with error condition.
break;
}
// If the first character of the line is '|', break out of the loop.
if ( line[0] == '|' )
{
break;
}
// Read the numbers from the line of text.
int n = sscanf(line, "%d %d", &val1, &val2);
if ( n != 2 )
{
// Deal with error condition.
continue;
}
cout << val1 << " " << val2 << "nn";
}
return 0;
}
您可以使用
istream.peek()
来判断下一个字符是什么,而无需消耗它:
int main()
{
int val1 = 0;
int val2 = 0;
while (std::cin.peek() != '|')
{
std::cin >> val1;
std::cin >> val2;
std::cout << val1 << " " << val2 << std::endl;
}
return 0;
}
检查 EOF 也是一种很好的做法:
while (std::cin.peek() != EOF && std::cin.peek() != '|')
而且您不必使用两个变量,将它们放在作用域内:
int main()
{
while (std::cin.peek() != '|')
{
int val = 0;
std::cin >> val;
std::cout << val << " ";
std::cin >> val;
std::cout << val2 << std::endl;
}
return 0;
}
尽管如果您需要跳过白色字符(如""),这将不起作用。您应该尝试查看此相关问题来解决此问题:如何正确使用 cin.peek()
请关注代码中的注释
// while (cin >> variable), read whitespace-separated values in type condition.
// We use character '|' to terminate the input — anything that isn’t an int type can be used.
// for string type, Ctrl+Z terminates an input stream under Windows and Ctrl+D does that under Unix.
int main ()
{
int val1;
int val2;
cout << "Enter 2 integers: n";
while (cin >> val1 >> val2 )
cout << "You entered: n" << val1 <<'n' << val2 << 'n';
return 0;
}
相关文章:
- while 循环不以哨兵值终止
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- C++ 即使不满足条件,循环也会终止
- 为什么 EOF 在循环扫描期间没有终止?
- 对于检查 >=0 终止条件时的循环索引类型
- 程序在终止循环后不接收输入
- 即使我尝试递减 t,While 循环也没有终止
- 如何修复不会在退出选择时终止的 do-while 循环
- 如何用字符终止整数输入循环?
- 读入变量时循环终止,'Range_error' on C++
- 当我输入非整数值时,我的二次方程代码终止,如何让它正确循环?
- 如何正确终止此循环?
- 多个查询的循环过早终止
- 程序终止,状态为 -1073741510 在 for 循环的中间,将输入分配给二维数组
- 终止具有特定字符的循环
- 程序终止,而不是继续整数输入验证循环
- 程序似乎在 for 循环 - C++后静默终止
- 循环终止不正确时
- C :终止循环的最优雅方法,该循环在字符串向量上迭代
- 如果未检测到alpha,则终止循环