4

They are asking me to declare 3 variables, one for integer, one for double and one for string. Then read 3 lines of input from stdin. I have posted up my solution but it is not working. I don't know why my variable for string is not reading from the stdin. When I try it in the VSCODE, it is working. Can you tell me what am I doing wrong?

Here is the problem

Sample input:

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

Sample output:

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

This is the code I use to check in my VSCODE. This works! But it doesn't work in HackerRank website.

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

Picture of Test Case failed

kiner_shah
  • 2,055
  • 3
  • 22
  • 29
kazi siam
  • 109
  • 7
  • Works [here](https://ideone.com/HrSQSm). – kiner_shah Dec 28 '19 at 05:57
  • What do you mean by "it doesn't work in HackerRank website"? – Fred Larson Dec 28 '19 at 05:59
  • 2
    You forgot to read in the numbers. – NathanOliver Dec 28 '19 at 05:59
  • 1
    It fails the test case. It only output HackerRank. It doesn't output anything else afterward. @kiner_shah Maybe there is a bug in their stdin for C++. – kazi siam Dec 28 '19 at 06:06
  • @NathanOliver-ReinstateMonica I know. But I am only having problem with reading input from stdin for string. it is failing the test case because of the string input in the HackerRank website. – kazi siam Dec 28 '19 at 06:08
  • Hackerrank is all about *corner-cases*. In the instructions it states if you are having trouble reading the entire string, go back and closely read the string tutorial provided. Do you know what the first testcase string is that is failing? (it may cost you 5-hackos to find out, but it is probably worth it) Also, since there are numbers read before the string, are you sure you are handing the `'\n'` that `cin >> int` or `cin >> double` would leave in `stdin` (e.g. `cin.ignore(...)`) before calling `getline`? – David C. Rankin Dec 28 '19 at 06:15
  • E.g. after you `cin >> y;` (you should validate that succeeds), you need `std::cin.ignore (std::numeric_limits::max(), '\n');` before your call to `getline(cin, str);` (which you should also validate -- as you do every input) – David C. Rankin Dec 28 '19 at 06:20
  • 1
    All I had to do is add ws like this "getline(cin >> ws, str)" to discard leading whitespace from an input stream. – kazi siam Dec 28 '19 at 06:28
  • Possible duplicate of [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – user202729 Jun 12 '20 at 03:56

4 Answers4

5

After reading input using std::cin, there will be an extra line left which is read by the getline() everytime.
Try using std::cin.ignore() before reading the string.
It will ignore the extra line.

std::cin>>x;
std::cin.ignore();
std::getline(std::cin,str);
shubhgkr
  • 361
  • 1
  • 6
0

I had the same problem before and i found that this problem because you can't use getline() and std::cin at the same time , so what's the solution for this problem ? It's easy more than you think , just create 3 strings and read them using Getline() and then convert it from string to int using stoi(string) , and stod(string) to convert from string to double.

Here's the solution:

#include <iostream>
#include <iomanip>

using namespace std;

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


    string ss, n, dd;
    getline(cin, n); // string ---> must convert it to integer 
    getline(cin, dd); // string ---> must convert it to double   
    getline(cin, ss); // string 
    cout << int(stoi (n) + i) << endl;
    cout << fixed << setprecision(1) << double(stod(dd) + d ) << endl;
    cout << s << ss << endl;

    return 0;

Hope that helps you with your problem.

AEM
  • 868
  • 11
  • 23
0
#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.
    std::cin >> x;
    std::cin >> y;
    std::cin.ignore();
    getline(std::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.
    std::cout << std::fixed;
    std::cout << std::setprecision(1);
    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;
}

It work test here

kiner_shah
  • 2,055
  • 3
  • 22
  • 29
Rafiqul Islam
  • 1,526
  • 1
  • 11
  • 25
-1
int main() {
    int i = 4;
    double d = 4.0;
    string s = "HackerRank ";

    int ii; double dd; string ss;
    cin>>ii>>dd;
    cin.ignore();
    getline(cin,ss);
    int isum=(i+ii);
    double dsum=dd+d;
    cout<<isum<<endl<<fixed<<setprecision(1)<<dsum<<endl<<s<<ss;
    return 0;
    //credits to Bytewise
}
  • Can you post some explanation of why things were broken before and why your's works? – Charlie OConor Apr 29 '20 at 16:34
  • Sir, the Hackerrank task has stated to use int, double, and string data types. I directly dealt with datatypes rather than converting strings into an int or double datatype. It decreases the complexity of the code. some people don't know what is "stoi, stod" etc. – user13434413 Apr 30 '20 at 19:24