fstream program help

Thread Solved

Join Date: Nov 2008
Posts: 20
Reputation: jimbob90 is an unknown quantity at this point 
Solved Threads: 0
jimbob90 jimbob90 is offline Offline
Newbie Poster

fstream program help

 
0
  #1
Dec 14th, 2008
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)

  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 5:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 680
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 47
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: fstream program help

 
0
  #2
Dec 14th, 2008
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
  1. while (!a.eof()){
  2. getline(cin, line);

you can simply do
  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 20
Reputation: jimbob90 is an unknown quantity at this point 
Solved Threads: 0
jimbob90 jimbob90 is offline Offline
Newbie Poster

Re: fstream program help

 
0
  #3
Dec 14th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: fstream program help

 
0
  #4
Dec 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 20
Reputation: jimbob90 is an unknown quantity at this point 
Solved Threads: 0
jimbob90 jimbob90 is offline Offline
Newbie Poster

Re: fstream program help

 
0
  #5
Dec 14th, 2008
  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)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 20
Reputation: jimbob90 is an unknown quantity at this point 
Solved Threads: 0
jimbob90 jimbob90 is offline Offline
Newbie Poster

Re: fstream program help

 
0
  #6
Dec 14th, 2008
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 6:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: fstream program help

 
0
  #7
Dec 14th, 2008
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:
printed out what I typed in, lovely
that has to do with minor problem #1
Last edited by mrboolf; Dec 14th, 2008 at 6:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 21
Reputation: deepglue555 is an unknown quantity at this point 
Solved Threads: 0
deepglue555 deepglue555 is offline Offline
Newbie Poster

Re: fstream program help

 
0
  #8
Jan 14th, 2009
Originally Posted by mrboolf View Post
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?????
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 937 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC