Question

In: Computer Science

C++ Write a program that asks a teacher to input a student’s first name, last name,...

C++

Write a program that asks a teacher to input a student’s first name, last name, and four test scores. The program should find the average of the four test scores and should then write the following information to a file named “students.txt”

last_name

first_name

average

A student's first name of “XX” should be used as a sentinel value and no numeric grades less than 0 or greater than 100 should be accepted.  The program should then read the information in for the file “students.txt”, and for each student display the following on the monitor:

first_name last_name    average

first_name last_name    average

Number of students        n

Overall average:               nn.n

Note: all averages should be displayed with one decimal position

Solutions

Expert Solution

Below is the source code. Please comment if any queries or issues. Thanks.

#include <iostream>
#include<bits/stdc++.h> 
#include <fstream>
using namespace std;


class Student{
    public:
    string firstName;
    string lastName;
    float marks[4];
    float average;
};
int main()
{
    char cont = 'N';
        float exam1[100], total = 0 , total_average = 0;
    int count=0;
    ifstream infile; 
    string line;
        
    do {
        
        Student s;
        cout<<"Enter Student's first name"<<endl;
        cin>>s.firstName;
        cout<<"Enter Student's Last Name" <<endl;
        cin>>s.lastName;
        cout<<"Enter Marks"<<endl;
        float marks;
        for (int i = 0; i < 4 ; i++){
            cin>>marks;
            if (marks <0 || marks > 100){
                cout<<"Marks cannot be less than 0 or greater than 100, please enter again"<<endl;
                i = i-1;
                continue;
            }
            else
                s.marks[i] = marks;
        }
        
        
        float sum = 0;
        for (int i = 0; i < 4; i++){
            sum = sum + s.marks[i];
        }
        s.average = sum/4;

        char filename[ ] = "students.txt";

        ofstream outputfile;


        // creating, opening and writing/appending data to a file
        
        outputfile.open(filename, ios::out|ios::app);

        // simple error handling for file creating/opening for writing, test if fail to open the file
        
        if(outputfile.fail())
        {
            cout<<"The "<<filename<<" file could not be created/opened!\n";
            exit(1); // just exit
        }
        else
        {
            cout<<"The "<<filename<<" file was created and opened successfully!\n";
           // outputfile<<"Writing some data to this file\n";
            //outputfile<<"------------------------------\n";
            cout<<"Check the "<<filename<<" file contents :-)"<<endl;
            cout<<"If the file already have had data, the new data will be appended\n";
            
            outputfile<<s.firstName<<" "<<s.lastName<< " " << fixed << setprecision(1) << s.average;
            
            outputfile<<endl;
            
            outputfile.close();
        
            if(outputfile.fail())
            
            {
                cout<<"The "<<filename<<" file could not be closed!\n";
                exit(1);
            }
            // test if successful to close the file, do the following...
            else
                cout<<"\nThe "<<filename<<" file was closed successfully!\n";
        }
        cout<<"Enter Y/y if there is another student"<<endl;
        
        cin>>cont;
    }while(cont == 'Y' || cont == 'y');

  ifstream myfile ("students.txt");
  if (myfile.is_open())
  {
    
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file";
  
  
  //To calcualte the average 
     cout<< "Displaying the Student Data\n";
     infile.open("students.txt");// file containing numbers in 3 columns 
     if(infile.fail()) // checks to see if file opended 
     { 
      cout << "error" << endl; 
      return 1; // no point continuing if the file didn't open...
     } 
     while(getline(infile, line)) // reads file to end of *file*, not line
     { 
         
         stringstream ss(line);
         char a[50], b[50]; 
         float c;
        if (ss >> a >> b >> c)
        {
            exam1[count] = c;
            count++;
        }
      } 
    infile.close(); 
    cout << "Number of students " << count <<"\n";
   for (int i=0; i<count;i++){
       total = total + exam1[i];
   }
   total_average = total/count;
   cout << "Overall average: "  << fixed << setprecision(1) << total_average; 

  return 0;

}


Below is the execution screenshot


Related Solutions

C++ Change the program to take user input for first name and last name for five...
C++ Change the program to take user input for first name and last name for five employees. Add a loop to read the first name and last name. // EmployeeStatic.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> #include <string> using namespace std; class Employee { public:    Employee(const std::string&, const std::string&); // constructor    ~Employee(); // destructor    std::string getFirstName() const; // return first name    std::string getLastName() const; // return...
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other.
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Write a program that: a. Asks the user for their first name using a JOptionPane. b....
Write a program that: a. Asks the user for their first name using a JOptionPane. b. Asks the user for their age using a JOptionPane. C. Asks the user for their last name using a JOptionPane. d. Pops up a dialog with the following information: i. Number of characters in the first & last name combined ii. The full name all-in upper-case letters iii. The full name all-in lower-case letters iv. The first letter of the name v. The age...
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
Write a program that prompts the user to input their first name from the keyboard and...
Write a program that prompts the user to input their first name from the keyboard and stores them in the variable "firstName". It does the same for last name and stores it in the variable "lastName". It then uses strcat to merge the two names, separates them by a space and stores the full name into a string variable called "fullName". In the end, the program must print out the string stored within fullName. ANSWER IN C LANGUAGE ! You...
1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
Write a C program that loops and asks you every time to enter the name of...
Write a C program that loops and asks you every time to enter the name of a text file name, then it will read the file and perform statistics on its contents and display the results to the screen and to an output file called out.txt. The input files can be any text file. The program should calculate these integers as it reads character by character as we did in the last class example filecopy and make use of the...
Design and implement a C++ Application that: Interactively input: Employee First Name Employee Last Name Employee...
Design and implement a C++ Application that: Interactively input: Employee First Name Employee Last Name Employee id Employee hours worked per week Employee Pay Rate Menu with Option to: Print out Employee Report in the following report format Search for an Employee Display the Report in Sorted based on Last Name or ID Calculate the Pay Quit Criteria: Be sure to use Parallel Arrays to Store the Employee Information Output must be formatted and line up with the column header...
Write a C++ program using separate void which asks the user to input side of a...
Write a C++ program using separate void which asks the user to input side of a square, radius of a circle , height and base of a triangle and finds the area of squares, circles and triangles. Then using main function display the area of square, circle and triangle
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT