cin 在输入字符时跳过
cin skips when a character is input
我有一个程序,我必须在其中输入两个双打,并且它必须在字符 '|' 时终止被输入。但是,当我输入字符“|”时cin 被跳过,程序只会 运行 而不会阻塞。如何在输入字符时终止程序?
我的代码是:
#include "std_lib_facilities.h"
int main()
{
bool start = true;
while(start)
{
double number1, number2 = 0;
cout << "Enter two doubles\n";
cin >> number1 >> number2;
if((number1 == double{'|'}) || (number2 == double{'|'}))
{
start = false;
}
cout << "The value of number1 is: " << number1 << '\n';
cout << "The value of number2 is: " << number2 << '\n';
}
return 0;
}
double是一种内置数据类型,用于保存数字从2.3E-308到1.7E+308(包括十进制)点数)。另一方面,垂直斜杠 (|) 是一个字符。 char 可以容纳任何单数字符(例如:'a'、'7'、'#'、'|' 等) .您试图在程序中输入双精度数据类型的字符,这是不可接受的。使用 char 数据类型而不是 double。
//Using char
#include<iostream>
int main() {
bool start = true;
while(start) {
char number1, number2;
std :: cout << "Enter two characters\n";
std :: cin >> number1 >> number2;
if((number1 == '|') || (number2 == '|'))
start = false;
std :: cout << "The value of number1 is: " << number1 << std :: endl;
std :: cout << "The value of number2 is: " << number2 << std :: endl;
}
return 0;
}
这里"number1" & "number2"可以存储单数、字母或特殊字符。如果您想存储双精度和字符,请尝试使用“std :: string”而不是 char。
//Using string
#include<iostream>
int main() {
bool start = true;
while(start) {
std :: string number1, number2;
std :: cout << "Enter two characters\n";
std :: cin >> number1 >> number2;
if((number1 == "|") || (number2 == "|"))
start = false;
std :: cout << "The value of number1 is: " << number1 << std :: endl;
std :: cout << "The value of number2 is: " << number2 << std :: endl;
}
return 0;
}
首先,您需要了解 C++ 中的数据类型和字符串。
其次,考虑讲座,为您的输入选择合适的类型。
像这样应该可以解决您的问题:
#include "std_lib_facilities.h"
int main()
{
bool start = true;
while(start)
{
double number1=0, number2 = 0;//here you were initializing only number2 to 0
cout << "Enter two doubles\n";
char condition;
cin >> number1 >> number2;
cin>>condition;//to continue reading doubles give it something different from |
if(condition == '|')
{
start = false;
}
cout << "The value of number1 is: " << number1 << '\n';
cout << "The value of number2 is: " << number2 << '\n';
}
return 0;
}
您不能将 |
字符读入 double
变量。 operator>>
将无法提取号码,并将在流上设置 failbit
,您可以对其进行测试,例如:
#include "std_lib_facilities.h"
int main()
{
do
{
double number1, number2;
cout << "Enter two doubles\n";
if (!(cin >> number1 >> number2))
break;
cout << "The value of number1 is: " << number1 << '\n';
cout << "The value of number2 is: " << number2 << '\n';
}
while (true);
return 0;
}
否则,先将输入读取为字符串,然后根据需要验证格式,例如:
#include "std_lib_facilities.h"
#include <string>
int main()
{
do
{
std::string input1, input2;
double number1, number2;
cout << "Enter two doubles\n";
if (!(cin >> input1 >> input2))
break;
if ((input == "|") || (input2 == "|"))
break;
number1 = std::stod(input1);
number2 = std::stod(input2);
cout << "The value of number1 is: " << number1 << '\n';
cout << "The value of number2 is: " << number2 << '\n';
}
while (true);
return 0;
}
我有一个程序,我必须在其中输入两个双打,并且它必须在字符 '|' 时终止被输入。但是,当我输入字符“|”时cin 被跳过,程序只会 运行 而不会阻塞。如何在输入字符时终止程序?
我的代码是:
#include "std_lib_facilities.h"
int main()
{
bool start = true;
while(start)
{
double number1, number2 = 0;
cout << "Enter two doubles\n";
cin >> number1 >> number2;
if((number1 == double{'|'}) || (number2 == double{'|'}))
{
start = false;
}
cout << "The value of number1 is: " << number1 << '\n';
cout << "The value of number2 is: " << number2 << '\n';
}
return 0;
}
double是一种内置数据类型,用于保存数字从2.3E-308到1.7E+308(包括十进制)点数)。另一方面,垂直斜杠 (|) 是一个字符。 char 可以容纳任何单数字符(例如:'a'、'7'、'#'、'|' 等) .您试图在程序中输入双精度数据类型的字符,这是不可接受的。使用 char 数据类型而不是 double。
//Using char
#include<iostream>
int main() {
bool start = true;
while(start) {
char number1, number2;
std :: cout << "Enter two characters\n";
std :: cin >> number1 >> number2;
if((number1 == '|') || (number2 == '|'))
start = false;
std :: cout << "The value of number1 is: " << number1 << std :: endl;
std :: cout << "The value of number2 is: " << number2 << std :: endl;
}
return 0;
}
这里"number1" & "number2"可以存储单数、字母或特殊字符。如果您想存储双精度和字符,请尝试使用“std :: string”而不是 char。
//Using string
#include<iostream>
int main() {
bool start = true;
while(start) {
std :: string number1, number2;
std :: cout << "Enter two characters\n";
std :: cin >> number1 >> number2;
if((number1 == "|") || (number2 == "|"))
start = false;
std :: cout << "The value of number1 is: " << number1 << std :: endl;
std :: cout << "The value of number2 is: " << number2 << std :: endl;
}
return 0;
}
首先,您需要了解 C++ 中的数据类型和字符串。 其次,考虑讲座,为您的输入选择合适的类型。 像这样应该可以解决您的问题:
#include "std_lib_facilities.h"
int main()
{
bool start = true;
while(start)
{
double number1=0, number2 = 0;//here you were initializing only number2 to 0
cout << "Enter two doubles\n";
char condition;
cin >> number1 >> number2;
cin>>condition;//to continue reading doubles give it something different from |
if(condition == '|')
{
start = false;
}
cout << "The value of number1 is: " << number1 << '\n';
cout << "The value of number2 is: " << number2 << '\n';
}
return 0;
}
您不能将 |
字符读入 double
变量。 operator>>
将无法提取号码,并将在流上设置 failbit
,您可以对其进行测试,例如:
#include "std_lib_facilities.h"
int main()
{
do
{
double number1, number2;
cout << "Enter two doubles\n";
if (!(cin >> number1 >> number2))
break;
cout << "The value of number1 is: " << number1 << '\n';
cout << "The value of number2 is: " << number2 << '\n';
}
while (true);
return 0;
}
否则,先将输入读取为字符串,然后根据需要验证格式,例如:
#include "std_lib_facilities.h"
#include <string>
int main()
{
do
{
std::string input1, input2;
double number1, number2;
cout << "Enter two doubles\n";
if (!(cin >> input1 >> input2))
break;
if ((input == "|") || (input2 == "|"))
break;
number1 = std::stod(input1);
number2 = std::stod(input2);
cout << "The value of number1 is: " << number1 << '\n';
cout << "The value of number2 is: " << number2 << '\n';
}
while (true);
return 0;
}