HackerRank:第 1 天:C++ 中的数据类型

kaz*_*iam 4 c++

他们要我声明 3 个变量,一个是整数,一个是双精度,一个是字符串。然后从标准输入读取 3 行输入。我已经发布了我的解决方案,但它不起作用。我不知道为什么我的字符串变量没有从标准输入读取。当我在 VSCODE 中尝试时,它正在工作。你能告诉我我做错了什么吗?

这是问题所在

样本输入:

12
4.0    
is the best place to learn and practice coding!
Run Code Online (Sandbox Code Playgroud)

示例输出:

16
8.0
HackerRank is the best place to learn and practice coding!
Run Code Online (Sandbox Code Playgroud)

这是我用来检查我的 VSCODE 的代码。这有效!但它在 HackerRank 网站上不起作用。

#include <iostream>
#include <iomanip>
#include <limits>

using namespace std;

int main() {
    int i = 4;
    double d = 4.0;
    string s = "HackerRank ";


    // Declare second integer, double, and String variables.int x;
    int x;
    double y;
    string str;
    // Read and save an integer, double, and String to your variables.
    cin >> x;
    cin >> y;
    getline(cin, str);

    // 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.
    cout << x + i << endl;
    // Print the sum of the double variables on a new line.
    cout << y + d << endl;
    // Concatenate and print the String variables on a new line
    // The 's' variable above should be printed first.
    cout << s + str << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

测试用例失败图片

小智 5

使用 std::cin 读取输入后,每次都会有额外的一行被 getline() 读取。在读取字符串之前
尝试使用std::cin.ignore()
它将忽略额外的行。

std::cin>>x;
std::cin.ignore();
std::getline(std::cin,str);
Run Code Online (Sandbox Code Playgroud)