C++程式獲取使用者輸入


在任何語言中編寫程式時,獲取輸入是我們在幾乎所有程式中都會執行的基本操作。有時我們直接從控制檯獲取輸入,或者從檔案中獲取輸入。從檔案獲取輸入在某種程度上是有益的,因為它不需要我們一遍又一遍地輸入,或者有時我們可以將一些好的輸入測試用例儲存到檔案中。但是,在本文中,我們將重點關注基於控制檯的輸入。我們將學習在 C++ 中獲取使用者輸入的不同技術。

從控制檯獲取輸入有幾種不同的方法。其中一些是 C 語言風格的方法,還有一些是使用 C++ 中提供的輸入流。我們將逐一介紹它們,並提供一些示例以更好地理解。

使用 scanf() 函式獲取輸入

在 C 語言中,我們使用 scanf() 函式使用格式化字串從控制檯掃描輸入。此函式在 C++ 中也可用,因此要以格式化的方式獲取輸入,scanf() 方法是很好的選擇。

語法

scanf() 方法的基本語法以及格式化字串。

scanf ( “<format string>”, <address of variable> );

scanf() 格式化的格式說明符。

格式說明符 描述
%c 用於單個字元輸入
%s 用於沒有空格的字串
%hi 短整型(帶符號)
%hu 短整型(無符號)
%Lf 長雙精度浮點數
%d 十進位制整數(帶符號),假設基數為 10
%i 整數(自動檢測基數)
%o 八進位制整數
%x 十六進位制整數
%p 指標
%f 浮點數

示例 1

#include <iostream> using namespace std; void takeInput() { int x; char s[50]; // C like string or character array char c; float f; cout << "Enter an integer: "; scanf( "%d", &x ); cout << "\nYou have entered an integer: " << x << endl; cout << "Enter a character: "; scanf( " %c", &c ); cout << "\nYou have entered a character: " << c << endl; cout << "Enter a float value: "; scanf( "%f", &f ); cout << "\nYou have entered float value: " << f << endl; cout << "Enter a string: "; scanf( "%s", s ); //string do not need address //convert to C++ like string from C like string string SCpp; SCpp.assign(s); cout << "\nYou have entered the string: " << SCpp << endl; } int main(){ takeInput(); }

輸出

Enter an integer: 5
You have entered an integer: 5
Enter a character: K
You have entered a character: K
Enter a float value: 2.56
You have entered float value: 2.56
Enter a string: HelloWorld
You have entered the string: HelloWorld

在這種方法中,它適用於其他資料型別,但對於字串,它只接受 C 風格的字串或字元陣列。要使用“cout”顯示字串,我們需要將其轉換為 C++ 風格的字串物件。否則,我們可以使用 printf() 函式顯示輸出。這些是基本示例。現在讓我們在下一個示例中看看格式化字串的效果。

示例 2

#include <iostream> using namespace std; void takeInput() { int dd, mm, yyyy; cout << "Enter a date in dd-mm-yyyy format: "; scanf( "%d-%d-%d", &dd, &mm, &yyyy ); cout << "\nThe given date is: "; printf( "%d/%d/%d", dd, mm, yyyy ); } int main(){ takeInput(); }

輸出

Enter a date in dd-mm-yyyy format: 14-10-2022
The given date is: 14/10/2022

在這個例子中,我們以 (dd-mm-yyyy) 的格式獲取輸入,這不會以任何其他格式接受這三個整數值。在我們的輸出中,我們以另一種格式 (dd/mm/yyyy) 顯示相同的日期。這就是格式化字串輸入的實際用途。接下來,我們將看到在 C++ 中使用“cin”輸入流直接獲取任何型別資料到指定變數的更簡單的形式。

在 C++ 中使用 cin 獲取輸入

cin 是一個 C++ 輸入流類,它使用提取運算子 >> 從流中獲取輸入。此運算子透過從控制檯獲取輸入,自動將值插入到指定的變數中。語法如下所示。

語法

cin 方法的基本語法

cin >> <input variable name>

示例 1

#include <iostream> using namespace std; void takeInput() { int x; string s; char c; float f; cout << "Enter an integer: "; cin >> x; cout << "\nYou have entered an integer: " << x << endl; cout << "Enter a character: "; cin >> c; cout << "\nYou have entered a character: " << c << endl; cout << "Enter a float value: "; cin >> f; cout << "\nYou have entered float value: " << f << endl; cout << "Enter a string: "; cin >> s; cout << "\nYou have entered the string: " << s << endl; } int main(){ takeInput(); }

輸出

Enter an integer: 8
You have entered an integer: 8
Enter a character: L
You have entered a character: L
Enter a float value: 3.14159
You have entered float value: 3.14159
Enter a string: WeAreLearningC++InputTaking
You have entered the string: WeAreLearningC++InputTaking

與其他變數一樣,我們也可以直接獲取字串,而無需將其作為字元陣列。在這種方法中,它會自動將給定的輸入分配給字串物件。但是,字串存在一個問題。我們不能以這種方式輸入多單詞字串。如果我們寫一個多單詞字串,它只會獲取第一個單詞。讓我們在下面的例子中看看這一點。

示例 2

#include <iostream> using namespace std; void takeInput() { string s; cout << "Enter a string: "; cin >> s; cout << "\nYou have entered the string: " << s << endl; } int main(){ takeInput(); }

輸出

Enter a string: Hello World, This is a nice day
You have entered the string: Hello

為了克服這個問題,我們需要使用 getline() 函式獲取帶空格分隔的單詞的字串。在這種方法中,當遇到換行符時,它將結束讀取文字。

語法

getline(std::cin, <string variable>)

示例 3

#include <iostream> using namespace std; void takeInput() { string s; cout << "Enter a string: "; getline(cin, s); cout << "\nYou have entered the string: " << s << endl; } int main(){ takeInput(); }

輸出

Enter a string: Hello World, Have a nice day
You have entered the string: Hello World, Have a nice day

結論

在本文中,我們瞭解了使用 scanf() 方法以及 cin 流讀取器從使用者獲取輸入的不同用法。獲取輸入到任何其他變數型別都很簡單。但是,%s 格式說明符和 cin 類都不能獲取帶空格的輸入字串。與 C 語言類似,C++ 中也存在一個指定函式來讀取帶空格分隔單詞的字串。getline() 方法可以用來獲取這樣的輸入字串。我們還可以從檔案以及字串流中獲取輸入。

更新於: 2023年4月4日

1K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告