为什么这个C++程序舍入双值而不打印整个字符串



我正在处理一个HackerBank问题,我不明白为什么C++代码在添加双值时会舍入双值,以及为什么它不接受/打印给定的整个字符串输入。

该代码应该接受一个整数输入(来自一行)、一个双输入(来自另一行)和一个字符串输入(也来自另一行将)。然后,它应该打印出int输入和4的总和,双输入和4.0的总和,并将字符串"HackerBank"连接到输入字符串的开头。

这是我的代码:

#include <iostream>
#include <iomanip>
#include <limits>
int main(){
int i = 4;
double d = 4.0;
string s = "HackerRank";
// Declare second integer, double, and String variables.
// Read and save an integer, double, and String to your variables.
// Note: If you have trouble reading the entire string, please go back and review the Tutorial closely.
// Print the sum of both integer variables on a new line.
// Print the sum of the double variables on a new line.
// Concatenate and print the String variables on a new line
// The 's' variable above should be printed first.
int a;
double b;
string c;
cin >> a; 
cin >> b;
cin >> c;

a = a + i;
b = b + d;
cout << a << endl;
cout << b << endl;
cout << "" + s + c;
return 0;
}

对于以下输入:

12
4.0
is the best place to learn and practice coding!

我得到了输出:

16
8
HackerRank is

当这是预期输出时:

16
8.0
HackerRank is the best place to learn and practice coding!

两个问题的答案:

首先,当您将类型为int的值添加到类型为float/string的值时,结果将为int类型。这解释了为什么输出为8而不是8.0。这个规则同样适用于乘法、除法和减法。每当一个int被浮点/双精度值操作时,或者反之亦然,结果总是int类型。因此,您应该将d值的初始化更改为:

double d = 4.0; // Or float d = 4.0

顺便说一句,您不能将小数点添加到int类型的值中,并期望它是浮点/双精度值。存储值的变量的数据类型必须使用特定的数据类型进行定义/初始化。

其次,您的代码没有打印所需的字符串,因为您使用了错误的函数从用户那里获得字符串作为输入。虽然cin是要在输入中使用的规范,但它与"字符串"类型的变量不能很好地配合使用。原因是cin只接受一个单词;一个连续的int、浮点值、char等,并且它不能接受中间有空格的整个句子,因为它只是在看到空格后停止阅读;这就是cin的规则。要绕过这个问题,你需要另一个功能,那就是

getline(cin, variable_to_store_data);

而不是做:

cin >> c;

这样做:

getline(cin, c);

这样,您输入的整个句子将存储在变量中,而不仅仅是句子的第一个单词。getline不会忽略句子中第一个单词之后的单词;它读取所有用户输入,直到他/她点击Enter为止,然后将整个值存储在变量中(这是第二个参数)。

顺便说一句,如果你想在一行中输出多个东西,那么可以使用以下模板:

cin << a << ... << z << endl; // endl is optional; depends on your needs

避免使用上面使用的方法:

cout << "" + s + c;

这样做:

cout << "" << s << c; // Why do you have "" at the begninning? That prints nothing. You can take that off also.

顺便说一句,getline()还有很多其他函数,比如从文件中读取行。在线阅读更多关于它的信息;有很多可用的资源。

希望这能回答你的问题。


EDIT:为了使程序正常工作,您实际上必须添加另一行来忽略cin >> b;命令末尾的回车键,因为这会保存为c中的字符串。因此,这样做:

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

我刚才添加的行忽略了cin >> b命令末尾的换行符。通过这种方式,编译器继续请求用户将字符串存储在c中。我已经尝试过这段代码,它可以正常工作。

另一件事是,将输出语句更改为;

cout << "" << s << " " << c << "." << endl;

这使得字符串更容易读取,并且在输出过程中在变量s和变量c之间添加了一个空格。希望这能有所帮助!

此代码将工作。。。

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int i = 4;
double d = 4.0;
string s = "HackerRank ";
int i2;
double d2;
string s2;
cin>>i2;
cin>>d2;
cin.ignore();
getline(cin, s2);
cout<<i2+i<<endl;
cout.precision(1);
cout << fixed << d+d2 << endl;
cout<<s+s2;
return 0;
}

您可以使用函数"getline"来实现它。

下面的例子在VS2013上做得很好。

#include <string>
#include <iostream>
using namespace std;
int main(){
string c;
getline(cin, c); // is there anybody ?
cout << "Hello, " + c; // Hello, is there anybody ?
system("pause");
return 1;
}
cin>>b;

使用cin.ignore();然后获取行(cin,string_name);

这将读取完整的字符串。

最新更新