Question

In: Computer Science

In this assignment you will be given an input file containing a series of universities along...

In this assignment you will be given an input file containing a series of universities along with that universities' location and rating. You must write a program that uses user defined functions to output a list of all school's above a certain rating to the terminal.

Your program should do the following:

- Prompt the user for the name of the input file to open

- Prompt the user for the rating threshold ( Note: we print if the rating is greater than or equal to the threshold)

- Open the file and take in the input from the file

- Use a user defined function to output a heading to the terminal with the following format:

University Rating City State
--------------- --------------- --------------- ---------------
- Use a user defined function to check if a university should be printed

- Use a user defined function to output the appropriate universities in the following format:

[University1] [Rating1] [City1] [State1]
[University2] [Rating2] [City2] [State2]

NOTE: the input file can be of any length

Example Input file: exampleInput.txt

Example output file for ratings above 50: exampleOutput.txt

Sample Code : Sample code.cpp

Solutions

Expert Solution

Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code.  Please get back to me if you need any change in code. Else please upvote

CODE:

#include <iostream>

#include <fstream>

#include <iomanip>

using namespace std;

//function prototype

void printheading();

bool check_university_rating(int rating, int threshold);

void printUniversity(string university, int rating, string city, string state);

//main function

int main()

{

               ifstream infile; //input file stream object

              

               string filename; //Variable to store filename

               int threshold; //Variable to store threshold value

              

               string university; //Variable to store university name

               string state; //Variable to store university state

               string city; //Variable to store university city

               int rating; //Variable to store rating

              

               // Prompt the user for the name of the input file to open

               cout << "Enter the name of the input file to open: ";

               cin >> filename;

              

               // Prompt the user for the rating threshold

               cout << "Enter the rating threshold: ";

               cin >> threshold;

              

               //Open the file and take in the input from the file

               infile.open(filename, ios::in); //Open the file in read mode

              

               if(!infile) { //checking if file exists

                   cout<<"File input.txt does not exist\n"; //display error if file not exists

                   exit(1);

               }

   

    //Using a user defined function to output a heading to the terminal

    printheading();

   

    while(infile.eof() == 0) //loop until end of file in input file

    {

        infile >> university; //Reading the university from file

        infile >> rating; //Reading the rating from file

        infile >> city; //Reading the city from file

        infile >> state; //Reading the state

        //Using a user defined function to check if a university should be printed

        if(check_university_rating(rating, threshold))

            //Using a user defined function to output the appropriate universities

            printUniversity(university, rating, city, state);

    }

   

    infile.close(); //closing the input file

              

               return 0;

}

//user defined function to output a heading to the terminal

void printheading(){

    cout << setw(15) << left << " University" <<" " <<setw(15) << left << "    Rating" <<" ";

    cout << setw(15) << left << "     City" <<" "<< setw(15) << left <<"     State";

    cout <<"\n--------------- --------------- --------------- ---------------\n";

}

//user defined function to check if a university should be printed

bool check_university_rating(int rating, int threshold){

   

    if(rating >= threshold)

        return true; //return true if the rating is greater than or equal to the threshold

    else

        return false; //else return false

}

//user defined function to output the appropriate universities

void printUniversity(string university, int rating, string city, string state){

    cout << setw(20) << left << university <<" " << setw(15) << left << rating <<" ";

    cout << setw(15) << left << city <<" " << setw(15) << left << state <<"\n";

}

INPUT FILE:

OUTPUT:


Related Solutions

2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
Part I The input to the program will be a text file containing the information for...
Part I The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." In C, Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume the length...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C language to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters....
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
In Python: Assume a file containing a series of integers is named numbers.txt and exists on...
In Python: Assume a file containing a series of integers is named numbers.txt and exists on the computer's Disk. Write a program that reads al the numbers stored in the file and calculates their total. - create your own text file called numbers.txt and put in a set of 20 numbers (each number should be between 1 and 100). - Each number should be on its own line. - do not assume that the file will always have 20 numbers...
Assume a file containing a series of words is named words.txt and exists on the computer’s...
Assume a file containing a series of words is named words.txt and exists on the computer’s disk. The program should generate the set of unique words in the document and display the number of times each word in the list appears in the document.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT