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)

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

using namespace std;

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;
}

return 0;
}
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
"";
}
}
4
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by deepglue555

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

while (!a.eof()){
getline(cin, line);

you can simply do

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

not realy sure what you meanwith add().
file:
smith joe jsmith@gmail.com
harper stephen sharper@pm.gov.ca
rococco rocky [email]rocky@.fsign.com[/email]
output:
lookup rockylookup fred

if i find the string i need i print out the whole line that is in the file

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.

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

using namespace std;

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;
}

return 0;
}
ofstream a;
a.open("phonedir");
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");
}

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)

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 [email]rocky@.fsign.com[/email]

output

lookup rockylookup fred (printed out what I typed in, lovely)

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)

EDIT:

printed out what I typed in, lovely

that has to do with minor problem #1

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?????