Using Getline

Hello,

I am currently a beginner in C++; I'm working on a small task to write a program that prompts the user for a last name then searches a file named book.data and prints out the phone numbers of all entries with that last name. I have made the text file already, so my problem is that I cannot get the program to search the file the entries with the specific letter pertaining to the users input.

Here's what I have:

1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 using namespace std;
5
6 int main () {
7
8 string phone_book;
9 string lastName; // users last name
10 ifstream myfile ("book.data.txt");
11
12 cout << endl << "Enter your last Name" << endl;
13 cin >> lastName;
14
15 if (myfile.is_open()) {
16
17 while(myfile.good()){
18
19 getline(myfile, lastName); // reading book.data
20 cout << lastName << endl;
21 }
22
23 myfile.close();
24
25 }
26
27 else cout << endl << "Unable to open Phone Book" << endl;
28
29
30 return 0;
31 }


My goal is to print out all the last names that the user inputed. For example the letter "G", my program doesn't print out a list of last names with specifically with the letter G, but rather all the last names.
Last edited on
i am pretty new at this too but you need to get the user input and assign it to a var then do a loop to compare it to your data, them if name in == data then cout your data file. you can use string stream for input and convert to a char array to capture the first letter. let me know if that is more confusing or helpful.


You don't give details of the contents of book.data.txt. For example, I assume it contains more than just a the lastname on each line.

Meanwhile I'd recommend you change this:
17
18
19
20
21
    while(myfile.good()){

        getline(myfile, lastName); // reading book.data
        cout << lastName << endl;
    }


to this:
17
18
19
    while(getline(myfile, lastName)) {    // reading book.data
        cout << lastName << endl;
    }
Dear Chervil, I've made the changes, thank you! My text file book.data currently contains information such as last name, first name, followed by address & tel. per line, it's a dummy phone book.

-Lazysquirell, I think I understand..you're suggesting to create an array?
If the first item on each row is the last name, you might try this:
1
2
3
4
5
6
    string line;

    while(getline(myfile, line)) {    // reading book.data
        if (line.find(lastName) == 0)
            cout << line<< endl;
    }


I've not tested this, so there may be mistakes. It tries to search each line for the name entered by the user. If it is found starting at the first character position, display that line.

You may also find the ideas in this thread of interest:
http://www.cplusplus.com/forum/beginner/93747/#msg503264
Last edited on
#include <iostream>
#include <string>


int main()
{
std::string name;
std::cout<<"please enter your full name:";

std::getline(std::cin,name);
std::cout<<"Hellow,"<<name<<"!\n";

system("pause")
return 0;
}
never replied back, I resolved my issue thanks to all who helped in this forum! :)

Here's my result:

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
36
37
38
39
40
#include <iostream>
#include <string>
using namespace std;

int main () {

cout << string (50, '\n'); // clear screen with blank spaces
string lastName;
string firstName;
string address;
string city;
string state;
signed int zip;

	cout << endl << "Enter your first name" << endl;
		cin >> firstName;
		cin.ignore(); // this ignores the cin>> ability to stop immediately.
	cout << endl << "Enter your last name" << endl;
		cin >> lastName;
		cin.ignore();
	cout << endl << "Enter your address" << endl;
		getline(cin, address);

	cout << endl << "Enter your city" << endl;
		cin >> city;
		cin.ignore();
	cout << endl << "Enter your zip" << endl;
		cin >> zip;
		cin.ignore();
	cout << endl << "Enter your State: " << endl;
		cin >> state;
		cin.ignore();
	cout << endl << "Your Shipping Label is : " << endl << firstName << " " << lastName << '\n';  
	cout << address << "." << '\n';
	cout << city << ", " << state << " " << zip << endl;	



return 0;
}
Topic archived. No new replies allowed.