Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// grade.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;

//class grade
//{
//
//public:
//
//	void getgrade();
//	void print();
//private:
//};
//
//void grade ::getgrade()
//{
//
//
//}

int main()
{

	int m , n  ;
	cout << "how many students do you have ? " << endl ;
	cin >> m ;

	cout << "plz enter the grades :" << endl ;
	for( int i = 0 ; i< m ; i++ )
	cin >> n;
	for( int i = 0 ; i< m ; i++ )
	if(n>=17 && n<=20)
	{
		cout << 'A' << endl ;
	}
	if(n>=13 && n<=16)
	{
		cout << 'B' << endl;
	}
	if(n>=10 && n<=12)
	{
		cout << 'C' <<endl;
	}
	if(n>=6 && n<=9)
	{
		cout << 'D' <<endl;
	}
	if(n>=0 && n<=5)
	{
		cout << 'E'<<endl;
	}
	/*switch(n)
	{
	case 1:
		cout << 'A' << endl ;
		break;
		case 2:
		cout << 'B' << endl;
		break;
		case 3:
		cout << 'C' <<endl;
		break;
		case 17:
		cout << 'D' <<endl;
		break;
	}*/

	
	return 0;
}

</iostream>
Posted

It would help if you said what problem you are getting.
The first change I would make is:
Enclose your loop content in a block. It is always worth doing.
for( int i = 0 ; i< m ; i++ )
if(n>=17 && n<=20)
{
    cout << 'A' << endl ;
}
if(n>=13 && n<=16)
{
    cout << 'B' << endl;
}
Does this execute one "if" in a loop, or two?
for( int i = 0 ; i< m ; i++ )
    {
   if(n>=17 && n<=20)
   {
    cout << 'A' << endl ;
   }
   if(n>=13 && n<=16)
   {
    cout << 'B' << endl;
   }
    }
Is a lot clearer: it executes both "if" inside the loop.
Try it: it may get rid of your problem. If it doesn't, try telling us what your problem is!



"for example in my program if i write 20 , 16 the program give me (B & B)but it is not correct it must be (A & B)"


Ok. It is what I was describing above, but slightly more serious.
Your code:
cout << "plz enter the grades :" << endl ;
for( int i = 0 ; i< m ; i++ )
cin >> n;
for( int i = 0 ; i< m ; i++ )
if(n>=17 && n<=20)
Means that the line
cin >> n;
is exectuted "m" times. Then it moves on to the next for loop.
A for loop, or an if condition, only executes the statement immediately after it.
for (int i = 0; i < 10; i++)
   printf("This line prints 10 times\n");
printf("This line prints once\n");
or
if (myInteger == 0)
   printf("This prints only when myInteger is zero\n");
printf("This prints every time\n");
If you want to execute more than one statement, you need to make it a statement block by enclosing it in curly braces:
for (int i = 0; i < 10; i++)
   {
   printf("This line prints 10 times\n");
   printf("So does this one\n");
   }
printf("This line prints once\n");
or
if (myInteger == 0)
   {
   printf("This prints only when myInteger is zero\n");
   printf("So is this one\n");
   }
printf("This prints every time\n");


Change your code: put curly braces in EVERYWHERE you want to execute - even if it is a single line in a if condition. You will understand later when you can leave them out - but for now, always put them in.

cout << "plz enter the grades :" << endl ;
for( int i = 0 ; i< m ; i++ )
       {
       cin >> n;
   for( int i = 0 ; i< m ; i++ )
          {
          if(n>=17 && n<=20)
         {
     cout << 'A' << endl ;
         }
      if(n>=13 && n<=16)
         {
     cout << 'B' << endl;
         }
      if(n>=10 && n<=12)
         {
     cout << 'C' <<endl;
         }
      if(n>=6 && n<=9)
         {
     cout << 'D' <<endl;
         }
      if(n>=0 && n<=5)
         {
     cout << 'E'<<endl;
         }
      }
       }
return 0;
See the difference?
 
Share this answer
 
v2
Comments
yay hello 3-Apr-11 8:43am    
for example in my program if i write 20 , 16 the program give me (B & B)but it is not correct it must be (A & B).
OriginalGriff 3-Apr-11 9:06am    
Answer updated
yay hello 3-Apr-11 9:19am    
thank you 8)
This part of your code is not proper for your propose:
for( int i = 0 ; i<m mode="hold"> cin >> n;

if you want to have m numbers, you can create a dynamic array with the length of m and store each number in a separate array member. At the end of your program, you should delete the pointer to created dynamic array.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900