Need help with a homework assignment

Pages: 12
Hi everyone. I have a homework assignment that requires me to prompt the user to enter the name of a file, and then the output will tell you how many words are in that file. Full disclosure, I'm still new, and often I have no idea how to approach building certain programs. I've researched different was of doing this, but now I'm thoroughly confused. I've tried to get the code below to work, but to little success. When I type in a file name, it doesn't appear to create one, nor does it count the number of words entered. I was a little skeptical of the
while (!in_file.eof()), which to my knowledge says run while the file has not ended, but at first things seemed to run fine. Now it doesn't, and the program returns nothing after I type in a file name. I'm super confused.

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
35
36
37
38
39
40
41
42
43
44
#include <iostream> 
#include <fstream>
#include <cstdlib>

//using namespace std;

int main()
{
	#include <iostream> 
#include <fstream>
#include <cstdlib>

 

using namespace std; 

int main()
{
	char fileName [20]; 
	ifstream in_file; 
	char n; // number of words in a file 
	int counter = 0; 
	
	cout << "Enter a file name: ";
	cin >> fileName;  
	cin.get(fileName,'\n'); 

	in_file.open(fileName);
	
	while (!in_file.eof())
	{
		in_file >> n; 
		counter++; 
	}
	
	cout << "The number of words int he file is " << counter; 

	in_file.close(); 
        
	

}
	
}
closed account (48T7M4Gy)
For a start, delete lines 26, 9, 10 & 11 and in-comment line 5

Also in line 32 you have a problem. You're reading in a character. This has nothing to do with the number of words.
I added the code below. The lines you suggested to omit are taken out (they looked like silly mistakes I forgot to check). I also changed variable "n" to a type string. I think I got that mixed up with fileName, which to my understanding, can be taken in from standard input as long as they are character types. Clearly I still have some gaping problems, because I'm still not getting anything. Working on it though.


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
char fileName[20];
ifstream in_file;
string n; // number of words in a file
int counter = 0;

cout << "Enter a file name: ";
cin >> fileName;


in_file.open(fileName);

while (!in_file.eof())
{
in_file >> n;
counter++;
}

cout << "The number of words int he file is " << counter;

in_file.close();





}
closed account (48T7M4Gy)
http://www.cplusplus.com/doc/tutorial/files/

You can always use the tutorials here. They have sample programs that often are extremely useful. Remember that just reading the words in and displaying them is the first step to greatness.
Thanks for the tutorials, they help make me make a little more sense of the basics. Trouble is, I am have a hard time applying them. I've researched ways of counting characters in a string, and those aren't too bad. But I have no idea how to count words in a file, and I can't get anything to work. I thought (and according to my text) that in order to read the name of a file that the file name had to be characters, but somehow they're strings? It's not making sense. How can something like char filename [20] be a string, when it's declared type char? I'm completely at a loss, and I have no idea how to solve this.
closed account (48T7M4Gy)
How can something like char filename [20] be a string, when it's declared type char?

c-strings are discussed at http://www.cplusplus.com/doc/tutorial/ntcs/

What is your program doing/not doing?



closed account (48T7M4Gy)
Hi lazy,

I just tried out your program on my machine and it works perfectly. I then added a word to my sample txt file and, sure enough the extra word was counted.

If yours is not working then maybe it is because you need to type in the whole text file path or , better still, put the text file in the same directory as the exe file.
Okay, here is what it is doing on my end. I use Visual Studio (not sure if that makes a difference), but when I run the code, it prompts me to enter a file name. When I do, I add something like "filename.txt", and then hit enter. After I hit enter, the command prompt stays open, and acts as if it is waiting for more input, but I can't type anything. I tried creating a new project, and saving that into a different folder, but that doesn't seem to solve anything either. My next step is to try a different IDE.
Try this:
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
35
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
    char fileName[20];
    ifstream in_file;
    string n; // number of words in a file
    int counter = 0;

    cout << "Enter a file name: ";
    cin >> fileName;


    in_file.open(fileName);


    if (in_file.is_open())
    {
        while (!in_file.eof())
        {
            in_file >> n;
            counter++;
        }

        cout << "The number of words int he file is " << counter;

    }
    else
        cout << "Unable to open file \"" << fileName << "\"\n";
}


My guess is that the file is not able to be opened, and since eof is never read from the file your loop that terminates after eof is encountered will be infinite.
closed account (48T7M4Gy)
I'm using VS2015 so we should be getting the same result.

I put your code via cut and paste into a source file I have set up as a dummy console project. Put the textfile in the debug directory and run project1.exe

I'd try that before changing IDE's because you might have the same problem there.

I know it doesn't help but believe me, your code works perfectly.


z:\k\Programs\Project1\Debug>
z:\k\Programs\Project1\Debug>project1
Enter a file name: sample.txt
The number of words int he file is 798
z:\k\Programs\Project1\Debug>project1
Enter a file name: sample.txt
The number of words int he file is 799
z:\k\Programs\Project1\Debug>
Last edited on
closed account (48T7M4Gy)
I also noticed in your earlier post. You just type the filename in you don't need quotes. That's why your program is hanging.
Last edited on
Hi cire,
I tried your suggestion, but it will not let me open the file. This is really weird.
I tried your suggestion, but it will not let me open the file. This is really weird.

Not so weird. That was the reason for the infinite loop. By default, if you're running from within the IDE, VS makes the "current" directory the one which contains your source code and not the directory containing the executable. How are you running the program and where is the file you're trying to open?
Okay, I'm probably not going to be much help here but I'll do the best I can. I run the code through VS, and I right click on the source file tab and click "Open folder containing file". That's how I've been able to view other .txt files I created with other projects, so I assume that this is the default directory. So I guess I do not know where the executable is being saved. The program also doesn't appear to even make a file name either, despite running okay according to kemort, as stated above.
closed account (48T7M4Gy)
lazy,

Let's do it in easy steps:
Go to the main directory where the Project.sln file is
closed account (48T7M4Gy)
When you get there, there will be a Debug directory and a Project1 directory (Project1 is the name of the overall project, you may have chosen a different name. But go to the Debug directory and open it.
closed account (48T7M4Gy)
You will see an .exe file and that is the same place you copy your text file into.
That's how I've been able to view other .txt files I created with other projects, so I assume that this is the default directory.

That is the default directory. Does the file exist in that directory or does "The program also doesn't appear to even make a file name either" indicate that the file doesn't exist? If you open a file for input and it doesn't exist, the open will fail. If you open a file for output and the file doesn't exist, it creates the file if it is able to and the open succeeds.
I appreciate it.

Okay, I see my project file.
closed account (48T7M4Gy)
Now open the command line and run the exe file. eg type project1 at the prompt

Then type in the file name without quotes
Pages: 12