Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

whats wrong with this C++ code?

Its adding the int variables up to there max. i don't understand why it would be doing this. thanks in advance.

#include <iostream>

using namespace std;

int main()

{

int a, one, two, three, four;

cin >> a;

if (a == 0) one++;

if (a < 2) two++;

if (a < 3) three++;

if (a < 4) four++;

cout << one << two << three << four;

cin.get();

cin.get();

return 0;

}

3 Answers

Relevance
  • 9 years ago
    Favorite Answer

    Not initializing one, two, three and four is certainly a problem.

    But even if you fix that, I don't get what you are trying to do. "adding the int variables up to there max" doesn't really make sense to me. I DON'T KNOW WHAT THAT MEANS!!!

    If you fix the initialization problem by setting one, two, three and four to 0, then...

    You enter 0 and you get output of 1111

    You enter 1 and you get output of 0111

    You enter 7 and you get output of 0000

    Is that what "adding the int variables up to there max" really means? That doesn't sound right to me. I think not initializing the variables is a problem, but it's just a side issue. what does "adding the int variables up to there max" really mean? What is the expected output for 0, 1 and 7? I'm somewhat sure that it isn't 1111 0111 and 0000.

  • 9 years ago

    You declare the variables but you haven't set it equal to any values. When you declare variables, its memory is stored in ram, which is the max value stored until you set it equals to some values.

    "a", "one", "two", "three", "four", all stored the max value of an integer data type because you didn't set it equal to some values, for example 0 or 3 or 2. To do this, you simply do this:

    int a = 0 // or any number

    int one = blah blah

    int two = blah blah

    int three = blah blah

    int four = blah blah

    Use only one cin.get();

  • ?
    Lv 7
    9 years ago

    You didn't define the variables ....it just gives you whatever was in memory at those locations...

    add the line

    one = two = three = four = 0;

    In the future you need to be doing this with an array.

Still have questions? Get your answers by asking now.