問題描述
我有以下代碼:
#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.
這篇關于使用字符串類輸入空格時出現 cin 問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!