Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: while

  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Malaysia
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    while

    Dear sir

    In my program, which should continue to accept values until a negative number is input. But my while function can't be accepted. Why???

    void main(void)
    {
    int score;
    int a_s=0, b_s=0, c_s=0, d_s=0, f_s=0;

    clrscr();

    printf ("SCORE? ");
    fflush (stdin);
    scanf ("%i", &score);

    while (score >= 0 || score <= 100)
    {

    if (score <= 100 && score >= 90)
    {
    a_s += 1;
    printf("THE GRADE IS A\n\n");
    }

    else
    if (score <=89 && score >= 80)
    {
    b_s += 1;
    printf("THE GRADE IS B\n\n");
    }

    else
    if (score <= 79 && score >= 70)
    {
    c_s += 1;
    printf("THE GRADE IS C\n\n");
    }

    else
    if (score <= 69 && score >= 60)
    {
    d_s += 1;
    printf("THE GRADE IS D\n\n");
    }

    else
    if (score <=59 && score >= 0)
    {
    f_s += 1;
    printf("THE GRADE IS F\n\n");
    }

    printf ("SCORE? ");
    fflush (stdin);
    scanf ("%i", &score);

    }

    printf ("\n");
    printf ("%i \t A's\n", a_s);
    printf ("%i \t B's\n", b_s);
    printf ("%i \t C's\n", c_s);
    printf ("%i \t D's\n", d_s);
    printf ("%i \t F's\n", f_s);

    getch();

    }


    Thanks


    Xiang

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Ughh, C. Try this:

    Code:
    #include <iostream>
    
    int main() {
    	int grade, amounts[5];
    	char letter;
    
    	amounts[0] = amounts[1] = amounts[2] = amounts[3] = amounts[4] = 0;
    
    	while (cin >> grade && grade >= 0) {
    		switch (grade/10) {
    			case 10:
    			case 9:
    				letter = 'A';
    				amounts[0]++;
    				break;
    			case 8:
    				letter = 'B';
    				amounts[1]++;
    				break;
    			case 7:
    				letter = 'C';
    				amounts[2]++;
    				break;
    			case 6:
    				letter = 'D';
    				amounts[3]++;
    			default:
    				letter = 'F';
    				amounts[4]++;
    		}
    		cout << "The grade is: " << letter << endl << endl;
    	}
    
    	cout << endl
    	     << "A\'s:\t" << amounts[0] << endl
    	     << "B\'s:\t" << amounts[1] << endl
    	     << "C\'s:\t" << amounts[2] << endl
    	     << "D\'s:\t" << amounts[3] << endl
    	     << "D\'s:\t" << amounts[4] << endl;
    
    	return 0;
    }

  • #3
    New to the CF scene
    Join Date
    Feb 2003
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Take a look at the conditions for your while loop. The loop will accept input while the input is greater than orequal to zero OR while the input is less than or equal to 100.

    If I input -20, this is still less than or equal to 100, so the loop will continue. The fix is pretty simple, I'm sure you can easily figure it out.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •