Hello, So this is part of my homework. I have been stuck on this code for hours now. I feel like I've tried everything or that I am just missing something super simple. Every time I run the code it only shows white space. I'm not sure where I have gone wrong. I've tried error checking parts of the code, but I can't figure it out. Please help.
I feel like I messed up somewhere. The second part is me just trying to check what is going wrong but I can't find it.
#include<iostream>
#include<string>
#include<fstream>
#include <algorithm>
usingnamespace std;
int main ()
{
ifstream inputfile;
string filename;
constint numstudents = 25;
string name[numstudents];
int y = 0;
//get filename
cout << "Hello, Please enter the file name you would like to use. " << endl;
cin >> filename;
// open file name
inputfile.open(filename.c_str());
// read filename into array
if(inputfile)
{
while(true)
{
inputfile >> name[y];
y++;
if(inputfile.eof())
break;
}
}
//sort and output in alphabetical order.
for(int i=0; i< numstudents; i++)
cout << "Here are all the students names in alphabetical order: " << endl;
sort(name, name + numstudents);
for (int i = 0; i < numstudents; i++)
{
cout << name[i] << endl;
}
return 0;
}
#include<iostream>
#include<string>
#include<fstream>
#include <algorithm>
using namespace std;
int main ()
{
ifstream inputfile;
string filename;
const int numstudents = 25;
string name[numstudents];
cout << "Hello, Please enter the file name you would like to use. " << endl;
cin >> filename;
inputfile.open(filename.c_str());
int y = 0;
if(inputfile)
{
while(true)
{
inputfile >> name[y];
y++;
cout << name[y] << endl;
if(inputfile.eof())
break;
}
}
return 0;
}
Since you're trying to sort your whole array when you probably only have a few entries is probably part of the problem. You should only be sorting for the actual number of elements retrieved from the file, not the whole array.
By the way your input loop is probably incorrect, it should probably be more like: