Jump to content

Statements that shouldn't be printed are printing. (C++14)

I don't really understand this issue all too well. I'm just doing a problem on HackerRank that's part of the 30 days of programming challenge and I've got to the point where my code looks alright but it's still producing many errors. 

For instance, whenever I input a number it will simply output "You are young." two times for every number I input. 

Here is the code: 

 

// ConsoleApplication5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std; 

class Person {
public:
    int age;
    Person(int initialAge);
    void amIOld();
    void yearPasses();
};

Person::Person(int initialAge) {
    // Add some more code to run some checks on initialAge
    if (initialAge > 0) {
        initialAge = age;
    }
    else {
        age = 0;
        cout << "Age is not valid, setting age to 0." << endl;
    }
}

void Person::amIOld() {
    // Do some computations in here and print out the correct statement to the console 
    if (age < 13) {
        cout << "You are young." << endl; 
    }
    else if (age < 18 && age >= 13) {
        cout << "You are a teenager.";
    }
    else {
        cout << "You are old.";
    }
}

void Person::yearPasses() {
    // Increment the age of the person in here
    age += 1;
}

int main() {
    int t;
    int age;
    cin >> t;
    for (int i = 0; i < t; i++) {
        cin >> age;
        Person p(age);
        p.amIOld();
        for (int j = 0; j < 3; j++) {
            p.yearPasses();
        }
        p.amIOld();

        cout << '\n';
    }

    return 0;
}

Link to comment
Share on other sites

Link to post
Share on other sites

You realize that this:

if (initialAge > 0) {
        initialAge = age;
    }
    else {
        age = 0;
        cout << "Age is not valid, setting age to 0." << endl;
    }

means the age will always be set to 0?

-edit

this is not meant to be a snarky comment, I asked to make sure you didn't already change that

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Sauron said:

You realize that this:


if (initialAge > 0) {
        initialAge = age;
    }
    else {
        age = 0;
        cout << "Age is not valid, setting age to 0." << endl;
    }

means the age will always be set to 0?

Clearly not, because he's asking the question.

In the constructor, you need to set the member "age" variable to the initialAge that is passed in. So change 
 

if (initialAge > 0) {
    initialAge = age;
}

To

if (initialAge > 0) {
    age = initialAge;
}

So that age stores the initialAge passed into the constructor.

Link to comment
Share on other sites

Link to post
Share on other sites

As well as those you need to put "<< endl;" in the amIOld() after each cout to pass the test cases.

3700x, Asus B450i, 16GB Corsair Vengeance RGB, Gigabyte GTX 970 ITXDan A4-SFX, SX600-G, 120mm AIO.

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, Pinguinsan said:

Clearly not, because he's asking the question.

In the constructor, you need to set the member "age" variable to the initialAge that is passed in. So change 
 


if (initialAge > 0) {
    initialAge = age;
}

To


if (initialAge > 0) {
    age = initialAge;
}

So that age stores the initialAge passed into the constructor.

Oh okay. During the task explanation it explained to set initialAge to age so by impulse I just took it to mean what I coded. Thank you! 

Link to comment
Share on other sites

Link to post
Share on other sites

27 minutes ago, Sauron said:

You realize that this:


if (initialAge > 0) {
        initialAge = age;
    }
    else {
        age = 0;
        cout << "Age is not valid, setting age to 0." << endl;
    }

means the age will always be set to 0?

-edit

this is not meant to be a snarky comment, I asked to make sure you didn't already change that

Don't worry this helped quite a bit. Thank you! 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, JonnyBoyThan said:

I don't really understand this issue all too well. I'm just doing a problem on HackerRank that's part of the 30 days of programming challenge and I've got to the point where my code looks alright but it's still producing many errors. 

For instance, whenever I input a number it will simply output "You are young." two times for every number I input. 

Here is the code: 

...

You should also handle the case where a user does enter invalid input. For example, if the user enters text in stead of a number. This will cause the stream state to go bad and all subsequent input operations to be invalid. Recommended practice is to test the stream state after each read, and reset it if bad and clear the stream to remove the data that caused the error.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×