943,862 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1247
  • C++ RSS
Dec 14th, 2008
0

fstream program help

Expand Post »
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)

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void add(string);
  8. void lookup(string);
  9.  
  10. int main(){
  11. ifstream a;
  12. string name, choice, name2, email, longstring;
  13. cin >> choice;
  14. if (choice == "lookup"){
  15. cin >> name;
  16. lookup(name);
  17. }
  18. else {
  19. cin >> name >> name2 >> email;
  20. longstring = name + " " + name2 + " " + email;
  21. add(longstring);
  22. }
  23.  
  24. return 0;
  25. }
  26. void add(string longstring){
  27. ofstream a;
  28. a.open("phonedir");
  29. a << longstring;
  30. }
  31. void lookup (string name){
  32. ifstream a;
  33. string line;
  34. a.open("phonedir");
  35. while (!a.eof()){
  36. getline(cin, line);
  37. if (line.find(name))
  38. cout << line;
  39. else
  40. "";
  41. }
  42. }
Last edited by jimbob90; Dec 14th, 2008 at 6:54 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
jimbob90 is offline Offline
25 posts
since Nov 2008
Dec 14th, 2008
0

Re: fstream program help

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
C++ Syntax (Toggle Plain Text)
  1. while (!a.eof()){
  2. getline(cin, line);

you can simply do
C++ Syntax (Toggle Plain Text)
  1. 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
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Dec 14th, 2008
0

Re: fstream program help

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
Reputation Points: 10
Solved Threads: 0
Light Poster
jimbob90 is offline Offline
25 posts
since Nov 2008
Dec 14th, 2008
0

Re: fstream program help

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 7:18 pm.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 14th, 2008
0

Re: fstream program help

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void add(string);
  8. void lookup(string);
  9.  
  10. int main() {
  11. string name, choice, name2, email, longstring;
  12. cin >> choice;
  13. if (choice == "lookup"){
  14. cin >> name;
  15. lookup(name);
  16. }
  17. else {
  18. cin >> name >> name2 >> email;
  19. longstring = name + " " + name2 + " " + email;
  20. add(longstring);
  21. }
  22.  
  23. return 0;
  24. }
  25. void add(string longstring){
  26. ofstream a;
  27. a.open("phonedir");
  28. add(longstring);
  29. a.close("phonedir");
  30. }
  31.  
  32. void lookup (string name){
  33. ifstream a;
  34. string line;
  35. a.open("phonedir");
  36. while (getline(cin, line)){
  37. if (line.find(name))
  38. cout << line;
  39. else
  40. "";
  41. }
  42. a.close("phonedir");
  43. }
when i close i get this weird error

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)
Reputation Points: 10
Solved Threads: 0
Light Poster
jimbob90 is offline Offline
25 posts
since Nov 2008
Dec 14th, 2008
0

Re: fstream program help

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)
Last edited by jimbob90; Dec 14th, 2008 at 7:39 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
jimbob90 is offline Offline
25 posts
since Nov 2008
Dec 14th, 2008
0

Re: fstream program help

What prevents your program from compiling is the following:

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:
Quote ...
printed out what I typed in, lovely
that has to do with minor problem #1
Last edited by mrboolf; Dec 14th, 2008 at 7:46 pm.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Jan 14th, 2009
0

Re: fstream program help

Click to Expand / Collapse  Quote originally posted by mrboolf ...
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.

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?????
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deepglue555 is offline Offline
21 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Ifstream - Change font
Next Thread in C++ Forum Timeline: Cant solve this





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC