| | |
fstream program help
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 20
Reputation:
Solved Threads: 0
program read from cin what the user wants to do ("add" a name to list) or ("lookup" a name). in add just add whatever was read in to the file. in lookup print out a line of text with the string that wa read in. hopefully that makes some sense =/
im pretty sure that the problem is in one of the loops of lookup function. (while or if else)
im pretty sure that the problem is in one of the loops of lookup function. (while or if else)
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; void add(string); void lookup(string); int main(){ ifstream a; string name, choice, name2, email, longstring; cin >> choice; if (choice == "lookup"){ cin >> name; lookup(name); } else { cin >> name >> name2 >> email; longstring = name + " " + name2 + " " + email; add(longstring); } return 0; } void add(string longstring){ ofstream a; a.open("phonedir"); a << longstring; } void lookup (string name){ ifstream a; string line; a.open("phonedir"); while (!a.eof()){ getline(cin, line); if (line.find(name)) cout << line; else ""; } }
Last edited by jimbob90; Dec 14th, 2008 at 5:54 pm.
•
•
Join Date: Feb 2008
Posts: 680
Reputation:
Solved Threads: 47
1) make sure you open the file in append mode in the add() function (i'm not sure what the default mode is)
2)there is no need to do
you can simply do
If you don't tell us what's wrong, we can tell you how to fix it! (unless we compile it ourselves - which you're supposed to do for us! haha)
Dave
2)there is no need to do
C++ Syntax (Toggle Plain Text)
while (!a.eof()){ getline(cin, line);
you can simply do
C++ Syntax (Toggle Plain Text)
while(getline(cin,line))
If you don't tell us what's wrong, we can tell you how to fix it! (unless we compile it ourselves - which you're supposed to do for us! haha)
Dave
•
•
Join Date: Nov 2008
Posts: 20
Reputation:
Solved Threads: 0
not realy sure what you meanwith add().
file:
smith joe jsmith@gmail.com
harper stephen sharper@pm.gov.ca
rococco rocky rocky@.fsign.com
output:
lookup rockylookup fred
if i find the string i need i print out the whole line that is in the file
file:
smith joe jsmith@gmail.com
harper stephen sharper@pm.gov.ca
rococco rocky rocky@.fsign.com
output:
lookup rockylookup fred
if i find the string i need i print out the whole line that is in the file
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
getline(cin, line); Shouldn't you take your line from the ifstream? What daviddoria meant with the add function is that you are every time rewriting the file instead of just adding lines.
Also you are not closing anything.
Edit: You declare
ifstream a; in main but you make no use of it. Last edited by mrboolf; Dec 14th, 2008 at 6:18 pm.
•
•
Join Date: Nov 2008
Posts: 20
Reputation:
Solved Threads: 0
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; void add(string); void lookup(string); int main() { string name, choice, name2, email, longstring; cin >> choice; if (choice == "lookup"){ cin >> name; lookup(name); } else { cin >> name >> name2 >> email; longstring = name + " " + name2 + " " + email; add(longstring); } return 0; } void add(string longstring){ ofstream a; a.open("phonedir"); add(longstring); a.close("phonedir"); } void lookup (string name){ ifstream a; string line; a.open("phonedir"); while (getline(cin, line)){ if (line.find(name)) cout << line; else ""; } a.close("phonedir"); }
29 no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::close(const char[9])'
note C:\Dev-Cpp\include\c++\3.4.2\fstream:708 candidates are: void std::basic_ofstream<_CharT, _Traits>::close() [with _CharT = char, _Traits = std::char_traits<char>]
got both of those two times (2 close() functions)
•
•
Join Date: Nov 2008
Posts: 20
Reputation:
Solved Threads: 0
figured out my close doesnt work yay?
and still my lookup function doesnt work heres the input/output again
in the file
smith joe jsmith@gmail.com
harper stephen sharper@pm.gov.ca
rococco rocky rocky@.fsign.com
output
lookup rockylookup fred (printed out what I typed in, lovely)
and still my lookup function doesnt work heres the input/output again
in the file
smith joe jsmith@gmail.com
harper stephen sharper@pm.gov.ca
rococco rocky rocky@.fsign.com
output
lookup rockylookup fred (printed out what I typed in, lovely)
Last edited by jimbob90; Dec 14th, 2008 at 6:39 pm.
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
What prevents your program from compiling is the following:
1) close doesn't need arguments - change both
2)
Said that, your code presents at least one big problem and two minor ones.
The big one: you call add function recursively (why?) and you don't set a base case. This is bad. Delete that recursive
The "minor" ones:
1)
2) You still probably need to open your ofstream in append mode in your add function.
EDIT: that has to do with minor problem #1
1) close doesn't need arguments - change both
a.close("phonedir"); with a.close(); .2)
else "" ; doesn't make sense. Maybe you wanted to write else cout << ""; .Said that, your code presents at least one big problem and two minor ones.
The big one: you call add function recursively (why?) and you don't set a base case. This is bad. Delete that recursive
add(longstring); line and put ther a simpler and healtier a << longstring; The "minor" ones:
1)
getline(cin, line); would likely be getline(a, line); (as you are searching a)2) You still probably need to open your ofstream in append mode in your add function.
EDIT:
•
•
•
•
printed out what I typed in, lovely
Last edited by mrboolf; Dec 14th, 2008 at 6:46 pm.
•
•
Join Date: Jan 2009
Posts: 21
Reputation:
Solved Threads: 0
•
•
•
•
getline(cin, line);Shouldn't you take your line from the ifstream?
What daviddoria meant with the add function is that you are every time rewriting the file instead of just adding lines.
Also you are not closing anything.
Edit: You declareifstream a;in main but you make no use of it.
what is the longest string that can come from the console into 'line'
I wrote a program that is crashing because it is recieving more than 280 characters.
How can i increase the amount getline can take in or is there an alternative function which will handle greater strings from IO?????
![]() |
Similar Threads
- Help with program (C++)
- fstream Tutorial (C++)
- correct errors in this program (C)
- Beginner needs assistance with 'error checking' program (C++)
- Stack Queue Fstream (C++)
- basic fstream stuff (C++)
- help for program involving switch loops and file (C++)
Other Threads in the C++ Forum
- Previous Thread: Ifstream - Change font
- Next Thread: Cant solve this
Views: 937 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm array arrays assignment beginner binary browser c++ c/c++ calculator char class classes client code compile compiler constructor conversion convert count delete dll dynamic encryption error exception file files fstream function functions game givemetehcodez graph gui helpwithhomework homework iamthwee ifstream input int lazy link linked-list linker list loop looping loops math matrix member memory newbie number object objects opengl output parameter path pointer pointers problem program programming project python random read reading recursion recursive reference sockets sort spoonfeeding string strings struct student studio system template templates text time tree undefined url variable vc++ vector video visual win32 window windows winsock





