cin.get() 不工作

发布于 2024-09-24 08:12:44 字数 760 浏览 3 评论 0原文

我今天写了这个简单的程序,但我发现 cin.get() 拒绝工作,除非有两个。有什么想法吗?

#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();
}

如果我将 cin.get() 移动到嵌套循环之前,循环将运行然后暂停。如果我取出一个 cin.get() ,程序就会结束。我正在使用最新版本的bloodshed c++ dev

I wrote this simple program today, but I found that cin.get() refuses to work unless there are 2 of them. 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. if i take one cin.get() out, the program just ends. im using the latest version of bloodshed c++ dev

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

策马西风 2024-10-01 08:12:44

您没有初始化“base”变量,但是虽然这会导致错误,但它与您在 cin 中看到的行为没有(直接)相关,尽管有时根据编译器的不同,它会导致您跳过循环。您可能正在以零初始化或其他方式的调试模式进行构建。

也就是说,假设这是固定的:

当您键入一个值(例如 5)并按 Enter 键时,流中的数据为 5 --operator<<不会从流中提取换行符,但 cin.get() 会提取换行符。您的第一个 cin.get() 从流中提取换行符,第二个 wait 等待输入,因为流现在为空。如果只有一个 cin.get() 调用,它将立即提取换行符并继续,并且由于 cin.get() 调用之后没有任何内容,程序将终止(正如它应该的那样)。

看来您正在使用 cin.get() 来阻止程序在从调试器运行时关闭;您通常可以通过 IDE 中的特定“启动而不调试”命令来执行此操作;那么您就不需要为此目的滥用 cin.get() 。

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. 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. Your first cin.get() extracts that newline from the stream, and the second wait waits for input because the stream is now empty. 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).

It seems that you're using cin.get() to stop your program from closing when run from the debugger; you can usually do this via a specific "start without debugging" command from your IDE; then you won't need to abuse cin.get() for this purpose.

秋日私语 2024-10-01 08:12:44

变量base尚未初始化。

您可以通过为 base 提供无效值来修复它:

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

或者

更好地使用 do-while 循环作为:

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

您需要第二个 cin.get() 的原因是使用 cin 读取 base 值后,缓冲区中会留下 \n 。第一次调用 cin.get() 会消耗该 \n,第二次调用 cin.get 会等待您的输入。为了避免这种情况,您需要在 cin 之后通过调用 cin.ignore 从缓冲区中刷新 \n

Variable base has not been initialized.

You can fix it by giving an invalid value to base as:

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

or

better use a do-while loop as:

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. The first call to cin.get() consumes that \n and the 2nd cin.get waits for your input. To avoid this you need to flush the \n from the buffer after cin by calling cin.ignore

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文