簡體   English   中英

帶空格的 C++ 輸入字符串

[英]C++ Input String with Spaces

我正在編寫一個接受用戶輸入的程序。 我需要輸入在單詞之間包含空格。 我很難找到解決方案來做到這一點。

在你問之前,我已經在 StackOverflow 上嘗試了多個其他問題,同樣的問題。 這些是我嘗試過的一些:

如何在 C++ 中輸入空間?

std::cin 輸入帶空格?

用 C++ 演示 noskipws

noskipws對cin的影響>>

我的代碼的問題是,只要我的setBusinessName()方法被調用,它就會自行完成。 它輸出然后返回自身,而無需等待我輸入我的數據。

string setBusinessName()
{
    string name = "";
    cout << "The name you desire for your business:";
    getline(cin, name, '\n');
    cout << name;
    return name;
}

我還不能評論,沒有足夠的積分,但你有沒有嘗試添加cin.ignore(); getline(cin, name, '\n'); ?

像這樣:

string setBusinessName()
{
    string name = "";
    cout << "The name you desire for your business:";
    cin.ignore();
    getline(cin, name, '\n');
    cout << name;
    return name;
}

當您這樣做時,只需在評論中添加更多解釋:

cout << "Enter value:";
cin >> x;

cin 指令在用戶按下Enter時執行,因此輸入緩沖區具有用戶插入的值和一個額外'\n'字符。 如果你繼續做cin問題,但是如果你想使用getline (比如在你的情況下在字符串中包含空格),你必須知道getline將在緩沖區中第一次出現'\n'時停止,所以getline的結果將為空。

為避免這種情況,並且如果您確實必須同時使用 cin 和 getline,則需要使用streamsize (streamsize n = 1, int delim = EOF)從緩沖區中刪除該'\n' ,此函數會從緩沖區直到匹配delim (包括)的第一個字符,這是一個示例:

cin << x;
cin.ignore(256, '\n');
getline(cin, name, '\n');

請注意,建議使用:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

如果您不想猜測緩沖區中有多少個字符。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name1, name2, name3, name4, name5;
    int a,b; //or float ...

    cout << "Input name 1: ";
    getline(cin, name1); //input: abc def
    cout << "=> Name 1: "<< name1 << endl;      //output: abc def
    cout << "Input name 2: ";
    getline(cin, name2); //input: abc def
    cout << "=> Name 2: "<< name2 << endl;      //output: abc def

    cout<<"a: ";
    cin>>a;
    cout<<"a: "<<a<<endl;
    cout << "Input name 3: ";
    getline(cin, name3); //can not input
    cout << "=> Name 3: "<< name3 << endl; //output:

    cout<<"b: ";
    cin>>b;
    cout<<"b: "<<b<<endl;
    cout << "Input name 4: ";
    cin.ignore();
    getline(cin, name4); //input: abc def
    cout << "=> Name 4: "<< name4 << endl; //output: abc def

    
    cout << "Input name 5: ";
    cin.ignore();
    getline(cin, name5); //input: abc def
    cout << "=> Name 5: "<< name5 << endl; //output: bc def  !!!!!!!!!!
    
    //=> cin>>number; cin.ignore(); getline(cin, str); => OK
    //else: !!!!!!!!
    return 0;
}

流中可能已經存在某些內容,而getline()只是讀取它。

確保在此函數之前沒有使用cin>> 你可以在getline() cin.ignore() ) 來避免流中已經存在的東西。

它工作正常。 我剛試過這個。

#include <iostream>
#include <string>
using namespace std;

string setBusinessName(){
    string name = "";
    cout << "The name you desire for your business:";
    getline(cin, name);
    cout << name;
    return name;
}

int main() {
    setBusinessName();
    system("PAUSE");
}
#include<bits/stdc++.h>
using namespace std;

string setBusinessName(){
 string name;
 cout << "The name you desire for your business: ";
 getline(cin, name);
 cout << name;
 return name;
 }

int main() {
  setBusinessName();
  return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM