I just learned c++ and is very hard for me to put in practice what I learned.
So, I would like to know if anybody could help me by giving me tasks and helping me to solve them.
Thanks

Read a file that contains text, in the form of words separated by whitespace (spaces, tabs, or newlines).

1) Print the longest and shortest words.
2) Print each word, together with how many times it occurs.
3) Print every word that occurs only once.

Ok
Sorry if i seem very newbie but some things just don't work for me.
When I use this code

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

int main () {
  ofstream myfile;
  myfile.open ("exercise.txt");
  system("pause");
  return 0;
}

in my opinion it should open the file but it does nothing.
I think that I just don't know how to tell the program where to look for it.

>>So, I would like to know if anybody could help me by giving me tasks and helping me to solve them

Here's a nice thread for you.

>> in my opinion it should open the file but it does nothing.

How do you know it "does nothing"? You're only opening the file, but not reading/writing so there's really no way to tell if it's doing anything :)

ok,
I made it like this but still does nothing.
I just don't know how the program is supposed to find the exercise.txt file

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

int main () {
  ofstream myfile;
  myfile.open ("exercise.txt");
   myfile << "Writing this to a file.\n";
  system("pause");
  return 0;
}

myfile is an ofstream, which means that you are intending to use it for output. Calling myfile.open("exercise.txt") says that you intend to write a file named exercise.txt -- which means that anything that was in that file already is about to vanish.

i made that file exercise.txt and I wrote in it " This the file I use for exercise."
I run the program and nothing happens.

So apparently your implementation doesn't do anything to an output file unless you try to write something.

I tried to write this

myfile << "Writing this to a file.\n";

But nothing happens

Edited 5 Years Ago by eduard77: n/a

Please show the entire program, and explain exactly what you did in order to run it.

It works fine over here, so I am thinking that something is odd about how you're running it.

I do not understand.
In my opinion my program does not even open the file because it doesn't know where to look for it.

I've found the file.
I was just not looking for it in the good location.
I move on now to try to print the longest and the shortest word.

I wrote this code first to see that I can read from a file.

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

int main () {
	string mystring;
  //ofstream myfile ("exercise.txt");
  ifstream infile("exercise.txt");
  if ( infile.is_open() )
  {
   //ifstream infile("exercise.txt"); 
   infile >> mystring;
   cout << mystring; 
  }
  else cout << "Unable to open file";

  //myfile.close();
  infile.close();
  cin.get();
  return 0;
}

but it shows me only the first character of the sentence

Edited 5 Years Ago by eduard77: n/a

Please show the full contents of your input file, as well as exactly what the program prints when you run it. Do not try to interpret or explain what the program does. Just the facts, please.

Edited 5 Years Ago by arkoenig: n/a

I wrote in the exercise.txt file "I have to find the longest and the shortest word from a sentence."
And the output is "I"

Your program reads and prints the first word in the input. The first word is "I". If that's what your program printed, it is working correctly.

Yes.
I tried to move on to read the longest and the shortest word from the sentence but it just not working

#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 don't know what am I suppose to do next to get the shortest and the longes word

If your program reads only the first word, it is impossible to get the shortest and longest word, because you have no way of knowing what those words are.

But this line
"infile >> mystring;"
was supposed to read the whole string

But this line
"infile >> mystring;"
was supposed to read the whole string

No -- when you use >> to read into a string, it reads one word.

So, what should I use instead?

You should arrange for your program to use >> as many times as necessary to read each word, so that you can see which is the longest and which is the shortest.

I've found another way to open the the file and now I can read the whole string.
The problem is now that i don't know how to read only the words from the sentence.

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

int main(int argc, char **argv){

    ifstream ifs("exercise.txt");
    if (!ifs.bad())
    {
        // u try to see if it reads from the file
        cout << ifs.rdbuf();
	
        ifs.close();
		cin.get();
    }
}

Edited 5 Years Ago by eduard77: n/a

You've used vectors in your other threads. Think about things you could do towards this problem with a vector of strings (or even an array of strings, if you wanted to practice those).

This question has already been answered. Start a new discussion instead.