need your help !!

hey everyone am new here and i just need your help in some code . i need a program to input five student grades in five subjects and if the student passes them all (pass mark 50 out of 100)it outputs "pass" and if he failed in one subject it out puts "warning 1" and if he failed 2 subjects it outputs "warning 2" and if he failed 3 or more it out puts "dismiss" i started by this way but it don't feel right .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream.h> 
#include <conio.h> 
int main () 
{ 
    int a,b,c,d,e; 
    cin>>a>>b>>c>>d>>e; 
    if ((a>=50)&&(b>=50)&&(c>=50)&&(d>=50)&&(e>=50)) 
{ 
    cout<<"pass"; 
} 
if ((a<50)&&(b>=50)&&(c>=50)&&(d>=50)&&(e>=50)||(a>=50)&&(b<50)&&(c>=50)&&(d>=50)&&((e>=50)||
(a>=50)&&(b>=50)&&(c<50)&&(d>=50)&&(e>=50)||(a>=50)&&(b>=50)&&(c>=50)&&(d<50)&&(e>=50)||
(a>=50)&&(b>=50)&&(c>=50)&&(d>=50)&&(e<50))
{
     cout<<"propation2"<<endl; 
      }
    getch(); 
    return 0; 
simply count the number of subjects in which the student passed and use a switch statement.
can you use structs/classes?
From what i understand from your post, you have 5 students each with 5 subjects.
You need to go through each of the subjects and chceck if the grades are above 5.
You could use a for loop to go through each of the subjects. You use a variable to count the failed subjects. If a subject is below 50 you increase this
you then check if this counter variable is greater then 3. If it is you break out of the for loop and print dismiss and finish the program or go to the next student
if not you continue with the for loop.At the end you use a switch
if the counter is 0 you print pass, if counter is 1 you print warning, and if it's 2 you print warning 2.
For the default you could print an error message since the counter shouldn't get any other values :)
after the switch you go to the next student.
Try and code something along this lines and post it here if you stil have dificulties.
(abhishekm71) am only allowed to use the if statement .
(nedo) the code is used to test any student in the five subjects and the pass mark is 50 .
Last edited on
nedo i got u but how can i make a for loop to count the field subjects ?
If you are only allowed to use if statements try something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
short fails = 0;
if( a < 50 )
{
    ++fails;
} else if( b < 50 )
{
    ++fails;
} else if( c < 50 )
{
    ++fails;
} else if( d < 50 )
{
    ++fails;
} else if( e < 50 )
{
    ++fails;
}


I still thin a struct or class would be the best attempt like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>

struct Student
{
    std::string name;
    std::vector<unsigned short> grades;
    unsigned short fails;
};

std::ostream& operator<< ( std::ostream& stm , const Student &stu )
{
    auto count = 0u;
    stm << "Student name - " << stu.name << '\n';
    for( const auto &it : stu.grades )
    {
        ++count;
        stm << "Assignment " << count << " grade - " <<  it << '\n';
    }
    stm << "Fails - " << stu.fails << '\n';
    switch( stu.fails )
    {
    case 0:
        stm << "Student is passing.";
        break;
    case 1: case 2:
        stm << "Student has Warning " << stu.fails;
        break;
    case 3:
        stm << "Student failed.";
        break;
    }
    return( stm );
}
int main()
{
    auto numberOfStudents = 0u ,
    numberOfAssignments = 0u ,
    grade = 0u;
    std::string name = "";
    std::vector<Student> students( 0 );

    std::cout << "How many students?\n> " << std::flush;
    std::cin >> numberOfStudents;
    std::cout << "How many assignments?\n> " << std::flush;
    std::cin >> numberOfAssignments;

    for( auto i = 0; i < numberOfStudents; ++i )
    {
        Student a;
        students.push_back( a );
        std::cout << "What is the name of student " << i + 1 << "\n> " << std::flush;
        std::cin >> name;
        students[ i ].name = name;
        students[ i ].fails = 0;
        for( auto j = 0; j < numberOfAssignments; ++j )
        {
            std::cout << "Please enter grade for assignment " << j + 1 << "\n> " << std::flush;
            std::cin >> grade;
            students[ i ].grades.push_back( grade );
            if( grade < 51 )
            {
                ++students[ i ].fails;
            }
        }
    }

    for( const auto &it : students )
    {
        std::cout << it << std::endl;
    }
}
i made this code but there r still something wrong
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream.h> 
#include <conio.h> 
int main () 
{ 
    int a,b,c,d,e; 
    int x=0;
cin>>a; 
cin>>b; 
cin>>c;
cin>>d;
cin>>e;

while (a<50)
{
      x+=1;
} 

while (b<50)
{ 
      x+=1; 
} 

while (c<50)
{ 
      x+=1;
} 

while (d<50)
{
      x+=1;
} 

while (e<50)
{ 
      x+=1;
} 
if (x==0)
{
         cout<<"pass"<<endl;
} 
else if (x==1)
{ 
     cout<<"propation 1"<<endl; 
} 
else if (x==2)
{
     cout<<"propation 2";
} 
else if (x>=3)
{
     cout<<"dissmis";
}
    getch(); 
    return 0; 
}
    

If not while. While will keep adding forever since no break
For the output messages I would suggest a switch so its three output messages instead of 4 and less lines than doing else if
Last edited on
Topic archived. No new replies allowed.