Can someone tell me why my code isnt working

/////////////////////////////////////////////////////

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

using namespace std;

int search(ifstream &in, string str) {
string line;
if (getline(in, line)) {
if (line.find(str) != -1) {
cout << line << endl;
return 1 + search(in, str);
}
else {
return search(in, str);
}
}
else {
return 0;
}
}

int main()
{
string filename;
cout << "Enter file name: ";
cin >> filename;
ifstream in(filename.c_str());
if (in.is_open()) {
cout << "Enter string: ";
string str;
cin >> str;
cout << str << " is found in" << endl;
int count = search(in, str);
cout << str << " appeared in " << count << " lines." << endl;
in.close();
}
else {
cout << "can not open " << filename << " to read" << endl;
}
}

///////////////////////////////////////////

It displays correctly and everything but when I go and type "input.txt" into where it says "Enter file name:" it says that it "can not open input.txt to read" and I dont know how to fix it.

///////////////////////////////////////////

EDIT: Just realized that I wrote it so it says that it cant open the file... duhh
But I still don't know why it cant read it

///////////////////////////////////////////

UPDATE: I tried putting the file's path in instead of just the files name and it still doesn't work. I tried all of the variations of:
"C:\Documents\C++\Input.txt"
If you could please help me out with this I would greatly appreciate it.
Last edited on
Are you sure your program is looking for the file in the right directory?

What happens when you enter the complete path to the file, rather than just the filename?
uhhhhhh......

I honestly have no idea lmao. This was the answer provided to a c++ question on Chegg and it fit the assignments directions. so I have no idea if it is looking for the file in the right directory. (I am just tryna get an A in my C++ class lmao)

And umm.... what would the complete path to the file look like? I know what it is..... I just wanna see if you knew what it was..... yeah. lmao


This was the answer provided to a c++ question on Chegg
am just tryna get an A in my C++ class
what would the complete path to the file look like?


Today's students, eh?
I am just tryna get an A in my C++ class


Well that attempt won't do it.

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 <string>
#include <fstream>

using namespace std;

size_t search(ifstream& in, const string& str) {

	size_t cnt {};

	for (string line; getline(in, line); )
		cnt += line.find(str) != string::npos;

	return cnt;
}

int main()
{
	string filename;

	cout << "Enter file name: ";
	getline(cin, filename);

	ifstream in(filename);

	if (in.is_open()) {
		string str;

		cout << "Enter string: ";
		getline(cin, str);

		cout << str << " appeared in " << search(in, str) << " lines.\n";
	} else
		cout << "can not open " << filename << " to read\n";
}

@lastchance
hehehe.... well me and my family are all getting ready to pack up our entire lives and move to Florida in less than a month so this practice program isn't really at the top of my priority list, ya know?

@seeplus
the edited version you wrote has the same issue still but thank you for your attempt
try typing the file name with its path, eg C:\folder\deeper\filename
the computer works like the operating system --- what happens in a command console when you copy a file that isn't in the current folder? It says file not found. You either need to move to that folder or provide the path.
Visual studio is notorious for running the program you built from another path than where the code lives, if you start it from inside the tool.
ohhh ok Ill try that real quick and let you know

UPDATE:
it didnt work but I am not sure I did the path thingy right
I typed in:
C:\Documents\C++\Input.txt
Last edited on
If you need recursion, then a 'better' version consider:

1
2
3
4
5
6
7
size_t search(ifstream& in, const string& str)
{
	if (string line; getline(in, line))
		return (line.find(str) != string::npos) + search(in, str);

	return 0;
}


If you are running the program from within the tool, you need to know the full path name to the file. Can you find it/open it from within explorer? When you're found it, what does the top bar say?

PS Is it 'Documents and Settings' rather than 'Documents'?
Ok good news
Just realized there was a "copy path" button so I did that and it worked :)

Thank you for your help.

Also, was it wrong for me to use the lyrics for "Never Gonna Give You Up" by Rick Astley for the input text? lmaoooo
glad it worked out. Remember this one; its a common aggravation that you just need to know about and have that 2 second 'oh yeah' moment when you forget.

pay attention to the other posts and suggestions...
Seeplus, the folder name depends on which os version. They keep changing it up. Its documents on winx, its documents and settings on earlier versions but I forget when it changed.
Last edited on
Seeplus, the folder name depends on which os version. They keep changing it up. Its documents on winx, its documents and settings on earlier versions but I forget when it changed.


Yeah. I can't remember either what changed in which version!
The Documents folder is actually a sub-folder within the logged-in user folder on Win10, with a Windows managed link to make it appear as just "Documents". It was similar with WinXP, 7 and 8/8.1.

I log in as "Owner", so my documents folder is: C:\Users\Owner\Documents

There is also a Public user that has a documents folder as well, named Public Documents: C:\Users\Public\Documents

I seriously suggest you move all your programming work to a different folder, C:\Programming for example. That way you get around the barriers Windows can throw up when using system folders such as Documents.
Last edited on
Topic archived. No new replies allowed.