我刚刚在这个答案中发现了一条评论说iostream::eof在循环条件下使用"几乎肯定是错误的".我通常使用类似的东西while(cin>>n)- 我猜是隐式检查EOF,为什么检查eof显式使用while (!cin.eof())错误?
它与scanf("...",...)!=EOF在C中使用有何不同(我经常使用没有问题)?
我正在尝试使用ss.fail错误地处理从字符串到int的转换,并且在程序循环回输入后,即使我输入整数,它也会一直出错.请注意,这只发生在错误句柄循环之后.我已经尝试过ss.clear(),cin.ignore等,当我输入一个整数时它仍然无限循环.我如何正确地错误处理这个?
string strNumber; //User to input a number
stringstream ss; //Used to convert string to int
int number; //Used to convert a string to number and display it
bool error = false; //Loops if there is an input error
do{
//Prompts the user to enter a number to be stored in string
cout << endl << "Enter an integer number: ";
getline(cin, strNumber);
//Converts the string number to int and loops otherwise
ss << strNumber;
ss >> number;
if(ss.fail()){
cout …Run Code Online (Sandbox Code Playgroud) 我是C++的新手,我想知道在布尔数据的情况下函数cin是如何工作的.比方说吧:
bool a;
cin >> a;
Run Code Online (Sandbox Code Playgroud)
我明白,如果我给0或1,我的数据a将是真或假.但是如果我给另一个整数甚至字符串会发生什么?
我正在处理以下代码:
#include <iostream>
using namespace std;
int main() {
bool aSmile,bSmile;
cout << "a smiling ?" << endl;
cin >> aSmile;
cout << "b smiling ?" << endl;
cin >> bSmile;
if (aSmile && bSmile == true)
{
cout << "problem";
}
else cout << "no problem";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我为两个布尔值赋予0或1的值,则没有问题.但如果我给另一个整数,这是输出:
a smiling ?
9
b smiling ?
problem
Run Code Online (Sandbox Code Playgroud)
我没有被要求输入bSmile的任何值,"cin >> bSmile"似乎被跳过了.如果我给aSmile一个字符串值,也会发生同样的情况.
发生了什么 ?
大家好!:)
我有一个字节字符串,如下所示:
"1,3,8,b,e,ff,10"
Run Code Online (Sandbox Code Playgroud)
如何将此字符串拆分为包含以下值的BYTE的std :: vector:
[0x01,0x03,0x08,0x0b,0x0e,0xff,0x10]
我正在尝试使用','作为分隔符来拆分字符串,但是我在使用它时遇到了一些麻烦.有人可以帮助我解决这个问题吗?
所以我试过这个:
std::istringstream iss("1 3 8 b e ff 10");
BYTE num = 0;
while(iss >> num || !iss.eof())
{
if(iss.fail())
{
iss.clear();
std::string dummy;
iss >> dummy;
continue;
}
dataValues.push_back(num);
}
Run Code Online (Sandbox Code Playgroud)
但是这会将ascii字节值推送到向量中:
49 //1
51 //3
56 //8
98 //b
101 //e
102 //f
102 //f
49 //1
48 //0
Run Code Online (Sandbox Code Playgroud)
我试图填充向量:
0x01
0x03
0x08
0x0b
0x0e
0xff
0x10
Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,此时只接受用户输入的 int,如果不是,请继续询问用户,直到获得正确的整数。这是下面的代码:
cout << "enter two integers: " << endl;
string input1, input2;
cin >> input1;
cin >> input2;
while (//if they are not integers)
...//ask again
Run Code Online (Sandbox Code Playgroud)
如您所见,我使用字符串来存储输入,但我不知道如何检查此字符串是否仅包含一个整数。
我试图在C++中使用strtok来获取字符串的标记.但是,我看到在5个运行的一个中,函数返回的标记是不正确的.有人可以,建议可能是什么问题?
示例代码重现了我面临的问题:
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
#define DEBUG(x) cout<<x<<endl;
void split(const string &s, const char* delim, vector<string> & v)
{
DEBUG("Input string to split:"<<s);
// to avoid modifying original string first duplicate the original string and return a char pointer then free the memory
char * dup = strdup(s.c_str());
DEBUG("dup is:"<<dup);
int i=0;
char* token = strtok(dup,delim);
while(token != NULL)
{
DEBUG("token is:"<<string(token));
v.push_back(string(token));
// the call is treated as a subsequent calls to strtok:
// the function …Run Code Online (Sandbox Code Playgroud) 我只是想编写一个从cin读取的简单程序,然后验证输入是一个整数.如果确实如此,我将打破我的while循环.如果没有,我会再次要求用户输入.
我的程序编译并运行得很好,这很棒.但是如果输入非数字值,它不会提示输入新内容.是什么赋予了?
#include <iostream>
using namespace std;
int main() {
bool flag = true;
int input;
while(flag){
try{
cout << "Please enter an integral value \n";
cin >> input;
if (!( input % 1 ) || input == 0){ break; }
}
catch (exception& e)
{ cout << "Please enter an integral value";
flag = true;}
}
cout << input;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个函数解析来自std :: cin的一些输入,并在成功时返回一个整数值.我需要根据返回值检测解析输入时是否出错.零是有效的回报.我可以用什么来区分有效零和"坏输入"?
如果我需要发布代码作为示例,请告诉我.谢谢!
[编辑]
int parseInput(){
int calculatedValue = 0;
bool parseOk = true;
/* Parse cin for valid input.*/
if (parseOk) {
/* Perform calculation. Can be zero */
}
return calculatedValue; // Any integer is valid
}
Run Code Online (Sandbox Code Playgroud)