Posted on

C++

You may find this information helpful when completing this challenge in C++.

To consume the whitespace or newline between the end of a token and the beginning of the next line:

// eat whitespace
getline(cin >> ws, s2);

where s2 is a string. In addition, you can specify the scale of floating-point output with the following code:

#include <iostream>
#include <iomanip>

using namespace std;
int main(int argc, char *argv[]) {
    double pi = 3.14159;
	
    // Let's say we wanted to scale this to 2 decimal places:
    cout << fixed << setprecision(2) << pi << endl;
    printf("%.2f", pi);
}

which produces this output:

3.14
3.14

Still unclear? Take some time to review the documentation on these topics:

Submission

#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.
    
    // 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; cin >> a;
    double b; cin >> b;
    string c; getline(cin >> ws, c);

    cout << a + i << endl 
        << fixed << setprecision(1) << double(d + b) << endl 
        << string(s + c) << endl;
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *