I'm probably just confusing myself, but what the basic idea is I put in a grade and it outputs if the grade is an f, c, d ... etc. It allows me to enter a number and after I enter it it will continue to show F! repetitively. After this happens I haft to close the program or it continues forever it seems.

int main ()
{
		int grade; 
		grade = 1;
		cout << setprecision(1) << fixed; 

	cout << "Enter the Students Grade Between 1 and 100 (-1 to stop):\n"; 
	cin >> grade; 

	while ((grade >= -1)||(grade >= 101))
	{
		
		if (grade <= 49)
		{
			cout << "F!"<< endl; 
		}
		else if ((grade == 50)||(grade <= 59) ) 
		{
			cout << "D!" << endl; 
		}
		else if ((grade == 60)||(grade <=69))
		{
			cout << "C!" << endl;                                                                              
		}
	}

	return 0; 
}

Recommended Answers

All 7 Replies

While loop is obviously not the right choice here .. whatever grade you enter will satisfy the condition always and the loop becomes infinite... just a simple 'if' should do i think if its to happen only once. or else you can use a 'yes'-'no' sort of menu.

int grade;
    const char* reply;
    while (std::cout << "Enter the Students Grade [1..100]\n"
        "or not-a-number to quit: " << std::flush, 
        (std::cin >> grade) && grade > 0 && grade <= 100)
    {	
        if (grade < 50)
            reply = "F!"; 
        else if (grade < 60) 
            reply = "D!"; 
        else if (grade < 70)
            reply = "C!";
        else { // ?????????
            reply = "Mea culpa: else missed...";
        }
        std::cout << reply << std::endl;
    }

You need to ask the question to input the grade inside your while loop, else it will keep looping on your first input infinitely !!

int main ()
{
  int grade; 
  grade = 1;
  cout << setprecision(1) << fixed; 
  cout << "Enter the Students Grade Between 1 and 100 (-1 to stop):\n"; 
  cin  >> grade; 

  while ((grade > -1) && (grade <= 100)){
    if (grade <= 49)
    {
      cout << "F!"<< endl; 
    }
    else if ((grade == 50)||(grade <= 59) ) 
    {
      cout << "D!" << endl; 
    }
    else if ((grade == 60)||(grade <=69))
    {
      cout << "C!" << endl;                                                                              
    }
    cout << "Enter the Students Grade Between 1 and 100 (-1 to stop):\n"; 
    cin  >> grade; 
  }

  return 0; 
}

With minimum change to what you already have:

while ((grade > 0) && (grade <= 100))
	{
	            cout << "Enter the Students Grade Between 1 and 100 (-1 to stop):\n"; 
	            cin >> grade; 
		if (grade <= 49)
		{
			cout << "F!"<< endl; 
		}
		else if ((grade == 50)||(grade <= 59) ) 
		{
			cout << "D!" << endl; 
		}
		else if ((grade == 60)||(grade <=69))
		{
			cout << "C!" << endl; 
                                }
	}

You seem to have solved the looping problem, except that in most tests a grade of zero is possible and your (grade > 0) condition excludes that.

An aside: I knew a teacher who came up with a test that, she said, nobody could earn a zero grade .... because there was one mark for candidates spelling their name correctly. One of her students mispelt his name.

You seem to have solved the looping problem, except that in most tests a grade of zero is possible and your (grade > 0) condition excludes that.

If you are refering to the solution I provided, that's because he prompt the user to enter a score between 1 to 100 in his original question.

Thanks for the help I see what I did now, I managed to get it working and I added (grade < -1) just in case of a 0 grade even though it says 1-100.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.