使用字符串类输入空格时出现 cin 问题

2021-12-21 00:00:00 string input c++

我有以下代码:

#include <iostream>
#include <string>

using namespace std;

string name;
string age;

int main() {
    cout <<"Name: ";
    cin >> name;
    cout << endl;
    cout <<"Age: ";
    cin >> age;
    cout << endl;
    cout << "Your name is " << name << ", and you are " << age << " years old."  << endl;
    cout << "Press enter to close this application" << endl;
    getchar();
    return 0;
}

我注意到,如果我在名称的输入中输入一个空格,它不会给我输入名称的机会,它会将空格后面的条目视为年龄.如果这是一个新手错误,我深表歉意,它可能是.我之前编写了 Java 并决定改用 C++,因为它更适合我的需求.我的代码格式也可能与您的标准很奇怪,如果您愿意,请更正.

I noticed that if I put a space in my input for name that it won't give me a chance to input name, and it will view the entry after the space as age. I apologize if this is a newbie mistake, which it probably is. I previously programmed Java and decided I wanted to switch to C++ because it better suits my needs. I also probably format my code weird to your standards, please correct it if you wish to.

我还注意到另一个错误,我在 Java 中从未真正遇到过任何问题.我不知道如何防止它在完成处理后立即关闭.我听说你可以使用system.(pause");但我也被告知不要使用它.我真的很困惑要使用什么.我听说过使用 getchar();,但它似乎没有任何作用.

I've also noticed another error, something I never really had any problems with in Java. I can't figure out how to prevent it from instantly closing down when it finishes processing. I've heard you can use "system.("pause"); but I've also been told to not use it. I'm really confused on what to use. I've heard to use getchar();, but it doesn't seem to do anything.

任何帮助将不胜感激,因为在 C++ 方面我是一个完整的初学者.

Any help would be greatly appreciated, as I'm a complete beginner when it comes to C++.

推荐答案

以下是运行程序时输入缓冲区发生的情况:

Here's what's happening with the input buffer when you run your program:

std::cin >> name;

您正在等待输入.当您输入Ryan Cleary"并按回车键时,输入缓冲区包含:

You're waiting for input. When you enter "Ryan Cleary", and press enter, the input buffer contains:

Ryan Cleary

现在你的 cin 像往常一样读取输入,在空格处停止,像这样离开你的缓冲区:

Now your cin reads input as normal, stopping at whitespace, leaving your buffer like this:

 Cleary

注意开头的空格,因为它在阅读 Ryan 后停止.您的第一个变量现在包含 Ryan.但是,如果您想要全名,请使用 std::getline.它将一直读到换行符,而不仅仅是空格.无论如何,继续:

Note the beginning space, as it stops after reading Ryan. Your first variable now contains Ryan. If, however, you want the full name, use std::getline. It will read until a newline, not just whitespace. Anyway, continuing on:

std::cin >> age;

现在你得到另一个输入.不过,那里已经有东西了.它跳过空白直到它可以开始读取,只留下缓冲区:

Now you're getting another input. There's already something there, though. It skips the whitespace until it can start reading, leaving the buffer with just:



您的第二个变量获取文本 Cleary.注意换行符仍在缓冲区中,这让我进入了第二部分.以始终有效的方式替换 system ("pause"); 很棘手.你最好的选择通常是接受一个不太完美的解决方案,或者像我喜欢的那样,一个不能保证完全按照它所说的去做:

Your second variable gets the text Cleary. Note the newline still in the buffer, which brings me to the second part. Replacing system ("pause"); in a way that always works is tricky. Your best bet is usually to live with a less-than-perfect solution, or as I like to do, one that isn't guaranteed to do exactly what it says:

std::cin.get(); //this consumes the left over newline and exits without waiting

好的,那么 cin.get() 没有用.这个怎么样:

Okay, so cin.get() didn't work. How about this:

std::cin.get(); //consume left over newline
std::cin.get(); //wait

这很好用,但是如果你将它复制粘贴到没有换行符的地方怎么办?你必须按两次回车!

That works perfectly, but what if you copy-paste it somewhere where the newline isn't left over? You'll have to hit enter twice!

解决方案是清除换行符(和其他任何东西),然后等待.这就是 cin.sync() 的目的一>.但是,如注释部分所示,不能保证像它所说的那样清除缓冲区,因此如果您的编译器选择不清除,则无法使用它.然而,对我来说,它正是这样做的,留下了一个解决方案:

The solution is to clear the newline (and anything else) out, and then wait. This is the purpose of cin.sync(). However, as seen in the notes section, it is not guaranteed to clear the buffer out like it says, so if your compiler chooses not to, it can't be used. For me, however, it does exactly that, leaving a solution of:

std::cin.sync(); //clear buffer
std::cin.get(); //wait

system("pause"); 的主要坏处是你不知道它会在别人的电脑上运行什么程序.他们可以更改 pause.exe 或将找到的放在第一位,而您无从得知.这可能会破坏他们的计算机,因为它可能是任何程序.

The main bad thing about system("pause"); is that you have no idea what program it will run on someone else's computer. They could've changed pause.exe or put one that's found first, and you have no way of knowing. This could potentially ruin their computer due to it being possibly any program.

相关文章