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 .

 ``123456789101112131415161718`` ``````#include #include 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"<
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
 ``1234567891011121314151617`` ``````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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#include #include struct Student { std::string name; std::vector 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 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
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include #include 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"<=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.