-5

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!
Nitesh Kartha
  • 31
  • 1
  • 6
  • See http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list for the answer to this question. – Sam Varshavchik Jan 25 '17 at 01:03
  • 1
    1) `8`, and `8.0` means exactly the same thing (no rounding involved). It's just a matter of representation. Use `std::fixed`, and `std::setprecision` I/O manipulators to achieve what you want. 2) You couldn't get the output of `HackerRank is` since, for specified input, `cout << "" + s + c;` would've produced `HackerRankis`. You never inserted a space in your string. And, if you want to read an entire line, use `std::getline`. – Algirdas Preidžius Jan 25 '17 at 01:05
  • try 4.1 then it won't round as you guess – Raindrop7 Jan 25 '17 at 01:13

4 Answers4

2

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!

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
2

This code will work...

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

You can use the function 'getline' to realize it.

And the following example is well done on 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;
}
breeze_whp
  • 33
  • 7
0
cin>>b;

use cin.ignore(); and then getline(cin, string_name);

This will read the complete string.