Why does this C ++ program double up the double values ​​and not print the entire string?

advertisements

I was working on a HackerRank problem and I could not figure out why the C++ code round the double values when I am adding them and why it does not take in/print the entire string input it is given.

The code is supposed to take in an integer input (from one line), a double input (from another line), and a string input (also, from another line). Then it is supposed to print out the sum of the int input and 4, the sum of the double input and 4.0, and concatenate the string "HackerRank" to the beginning of the input string.

Here's the code I have:

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

For the following input:

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

I got the output:

16
8
HackerRank is

When this is the expected output:

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


The answer to your 2 questions:

Firstly, when you add a value of type int to a value of type float/string, the result will be of type int. This explains why you the output is 8 and not 8.0. This same rule applies to multiplication, division, and subtraction. Whenever an int is operated on by a float/double value or vice versa, the result is always of type int. Therefore, you should change the initialization of your d value to:

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

By the way, you cannot add a decimal point to a value of type int and expect it to be a floating point/double value. The data type of the variable that stores the value must be defined/initialized with a certain data type.

Secondly, your code does not print the desired string as you are using the wrong function to get the string as input from the user. While cin is the norm to be used in input, it does not work so well with variables of type "string". The reason for this is because cin only accepts one word; one continuous int, floating point value, char, etc..., and it cannot accept an entire sentence with spaces in between because it just stops reading after it sees a space; that's the rules of cin. To bypass this problem, you'll need another function, and that is

    getline(cin, variable_to_store_data);

instead of doing:

    cin >> c;

Do this:

    getline(cin, c);

This way, the entire sentence you inputted will be stored in the variable and not just the first word of the sentence. The getline does not ignore the words in a sentence that come after the first one; it reads all of the user input till the point he/she hits Enter, and it then stores the entire value in the variable (that's the second parameter).

By the way, if you want to output multiple things in one cout line, then do it using the following template:

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

Avoid using the method you used above:

    cout << "" + s + c;

Do it this way:

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

On a side note, getline() also has a lot of other functions, such as reading lines from a file. Read more about it online; there are lots of resources available.

Hope this answers your question.


EDIT: To make the program work, you'll actually have to add another line to ignore the enter hit at the end of the cin >> b; command, because that saves as the string in c. Therefore, do this:

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

The line I just added ignores the newline character hit at the end of the cin >> b command. This way, the compiler goes on to ask the user for the string to be stored in c. I've tried this code, and it works as it should.

Another thing, change your output statement to;

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

This makes the string easier to read, and it adds a space between variable s and variable c during output. Hope this helps!