在任何编程语言中编写程序时,接收输入是我们几乎在所有程序中要做的基本工作。有时我们直接从控制台获取输入,有时我们从文件中获取输入。从文件中获取输入有一定的好处,因为它不需要我们一遍又一遍地输入,有时我们可以将一些好的输入测试用例保存到文件中。然而,在本文中,我们将重点关注基于控制台的输入。我们将学习在C++中从用户获取输入的不同技术。
有几种不同的方法可以从控制台获取输入。其中一些是类似C的方法,而另一些是使用C++中存在的输入流。我们将逐一介绍它们,并提供一些示例以便更好地理解。
使用scanf()函数接收输入
在C语言中,我们使用scanf()函数以格式化字符串的方式从控制台扫描输入。这个函数在C++中也是可用的,所以要以格式化的方式接收输入,可以使用scanf()方法。
语法
scanf()方法的基本语法,包括格式字符串。
'scanf ( “<format string>”, <address of variable> );
scanf()格式化的格式说明符。
格式说明符 | Description | 的中文翻译为:描述 |
---|---|---|
%c | 对于单个字符的输入 | |
%s | 对于没有空格的字符串 | |
%嗨 | 短有符号整数 | |
%hu | 短无符号整数 | |
%Lf | 长双 | |
%d | 十进制整数(有符号),假定基数为10 | |
%i | 整数(自动检测基数) | |
%o | 八进制整数 | |
%x | 十六进制整数 | |
%p | 指针 | |
%f | 浮点数 |
Example 1
的中文翻译为:示例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()函数来显示输出。这些都是基本的例子。现在让我们看看在下一个例子中格式化字符串的效果。
Example 2
的翻译为:示例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”输入流直接将任何类型的数据输入到指定的变量中。
使用cin在C++中接收输入
cin是一个C++输入流类,它使用提取运算符>>从流中获取输入。该运算符通过从控制台获取输入自动将值插入到指定的变量中。语法如下所示。
语法
cin方法的基本语法
'cin >> <input variable name>
Example 1
的中文翻译为:示例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
像其他变量一样,我们可以直接使用字符串,而不必将其作为字符数组。在这种方法中,它会自动将给定的输入分配给一个字符串对象。然而,关于字符串存在一个问题。我们不能以这种方式输入多词字符串。如果我们写一个多词字符串,它只会取第一个单词。让我们在下面的示例中看到这一点。
Example 2
的翻译为:示例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
的中文翻译为:示例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()方法来接受这种输入字符串。我们还可以从文件和字符串流中获取输入。