c++ - 有没有办法将 cin.fail 和 cin.clear 翻译成 C 编程语言?

标签 c++ c

我以前在 C++ 中使用过 clear 函数,但我不知道它在 C 中是如何工作的。 我在某处读到 C 不知道屏幕并且 C 中不存在 clear 函数。是否有另一种翻译方法? 如果 .clear.fail 函数不存在:

最后的问题:cin.failcin.clear还有cin.ignore用C怎么写?

 int main(){
int n;
cout << "Please enter number shapes to check: ";
cin >> n;

while(cin.fail() || n<1) {
    cout << "Your number of shapes is invalid. " << endl;
    cin.clear();
    cin.ignore(256,'\n');
    cout << "Enter the number of shapes to check: ";
    cin >> n;

最佳答案

查看feof(3) , 以及相关的 ferror, clearerr.

另见 getline(3)

注意 fscanf(3) (so sscanf) 返回一个计数并知道 %n

关于c++ - 有没有办法将 cin.fail 和 cin.clear 翻译成 C 编程语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47022288/

相关文章:

c++ - 如何在 VS C++ 中使用 IA32 指令 'fabs'?

c++ - 为了模拟对象,我们是否应该将所有成员函数声明为虚拟(C++)?

c++ - 使用 unique_ptr 实现的列表的迭代器

c++ - 引用 Qt C++ Project 中包含的库

c - 未定义为异步安全的函数(例如 mmap(2))是否会影响在信号处理程序中调用的其他异步安全函数?

c - 为什么 Kernighan 和 Ritchie 不将 int 包含在 main 函数中?

Java arrayList 与 C++ vector

c++ - std::string(itr, itr) 应该做什么?

c - 文本转十进制 C

c++ - 在 switch 语句中使用 continue