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.

another c++ question?

why is this outputting 00 even when the if values are true

int main()

{

float mover05(0); // match over 0.5

float mover15(0);

float mover25(0);

float mover35(0);

float mover45(0);

float munder05(0); //match under 0.5

float munder15(0);

float munder25(0);

float munder35(0);

float munder45(0);

char a;

int count(0);

cout << "Enter the total number of goals in match" << endl;

cout << "enter n to stop adding scores" << endl;

while (a != 'n')

{

cin >> a;

if (a == 0) { munder05++; munder15++; munder25++; munder35++; munder45++; }

else if (a == 1) { munder15++; munder25++; munder35++; munder45++; mover05++; }

else if (a == 2) { munder25++; munder35++; munder45++; mover05++; mover15++; }

else if (a == 3) { munder35++; munder45++; mover05++; mover15++; mover25++; }

else if (a == 4) { munder45++; mover05++; mover15++; mover25++; mover35++; }

else if ( a >= 5){ mover05++; mover15++; mover25++; mover35++; mover45++; }

}

cout << munder05 << munder45;

cin.get();

3 Answers

Relevance
  • 9 years ago
    Favorite Answer

    You can't expect a char data type to store a numeric value. Computers aren't that smart, well smart at math. Change your if statement to this:

    if (a == '0')

    The 0 is stored as a char data type, not a numeric data type.

    Also, you didn't ask the user for input after the cout statements. Add a "cin >> " statement for user input.

    cout << "Enter the total number of goals in match" << endl;

    cout << "enter n to stop add scores" << endl;

    cin >> a;

    You also need a return statement at the end: return 0;

  • 9 years ago

    As previous answer pointed out, a is going to end up with values like '0' or '1', not 0 or 1. They are very different things.

    Also, since you didn't initialize the a variable, there is a 1 in 256 chance that it will just happen to equal 'n'. Your while loop won't execute at all in that case. So your while loop executes most of the time on most machines, but not all the time on all machines. And you can piss away many man hours figuring out why.

    Also: Any time you are adding numbers to the end of variable names, you are probably doing it wrong. That's what arrays are for. Consider making munder and mover arrays, and build your code around that. I think you'll find the logic is much simpler that way.

  • ?
    Lv 7
    9 years ago

    I have no idea what the purpose of this is but you have two major errors...

    assignment should be =0 not (0)

    and comparison should be to chars not ints... if (a == '1') and not if (a==1)

    Fix those two errors and it will work fine (for whatever it does).

Still have questions? Get your answers by asking now.