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) 显示相同的日期。这是格式化字符串输入的实际用途。接下来,我们将看到一种更简单的形式,使用"cin"输入流在 C++ 中获取输入,将任何类型的数据直接传送到指定的变量。
在 C++ 中使用 cin 获取输入
cin 是一个 C++ 输入流类,它使用提取运算符 >> 从流中获取输入。此运算符通过从控制台获取输入,自动将值插入到指定的变量。语法如下。
语法
cin 方法的基本语法
cin >> <input variable name>
Example 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>)
Example 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() 方法可用于获取此类输入字符串。我们还可以从文件和字符串流中获取输入。