Question

Below is the problem that I have and here is the code that I have in...

Below is the problem that I have and here is the code that I have in c++. Can someone help me on what I am doing wrong or the correct code.

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 prompts the user to enter the number of students in the class, then loops to read that many names. Once all the names have been read, it reports which student would be at the front of the line and which one would be at the end of the line. You may assume that no two students have the same name.

Input Validation: Do not accept a number less than 1 or greater than 25 for the number of students

#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
   // declaring interger type values
   ifstream inputFile;
   string names[20];
   int i, n = 0;
   // opening a file
   inputFile.open("lineUp.txt");

   if (!inputFile)// check for file
       cout << "error in opening file\n";
   else
   {
       while (inputFile >> names[n])
       {
           n++;

       }
       n++;

       //sorting names
       int temp;
       // loop to iterate all names
       for (int i = 0;i <= n - 1;i++)
       {
           temp = i;
           //for string each string
           for (int j = i + 1;j

               if (names[j]
                   temp = j;
                   names[i].swap(names[temp]);
       }
       inputFile.close();

   }
   //end close
       //display name at front
   cout << "Front line:" <<names[1] << endl;
       //display name at end
       cout << "end line:" <<names[n - 1] << endl;
   //pause system for a while
   system("pause");
  
   return 0;
}
}

Homework Answers

Answer #1

//C++ program

#include <iostream>
using namespace std;

int main()
{
// declaring interger type values
int no_of_students;
string front,end;


while(1){
        cout<<"Enter the number of students in the class : ";
        cin>>no_of_students;
        if(no_of_students>=1 && no_of_students<=25)break;
        cout<<"Number of students should between 1 and 25\n";
}

cin.get();
string name[no_of_students];

for(int i=0;i<no_of_students;i++){
       
        cout<<"Enter name of student-"<<i+1<<" : ";
        getline(cin,name[i]);
       
        if(i==0){
            front = name[0];
            end = name[0];
       }
       else{
           if(name[i]<front)front = name[i];
           if(name[i]>end) end = name[i];
       }
}

cout << "Front line:" <<front << endl;
//display name at end
cout << "end line:" <<end << endl;
//pause system for a while
system("pause");
  
return 0;
}


//sample output

Know the answer?
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
Similar Questions
read a text file into two parrall arrays. The file has a name and a grade...
read a text file into two parrall arrays. The file has a name and a grade here is what I am doing not working #include < iostream> #include < fstream> #include <string> usind namespace std; int main() { int const size =2; string names[size]; int age[size]; ifstream inputFile; inputFile.open("text.txt"); for (int i = 0; i <size; i++) { inputFile >> names[i]; inputFile>> age[i]; cout << age[i] << endl; cout << name[i] << endl; } inputFile.close(); return 0; }
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Hello, I feel like I am super close but I can not figure out why my...
Hello, I feel like I am super close but I can not figure out why my C++ code it not displaying the proper medium. Thank you! Here is an example of the output: Input : a[] = {1, 3, 4, 2, 6, 5, 8, 7} Output : Mean = 4.5 Median = 4.5 Code so far:   #include <iostream> using namespace std; int main() { int a[100]; int n,i,sum=0; float mean, medium; //read array size // read array cout<<"Enter array size:...
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
C++ Question I have a text file that contains data from a CD (ex 1. Adagio...
C++ Question I have a text file that contains data from a CD (ex 1. Adagio “MoonLight” Sonata - Ludwig Van Beethoven /n 2. An Alexis - F.H. Hummel and J.N. Hummel) How do I sort the data by author since that information is in the middle of the string? Here's what I have that sorts the string from the beginning: if (file.is_open())    {        while (getline(file, line))        {            lines.push_back(line);        }...
Write a program that accepts as input the mass, in grams, and density, in grams per...
Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density. Format your output to two decimal places. ** Add Comments ** Print Name and Assignment on screen ** Date ** Submit .cpp file. Demo // This program uses a type cast to avoid an integer division. #include <iostream> // input - output stream #include <fstream> //working file...
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
How do i stop the loop if no next value in the list and prevent it...
How do i stop the loop if no next value in the list and prevent it from exit the program instead back to main menu. Let say i add food detail then i display it. It will display the detail but it will exit after it. struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; }; void Foodsystem::Place_Order(){ temp=new food; cout<<endl; cin.ignore(); cout<<"Enter your Name"; getline(cin,temp->name); cout<<"enter contact number::"; cin>>temp->contact_number; cin.ignore(); cout<<"enter address::"; getline(cin,temp->address); cout<<"customer_id::"; getline(cin,temp->customer_id);...
15.4 Zip code and population (class templates) Complete the TODOs in StatePair.h and main.cpp. StatePair.h Define...
15.4 Zip code and population (class templates) Complete the TODOs in StatePair.h and main.cpp. StatePair.h Define a class StatePair with two template types (T1 and T2), and constructor, mutators, accessors, and PrintInfo() member functions which will work correctly with main.cpp. Note, the three vectors (shown below) have been pre-filled with StatePair data in main() and do not require modification. vector<StatePair <int, string>> zipCodeState: ZIP code - state abbreviation pairs vector<StatePair<string, string>> abbrevState: state abbreviation - state name pairs vector<StatePair<string, int>>...
Write up to three lines to explain the logic used behind those codes.        1) #include <iostream>...
Write up to three lines to explain the logic used behind those codes.        1) #include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {         ifstream infile("worldpop.txt");         vector<pair<string, int>> population_directory;         string line;         while(getline(infile, line)){                 if(line.size()>0){                         stringstream ss(line);                         string country;                         int population;                         ss>>country;                         ss>>population;                         population_directory.push_back(make_pair(country, population));                 }         }         cout<<"Task 1"<<endl;         cout<<"Names of countries with population>=1000,000,000"<<endl;         for(int i=0;i<population_directory.size();i++){                 if(population_directory[i].second>=1000000000){                        ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT