简体   繁体   English

cin.get()不起作用

[英]cin.get() not working

I wrote this simple program today, but I found that cin.get() refuses to work unless there are 2 of them. 我今天写了这个简单的程序,但是我发现cin.get()除非有两个,否则拒绝工作。 Any ideas? 有任何想法吗?

#include <iostream>
using namespace std;

int main(){
    int base;
    while ((base < 2) || (base > 36)){
          cout << "Base (2-36):" << endl; 
          cin >> base;
          }
    string base_str = "0123456789abcdefghijklmnopqrstuvwxyz";
    for (int i = 0; i < base; i++){
        for (int j = 0; j < base; j++){
            for (int k = 0; k < base; k++){    
                cout << base_str[i] << base_str[j] << base_str[k] << endl;
            }
        }
    }
    cin.get();
    cin.get();
}

if i move a cin.get() to before the nested loops, the loops run then pause. 如果我将cin.get()移到嵌套循环之前,则循环将运行然后暂停。 if i take one cin.get() out, the program just ends. 如果我取出一个cin.get() ,该程序就结束了。 im using the latest version of bloodshed c++ dev 我正在使用最新版本的流血C ++开发人员

You are not initializing the 'base' variable, but while that will cause bugs it isn't (directly) related to the behavior you're seeing with cin, even though it will sometimes, depending on the compiler, cause you to skip loops. 您没有初始化'base'变量,但这虽然会导致错误,但它(与)您所看到的cin行为无关(直接),尽管有时(取决于编译器)会导致您跳过循环。 You're probably building in debug mode that zero-initializes or something. 您可能正在以零初始化之类的调试模式进行构建。

That said, assuming that was fixed: 就是说,假设这是固定的:

When you type a value (say, 5) and hit enter, the data in the stream is 5<newline> -- operator<< does not extract the newline from the stream, but cin.get() does. 当您键入一个值(例如5)并按Enter键时,流中的数据为5<newline> -运算符<<不会从流中提取换行符,而cin.get()会提取。 Your first cin.get() extracts that newline from the stream, and the second wait waits for input because the stream is now empty. 您的第一个cin.get()从流中提取该换行符,而第二个等待则等待输入,因为该流现在为空。 If you had only the one cin.get() call, it would extract the newline immediately and continue, and since there is nothing after that cin.get() call, the program terminates (as it should). 如果只有一个cin.get()调用,它将立即提取换行符并继续,并且由于在cin.get()调用之后没有任何内容,因此程序将终止(应如此)。

It seems that you're using cin.get() to stop your program from closing when run from the debugger; 从调试器运行时,似乎正在使用cin.get()来阻止程序关闭。 you can usually do this via a specific "start without debugging" command from your IDE; 您通常可以通过IDE中的特定“无需调试即可启动”命令来执行此操作; then you won't need to abuse cin.get() for this purpose. 那么您就不需要为此而滥用cin.get()了。

Variable base has not been initialized. 变量base尚未初始化。

You can fix it by giving an invalid value to base as: 您可以通过为base提供无效值来解决此问题:

int base = 1; // 1 is not in the valid range.
while ((base < 2) || (base > 36)){

or 要么

better use a do-while loop as: 最好将do-while循环用作:

int base;
do{
     cout << "Base (2-36):" << endl;
     cin >> base;
} while ((base < 2) || (base > 36));

The reason why you need the 2nd cin.get() is that, after you read the base value using cin , a \\n is left in the buffer. 需要第二个cin.get()是,在使用cin读取base值之后,缓冲区中留下了\\n The first call to cin.get() consumes that \\n and the 2nd cin.get waits for your input. 第一次调用cin.get()会消耗\\n ,第二个cin.get等待您的输入。 To avoid this you need to flush the \\n from the buffer after cin by calling cin.ignore 为避免这种情况,您需要在cin之后通过调用cin.ignore从缓冲区中清除\\n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM