Question

Starting Out with c++ Student Line Up Using Files A teacher has asked all her students...

Starting Out with c++ Student Line Up Using Files A teacher has asked all her students to line up according to their first name. For example, in one class Amy will be at the front of the line, and Yolanda will be at the end. Write a program that will prompt the user for a filename that contains the student names. Names should be read in until there is no more data to read. As the names are read in, you will check to see if the new name should be in the first or last student. Do not use arrays or sorting for this problem!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Program to read student names from a file and output the first and last student names when arranged alphabetically.

#include<iostream>
#include<fstream>
using namespace std;

int main()
{

//Intialize variables
string studentName;
string first="";
string last="";
ifstream inputFile;
string filename;
  

//Get file name from user
cout << "Please enter the name of the file with the list of students\n";
cin >> filename;

//Open the file
inputFile.open(filename);
if (!inputFile)
{
   cout << "Error opening the file.\nPlease restart the program and try again.";
   return 1;
}
while (inputFile >> studentName){
    if(first=="" && last=="")
    {
        first=studentName;
        last=studentName;
    }
if(studentName<first)
first=studentName;
  
if(studentName>last)
last=studentName;
  
}
cout << first << " is the first student in line.\n";
cout << last << " is the last student in line.\n";
   
inputFile.close();
return 0;
}

Output

//name.txt

//code screenshots

Add a comment
Know the answer?
Add Answer to:
Starting Out with c++ Student Line Up Using Files A teacher has asked all her students...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I would love it if someone would make this code from scratch.. not copied from somewhere...

    I would love it if someone would make this code from scratch.. not copied from somewhere else. I just want this explained and how it is done. The extra credit would be nice too.. This is C++ ASSIGNMENT: A7 - Student Line Up "Student Line Up", page 295, Number 14 A teacher has asked all students to line up single file according to their first name. For example, in one class Amy will be in the front of the line...

  • You are asked to define a user-defined data type for students, that will contain the following...

    You are asked to define a user-defined data type for students, that will contain the following information about a student: ID number, first name, last name, major, GPA. Your structure should be cal Student. Based on the defined structure, user name on the command line, show how you will use a loop to search for the given name within the array of 5 students. use an array to record information about 5 students. Then, given a Write a C program...

  • Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...

    Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file: Count: 5 Sally 78.0 84.0 79.0 86.0 Rachel 68.0 76.0 87.0 76.0 Melba 87.0 78.0 98.0 88.0 Grace 76.0 67.0 89.0 0.0 Lisa 68.0 76.0 65.0 87.0 The first line of the file will indicate the number of students...

  • Please solve using c++ plus There are two text files with the following information stored in...

    Please solve using c++ plus There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a C++ program that reads these text files and provides user with the following menu: 1. Enter the instructor ID...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

  • using C# Write a program which calculates student grades for multiple assignments as well as the...

    using C# Write a program which calculates student grades for multiple assignments as well as the per-assignment average. The user should first input the total number of assignments. Then, the user should enter the name of a student as well as a grade for each assignment. After entering the student the user should be asked to enter "Y" if there are more students to enter or "N" if there is not ("N" by default). The user should be able to...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • You are asked to build and test the following system using Java and the object-oriented concepts ...

    You are asked to build and test the following system using Java and the object-oriented concepts you learned in this course: Project (20 marks) CIT College of Information technology has students, faculty, courses and departments. You are asked to create a program to manage all these information's. Create a class CIT to represents the following: Array of Students, each student is represented by class Student. Array of Faculty, each faculty is represented by class Faculty. Array of Course, each course...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a...

    Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and writemarks() The program begins by calling the function readmarks () to read the input file marksin. txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks () the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT