reading strings with ifstream

When reading strings from a file, what should the strings be initialized to if you want to find the greatest string value and the least-greatest string value. If initialized as "" the smallest value will not be read because "" ASCII value is lower than a letter character.

/*
int main()
{
string
name,
smallest_name = "",
greatest_name = "";

ifstream
inputFile;

inputFile.open("//Users//kevin//Downloads//source_code//Chapter 05//LineUp.txt");

while (inputFile >> name)
{
if (smallest_name > name)
{
smallest_name = name;
}
if (greatest_name < name)
{
greatest_name = name;
}
}

inputFile.close();
*/
Last edited on
Initialise them to the first thing you read from file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <fstream>
using namespace std;

int main()
{
	string smallest_name, greatest_name;

	ifstream inputFile("//Users//kevin//Downloads//source_code//Chapter 05//LineUp.txt");

	for (string name; inputFile >> name; ) {
		if (smallest_name.empty() || smallest_name > name)
			smallest_name = name;

		if (greatest_name.empty() || greatest_name < name)
			greatest_name = name;
	}
}

Thank you.
Thank you.
Topic archived. No new replies allowed.