sorting

I try to find the shortest and the longest word from a sentence that i read from a file.
I just need some idea how to do this.
Please do not put any code just ideas.
Well how I would do it is first open the file.
Read in word by word and store the longest and shortest based on size().

So pseudo-code:
1
2
3
4
5
6
7
open(file);
read(file) -> variable;
check( variable.size() )
if > than largest_word_variable.size(), replace largest_word_variable
if < than smallest_word_variable.size(), replace smallest_word_variable
finished ? (end loop) : (goto read); 
print largest and smallest word.
I do not understand the part with the variable.
I open the file " ofstream myfile ("exercise.txt");
I read a file with fgets or fread?
And i put what i read in a variable?
Use ifstream from the std library fstream header.

ifstream infile ("file_with_sentence.txt");
then read in word by word:
infile >> variable;

the variable should be a std::string from the string header.
I wrote this and I receive a lot of errors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
	string mystring;
  ofstream myfile ("exercise.txt");
  if (myfile.is_open())
  {
   ifstream infile("exercise.txt");
   infile>>mystring;
   cout>>mystring;
  }

  else cout << "Unable to open file";
  myfile.close();
  cin.get();
  return 0;
}
Create the "exercise.txt" beforehand in the directory where the executable goes when compiled.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
	string mystring;
  //ofstream myfile ("exercise.txt");
  ifstream infile("exercise.txt");
  // You should create the file in the directory with the executable before hand.
  if ( infile.is_good() )
  {
   //ifstream infile("exercise.txt"); // Don't open inside a block. Will dissapear at its end.
   infile >> mystring;
   cout >> mystring; // cout uses <<, cin uses >>
  }
  else cout << "Unable to open file";

  //myfile.close();
  infile.close();
  cin.get();
  return 0;
}
I modified your code a little and now is compiling ok but I reads me only a strange character from my file.
The sentence is" I have to find the longest and the shortest word from a sentence."
and the output shows me only the first character" I."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
	string mystring;
  //ofstream myfile ("exercise.txt");
  ifstream infile("exercise.txt");
  // You should create the file in the directory with the executable before hand.
  if ( infile.is_open() )
  {
   //ifstream infile("exercise.txt"); // Don't open inside a block. Will dissapear at its end.
   infile >> mystring;
   cout << mystring; // cout uses <<, cin uses >>
  }
  else cout << "Unable to open file";
  //myfile.close();
  infile.close();
  cin.get();
  return 0;
}
 infile.is_good() wrutes me that is not valid soI had to replace it.

Well now you need to loop the reading of the file.

1
2
3
4
while ( infile >> mystring )
{
   // Compare, cout, or anything. This loop will run until it reads every word of the file.
}
Last edited on
I try to do this
for( int i=0; i<mystring.size(); i++) {

}
but I don't know how to continue
That for loop only loops for the size of the string you read in. You're going to need to loop reading the file, not the string.

The string will then need to be compared to other strings for size.

1
2
if ( mystring.length() < shortestword.length() )
  shortestword = mystring;
I do not understand how I declare the variable shortestword and how doI use it
1
2
  string mystring, longstr = "", shortstr = "";
  // All of these variables are of the type string. 
It is not ok
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
	string mystring;
  //ofstream myfile ("exercise.txt");
  ifstream infile("exercise.txt");
  // You should create the file in the directory with the executable before hand.
  if ( infile.is_open() )
  {
   //ifstream infile("exercise.txt"); // Don't open inside a block. Will dissapear at its end.
   infile >> mystring;
   cout << mystring; // cout uses <<, cin uses >>
  }
  else cout << "Unable to open file";

  //myfile.close();
 string longstr = "";
 string shortstr = "" ;
  for( int i=0; i<mystring.size(); i++) {
	  if(mystring.length()> longstr.length(){
		  mystring=longstr;
	  }
      if(mystring.length()< shortstr.length(){
		  mystring=shortstr;
  }
	  cout<<longstr<<endl;
	  cout<<shortstr<<endl;
  infile.close();
  cin.get();
  return 0;
}

I just can't get it
Topic archived. No new replies allowed.