函数std::get()通常在文件结束时设置一个eofbit。因为c++11 std::unget()首先清除了这个位,然后开始取消最后一个字符读取。困扰我的是,在我到达eof之后,unget()似乎对我不起作用,它只是清除了一点,但实际上并没有破坏任何东西。对get()的任何后续调用都不会再次设置eofbit。这是故意的吗,我是不是遗漏了文档中的什么东西?
gcc版本6.3.1,-std=c++11
#include <iostream>
using namespace std;
int main ()
{
while ( !cin . eof () )
我正在尝试理解当输入数据比格式字符串预期的“短”时,C++11 std::get_time()的正确行为应该是什么。例如,下面的程序应该打印什么:
#include <ctime>
#include <iomanip>
#include <sstream>
#include <iostream>
int main (int argc, char* argv[])
{
using namespace std;
tm t {};
istringstream is ("2016");
is >> get_
int position = 0;
void set_position() {
std::cout << "Player " << player << "'s Turn (Enter 1-9): ";
while (!(std::cin >> position)) {
std::cout << "Player " << player << ", please enter a valid number between 1 an
huffmanFile.open("code.txt");
while(huffmanFile.peek() != EOF)
{
cin >> letters[c] >> numbers[c];
c++;
}
huffmanFile.close();
输入文件:A 23 B 35 C 47 D 0 E 12 F 11 G 92 H 18 I 17
在执行完这个之后,我尝试从我的main函数中输出c,但是它不会离开while循环。我已经试过调试了,但是没有用。我已经将其与我编写的许多其他程序进行了比较,但仍然一无所获。
#include<iostream>
int main()
{
int x, y;
cin >> x ;
cin >> y ; // This gets ignored
}
在上面的程序中,在第一个cin中按下Ctrl-D后,第二个cin被忽略。如何在第一个cin中按下ctr-D后使第二个cin工作。cin.ignore()和cin.clear()似乎不起作用。
我的问题是,如果我在第一次cin时按下ctrl-D,基本上我会把eof char留在i/p流中,它不会被读取,它会在那里缩进。现在我不能从流中删除这个字符,这样它就
#include <iostream>
int main()
{
int sum = 0, value = 0;
// read until end-of-file, calculating a running total of all values read
while (std::cin >> value)
sum += value; // equivalent to sum = sum + value
std::cout << "Sum is: " << sum <
简单的代码:
std::ifstream file("file.txt");
std::string line;
while(getline(file,line))
; //exhaust file
//in this sample code, for simplicity assert that the only possible "fail"
//is EOF (which it will always be under normal circumstances).
assert(!file.fail() || file.eof());
assert
根据Stroustrup的“编程:使用C++的原则和实践(第2版)”,下面的程序应该输出对象‘年龄’的-1值。
#include <iostream>
using namespace std;
int main()
{
int age = -1;
cin >> age;
cout << "\nAge:" << age;
}
INPUT: Carlos // string
OUTPUT: 0 // Why not -1 as Stroustrup states ?
C++ Standard: C++ 4.8.1
using namespace std;
int main(void)
{
int input;
while (true)
{
cin >> input;
ios_base::iostate state = cin.rdstate();
if (state == ios_base::eofbit)
// if (state == (ios_base::eofbit | ios_base::failbit)) doesn't work also
break;
以下是我的代码:
#include <iostream>
using namespace std;
int main() {
int num;
cout<<"Enter a number: ";
cin>>num;
cout<<"The number is: "<<num;
return 0;
}
如果我在cin>>num的提示符上输入一个字符或字符串而不是整数,则每次都以0的形式返回num的值。是因为它是在C++中以这种方式实现的,还是我遗漏了一些琐碎的概念?任何回
在C++引物中,我找到了以下代码:
if (cin.fail())
{ // bad input
cerr<< "bad data, try again"; // warn the user
cin.clear(istream::failbit); // reset the stream
continue; // get next input
}
我对为什么使用istream::failbit来设置错误状态标志感到困惑,我的意思是,由于发生了错误(因此,如果块必须设置failbit,那么流就在中,那么必须设置failbit)
目前,我是自学C++初级五。有件事我不确定。(我在F.A.Q上找不到确切的相关问题)。
考虑一下这个while循环:
while(std::cin>>value){...} \\value here was defined as int.
课本上写着:
该表达式从标准输入中读取下一个数字,并以值存储该数字。输入运算符(§1.2,第8页)返回其左操作数,在本例中为std::cin。因此,这个条件测试std::cin.When我们使用istream作为条件,其效果是测试流的状态。如果流是有效的,也就是说,如果流没有遇到错误,那么测试就成功了。
我的问题是:std::cin是
它是一个菜单驱动的程序,当int被传递给变量选项时,它工作得非常好,但是当char被传递时,它会运行到一个无限循环中。
int main(){
while(true){
int options{0};
cout<<"\nYour choice >>";
cin>>options; //this line doesnt execute after any char(say r) is given as an input
switch(options){
case 1:login()
我在这段代码中看到了一个不寻常的问题:
#include <iostream>
using namespace std;
int main()
{
int number;
bool repeat=true;
while (repeat)
{
cout<<"\nEnter a number:";
cin>>number;
cout<<"\nNumber is:"
<<number;
cout<<"\nRepeat?:";
cin>&g
while(cin.fail()){
cout<<"Error: Your line is too long.\nPlease enter a shorter line: ";
cin.clear(cin.good());
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
cin.getline(name, maxCh);
}
我使用cin.clear(cin.good());清除错误标志
但是使用cin.clear();也有效吗?
#include <iostream>
using namespace std;
int main() {
int n, a = 0xfffffff;
cin >> n;
while (n--) {
string s;
cin >> s;
int c = 0;
for (char ch : s)
c |= 1 << (ch - 'a');
a &= c;
}
cout <&
我正在尝试学习C++中的异常处理。
我想读两个整数,然后把它们分开,然后打印出来。
当第二个整数为零时,代码应该抛出一个异常,要求用户重新输入第二个整数,然后完成除法操作。
我编写了这样的代码:
#include <iostream>
#include <stdexcept>
using std::cin;
using std::cout;
using std::endl;
int main() {
int i1, i2;
cout << "Give two integers" << endl;
cin