0

I am very new to c++ and I was trying to put together a script that just says how much older/younger someone is than me. The problem is the std::cin isn't working, it's not letting me say the input age. How do I make this work?

#include <iostream>

int main()
{
    int age;
    int diff = age - 20;

    std::cout << "What is your age?\n";
    std::cin >> age;
    /* There should be an option to say the age, but it goes right into the next code with a random number */

    if (diff < 20) {
        std::cout << "You are " << diff << " years younger than me.\n";
    }

    else if (diff > 20) {
        std::cout << "You are " << diff << " years older than me.\n";
    }

    else if (diff = 20) {
        std::cout << "You are my age.\n";
    }
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 1
    Your input problem has to do with whatever step you are using to run your code. You will have to explain for help.; – drescherjm Dec 10 '21 at 18:07
  • 2
    `else if (diff = 20) {` Using = makes this an assignment. Use == for comparison. In this case it won't matter because diff is 20 but you should use == for comparison – drescherjm Dec 10 '21 at 18:07
  • 4
    You don't use `age` after you read it in. It's only used before it has a value. – Kyle Dec 10 '21 at 18:20
  • 4
    C++ is not like a spreadsheet. It does not automatically recompute values that depend on a variable when you change a variable. All of the steps must be in the code in the order you need them performed. Eg: Get value from user, compute difference, make decision on difference. – user4581301 Dec 10 '21 at 18:23
  • 5
    `int diff = age - 20;` is undefined behavior. You did this calculation before `age` had a value. – drescherjm Dec 10 '21 at 18:24

1 Answers1

0

When you say int age;, there's no rhyme or reason to the bits stored in memory that represent that integer. It's undefined behavior, but it will be equal to some random value based on what bits happen to be there. You then use std::cin to store an integer there, which works fine (assuming your code keeps chugging along despite using age before it has a value). Your conditionals then compare diff, which has a random value minus 20, with 20, outputting the right statement based on whatever was stored in diff. For example, when I ran your code, I got the output, "You are -1635346580 years younger than me." To fix this problem, read the value of the user's age before using it like this:

int age;
std::cout << "What is your age?\n";
std::cin >> age;

int diff = age - 20;

Additionally, when you wrote else if (diff = 20), you used an assignment operator. That puts the value of 20 into the variable diff. It then returns that value for use in the expected Boolean. In C++, 0 translates to false, and everything else translates to true. That means, once it gets there, it will always execute since 20 becomes true. Your program will always function correctly, however, since by that point in the code, diff is guaranteed to be 20 since it's neither greater than 20 nor less than 20. You could say else if (diff == 20) to do the comparison, but I'd replace it with else // diff is 20 since you know the value by that point.

user904963
  • 1,520
  • 2
  • 15
  • 33