I found an exercise on the internet and decided to give a try.
The exercise is this:
"Write a modular program in C++ that search in a file strings with the presence of a word previously given by input, counting how many times the word has been found in the file as you wrote it.
Example:
Input
Path: C:/etc/etc/file.txt
Word: apple
Output
The word has been found (numbers of words found) times."
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string.h>
usingnamespace std;
int main()
{
int countwords=0;
string path;
string line;
string word;
cout<<"Write the path of the file."<<endl;
cin>>path;
ifstream File(path);
if(File.is_open())
{
while(getline(File,line))
{
cout<<line<<endl;
}
cout<<"Write the word you're searching for."<<endl;
cin>>word;
while(!File.eof())
{
if(word.compare(line)==0)
countwords++;
}
cout<<"The word has been found "<<countwords<<" times."<<endl;
File.close();
}
else
{
cout<<"Error! File not found!";
exit(1);
}
}
The first error I found is in "ifstream File(path)" and I don't get what is wrong there. If I write directly "file.txt" it gives me no error. Why?
The second error is when I start the program and he doesn't count the number of times the word has been found. What's the problem there? It's like he jump the "while".
I know it has to be a modular program but for now I want to try it without functions.
Can someone help me?
I am not saying you are wrong by any means, I am confused now though. I made a test file and was not able to open a file without using File.open(path). It might not have contributed to his error like I thought, but you still need to open the file somewhere don't you?
I am not saying you are wrong by any means, I am confused now though. I made a test file and was not able to open a file without using File.open(path).
Feeding the path to the constructor is the same as not feeding the path to the constructor and then feeding the path to the open method.
Your code is faulty. In line 9 you feed inFile's constructor an empty string as the path, then you ask the user for the name of the path. You would not use the constructor invoked on line 9 with the open method invoked on line 14.
Okay, sorry but I've been a lot busy these days.
cire, I tried ifstream file(path.c_str()) and worked, thanks!
However one problem still remain. The word I search.
When I write the word I'm searching for the program blocks and I can't write anymore(it doesn't crash, it just blocks with the underline).
1 2 3 4 5 6
Write the path of the file.
file.txt
File found.
Write the word you're searching for.
apple
_
That is performed once, above. You need to stick it at the end of the while loop as well or you're not extracting from the file any more (and the condition !File.eof() never changes).
@ Absolute 88: Don't do that either, I get corrected on what feels like a daily bases on this site. This is how we correct misunderstandings.
@ OP: Using "std::istream.get()" without parameters only extracts one character at a time and the set_equal operator overwrites, it does not append. You want one of the overloads that takes the char_type and stream size as parameters if you are hoping to compare the data you are reading to anything more then a single character: http://www.cplusplus.com/reference/istream/basic_istream/get/
file >> candidate attempts to read characters into the string. If no characters could be read, input fails, and the stream is put into a failed state. The result of file >> candidate is a reference to the input stream.
while( file >> candidate )
while after file >> candidate, the stream is not in a failed state