Help figuring out equation.

This is a discussion on Help figuring out equation. within the C++ Programming forums, part of the General Programming Boards category; How could I write these so that they would work?The "A" and the "F" work but the other 3 don't. ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    23

    Help figuring out equation.

    How could I write these so that they would work?The "A" and the "F" work but the other 3 don't.

    Code:
    cout << "Letter Grades" <<endl;
    	for (int i = 0; i < MAXGRADES; i++)
    	{
    		if (grade[i]>89)
    			cout<<"A"<<grade[i];
    		if (90>grade[i]>79)
    			cout<<"B"<<grade[i];
    		if (80>grade[i]>69)
    			cout<<"C"<<grade[i];
    		if (70>grade[i]>59)
    			cout<<"D"<<grade[i];
    		if (grade[i]<60)
    			cout<<"F"<<grade[i];
    	}

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Use &&, which performs a logical AND on two arguments.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    The construct:

    Code:
    a > b > c
    Does not do what you think it does. Instead, use:

    Code:
    a > b && b > c

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    OK. Thanks. I should have known that!

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    This is all you should need:
    Code:
    cout << "Letter Grades" <<endl;
    for (int i = 0; i < MAXGRADES; i++)
    {
        if (grade[i]>89)
            cout<<"A"<<grade[i];
        else if (grade[i]>79)
            cout<<"B"<<grade[i];
        else if (grade[i]>69)
            cout<<"C"<<grade[i];
        else if (grade[i]>59)
            cout<<"D"<<grade[i];
        else
            cout<<"F"<<grade[i];
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed! Finding Root of an Equation?
    By reader in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 11:10 AM
  2. Replies: 15
    Last Post: 11-04-2006, 04:06 AM
  3. IDEA: Equation solver
    By Magos in forum Contests Board
    Replies: 2
    Last Post: 01-07-2003, 11:46 AM
  4. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 09:21 PM
  5. equation solving
    By ajlott_coder in forum C Programming
    Replies: 6
    Last Post: 01-26-2002, 02:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21