user6911170
user6911170

Reputation:

how can i add two cpp to one single cpp program file

We have following results of a course with exam: 75 of the students who completed this course took the exam. We want to know how well the students did in the examination and are given the scores of 75 students. We want to write a program that summarizes and analyzes the results the following way: 1. Use random number generator Use 5 (Matric/Identification number of only five digits). 2. Input each test result (i.e., raw marks). Display the prompting message “Enter result” each time the program requests another test result. a) You are to enter the raw score and categorize it as follows: i. 90 and above is A ii. 89-80 is B iii. 79-70 is C iv. 69- 60 is D v. 59 and below is E 3. Count the number of test results of each type. 4. Further categorize the result into Pass or Fail (a P or an F) i. All As, Bs, Cs and Ds are under the P category while E is under F category. ii. Next to each matric/identification number is written a P if the student passed the exam or an F if the student failed. 5. Display a summary of the test results indicating the number of students who passed and the number who failed. 6. Program should restrict user from entering invalid marks (char, negative number, above 100 etc.)


this is my uni assingment. i have done the code part but i like to add these 2 code in one-single program. but can not figure out how know how here is the first code

      #include <iostream>
     using namespace std;
     int main()
     {
       int marks;
       int matric;
       cout <<"Matric/Identification number of only five digits \n";
       cin >> matric;
       cout <<"Enter marks \n";
       cin >> marks;
       if (marks <=50)
        { 
         cout << " Your grade is F \n";
         cout << " You fail in the exam";
        }
          else if (marks >=50 && marks <=59)
 {
    cout << " Your grade is E" <<endl;
    cout << " You pass in the exam" <<endl;
 }
 else if (marks >=60 && marks <=69)
 {
    cout << " Your grade is D" <<endl;
    cout << " You pass in the exam" <<endl;
 }
 else if (marks >=70 && marks <=79)
 {
    cout << " Your grade is C" <<endl;
    cout << " You pass in the exam" <<endl;
 }
 else if (marks >=80 && marks <=89)
 {
    cout << " Your grade is B" <<endl;
    cout << " You pass in the exam" <<endl;
 }
 else if (marks >=90 && marks <=100)
 {
    cout << " Your grade is A" <<endl;
    cout << " You pass in the exam" <<endl;
 }
 else if (marks <=-1 && marks >=101)
    cout << " Mark doesn't exsit" <<endl;     
     }

here is another code

  #include <iostream>
 using namespace std;
 int main()
 {
int passes = 0;
int failures = 0;
int studentCounter = 1;
int result;

while (studentCounter <=10)
{
    cout << "Enter Result";
    cin >> result;

    if (result >=50)
     passes = passes+1;
    else
     failures = failures+1;
    studentCounter = studentCounter + 1;          
}

cout << "passed" << passes <<"\nFailed" << failures << endl;

if( failures > 3)
   cout <<"Failures not eligible for Engineering " <<endl;
}

SAMPLE

Matric/Identification number of only five digits: 89798

Enter result: 90

Upvotes: 0

Views: 113

Answers (1)

Hedgehog
Hedgehog

Reputation: 60

Do you know what methods are? I don't know what is your level of programming so I will try to explain everything.

Well, first, you can see, that everything is in one file. your first file is not bool result() method, which means that it will return true(if passed the test) and false(if failed). I added while(true) in that part in case user enters invalid marks, he will be asked to do that again. In all other cases method will return value.

Now in main method(Do you know that main method is an entry point of every code? It means that program will start on the main method) I deleted line which require to enter result. Instead I call method result(), which asks to enter the marks and returns 1(true) if passed the test and 0(false) if failed the test. Also you can see that I changed your passes = passes + 1; to passes += 1;. There is no difference in logic, it's just looks better, and shorter.

#include <iostream>
using namespace std;



bool result1()
     {
       int marks;
       int matric;
       cout <<"Matric/Identification number of only five digits \n";
       cin >> matric;
       cout <<"Enter marks \n";
       cin >> marks;
       while(true){
       if (marks <=50)
        { 
         cout << " Your grade is F \n";
         return 0;
        }
          else if (marks >=50 && marks <=59)
        {
         cout << " Your grade is E" <<endl;
         return 1;
        }
         else if (marks >=60 && marks <=69)
        {
         cout << " Your grade is D" <<endl;
         return 1;
        }
         else if (marks >=70 && marks <=79)
        {
         cout << " Your grade is C" <<endl;
         return 1;
        }
         else if (marks >=80 && marks <=89)
        {
         cout << " Your grade is B" <<endl;
         return 1;
        }
         else if (marks >=90 && marks <=100){
         cout << " Your grade is A" <<endl; 
         return 1;
        }
         else if(marks < 0 || marks >100)
          cout << "Invalid marks" << endl << "Enter marks again!";
        }
    }

    int main()
    {
      int passes = 0;
      int failures = 0;
      int studentCounter = 1;
      int result;

      while (studentCounter <=10)
      {

         if (result1())
            passes += 1;
         else
            failures += 1;
         studentCounter += 1;          
      }

      cout << "passed" << passes <<"\nFailed" << failures << endl;

      if( failures > 3)
         cout <<"Failures not eligible for Engineering " <<endl;
      }

As you can see I call result() in if statement. That's the right way. You could write:

bool res = result();
if(res){
}
else{
}

But that would be just the same. You can put anything you want in if statement as long as it returns, or can be interpreted as boolean value.

Also for theory: I called bool result() a method, but that's wrong. bool result() is a function, because it returns a value(same as math functions f(x) always has a value of y). Now void result() is a method, because it doesn't return any value.

Oh, and, by the way, every method or function you write in your code, should be written before you use them. That's why bool result() is written before int main(), thought it would be better to have entry point at the very top, right?

I hope that this is what you need. Good luck

Upvotes: 1

Related Questions