kazi siam
kazi siam

Reputation: 109

HackerRank: Day 1: Data Types in C++

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

Upvotes: 4

Views: 2956

Answers (6)

Dushyant Singh
Dushyant Singh

Reputation: 77

Correct Answer
Github: https://github.com/Dushyantsingh-ds/30-Days-of-Code-hackerrank/blob/main/Content/Day%201:%20Data%20Types.md

#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 i2;
    double d2;
    string s2;

    // Read and save an integer, double, and String to your variables.
    string tmp;
    // Declare second integer, double, and String variables. 
    getline(cin, tmp);
    i2 = stoi(tmp);

    getline(cin, tmp);
    d2 = stod(tmp);

    getline(cin, s2);

    // Print the sum of both integer variables on a new line.
    printf("%i\n", i + i2);

    // Print the sum of the double variables on a new line.
    printf("%.1f\n", d + d2);

    // Concatenate and print the String variables on a new line
    // The 's' variable above should be printed first.
    cout << s + s2 << endl;
    return 0;

Upvotes: 0

Prajwal Benedict A
Prajwal Benedict A

Reputation: 1

use this

int num;
float db;
string str;
cin>>num;
cin>>db;
cin.get();
getline(cin,str);

cout<<i+num<<endl;
printf("%.1f\n", d + db);
cout<<s+str<<endl

Upvotes: 0

AEM
AEM

Reputation: 1386

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.

Upvotes: 0

Rafiqul Islam
Rafiqul Islam

Reputation: 1646

#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

Upvotes: 0

user13434413
user13434413

Reputation: 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
}

Upvotes: -1

shubhgkr
shubhgkr

Reputation: 471

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);

Upvotes: 6

Related Questions