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

时间:2017-01-25 00:56:50

标签: c++ io

我正在研究一个HackerRank问题,当我添加它们时,我无法弄清楚为什么C ++代码会对double值进行舍入,以及为什么它不会接收/打印整个字符串输入。

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

这是我的代码:

#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!

4 个答案:

答案 0 :(得分:2)

你的两个问题的答案:

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

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

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

其次,您的代码不会打印所需的字符串,因为您使用了错误的函数来获取用户输入的字符串。虽然cin是在输入中使用的标准,但对于“string”类型的变量,它不能很好地工作。这是因为cin只接受一个单词;一个连续的int,浮点值,char等等,并且它不能接受整个句子之间有空格,因为它只是在看到空格后停止阅读;这是cin的规则。要绕过这个问题,你需要另一个功能,那就是

    getline(cin, variable_to_store_data);

而不是:

    cin >> c;

这样做:

    getline(cin, c);

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

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

    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()还有许多其他功能,例如从文件中读取行。在线阅读更多相关信息;有很多可用的资源。

希望这能回答你的问题。

编辑:要使程序正常工作,您实际上必须添加另一行来忽略cin >> b;命令末尾的输入命中,因为它在c中保存为字符串。因此,这样做:

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

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

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

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

这使得字符串更易于阅读,并且在输出期间在变量 s 和变量 c 之间添加了空格。希望这有帮助!

答案 1 :(得分:2)

此代码将起作用...

#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;
}

答案 2 :(得分:1)

您可以使用&#39; getline&#39;实现它。

以下示例在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;
}

答案 3 :(得分:0)

cin>>b;

使用cin.ignore(); 然后是getline(cin,string_name);

这将读取完整的字符串。