Can anyone help with this issue?

How could I apply input >> ws into my program?
The program works perfect but I wanted it to display like this EX:
Jake Miller,712-9881
How could I go about this?
Thanks for the help guys!

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Abstract: This program starts with reading input file which holds people’s names and phone numbers and then save this input data to array.
The program should ask the user to enter a name or partial name to search for.
Any entries in the array that match the string entered should be displayed.
*/

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

string name1;
int x = 0;
const int SIZE = 48;
string data[SIZE];
string searchN;

string arr(string);

//Only part that is missing is input >> ws;
//how to apply that in my situation?

int main()
{
    ifstream input;
    input.open("clientInfo.txt");

    while(!input.eof()){
        getline(input,data[x]);
        x++;
        }
        arr(searchN);

    input.close();
    return 0;
}

string arr(string searchN){

    int dataFound = 0;
    cout << "Enter a name you would like to search for: ";
    getline(cin,searchN);

    cout << "Results of your search:" << endl;

    for(int i = 0; i < x; i++){
        if(data[i].find(searchN.data(), 0) < data[i].length()){
            dataFound++;
            cout << data[i] << endl;
        }
    }
    if(dataFound == 0){
        cout << "Data entered not found";
    }
    return searchN;
}
Last edited on
How could I apply input >> ws into my program?
What is wrong? Why do you think you need input >> ws?

By the way: Your loop should look like this:
1
2
3
    while(getline(input,data[x])){
        x++;
        }
Otherwise you read too far:

line 30: read the eof -> line 31 [regardless]: x++ -> line 29: check for eof -> end loop
coder777 the program runs perfect the way it is, the issue I'm having is I need to somehow have the formatting like this:
Jake Miller,712-9881

How to go about this?
closed account (48T7M4Gy)
So what is the structure of the data that goes into the file, out from it and that in data[i] for instance.
The structure of the output of this program looks like this right now:

Enter a name you would like to search for: Li
Results of your search:
Li Chen 555-1212

Process returned 0 (0x0) execution time : 3.126 s
Press any key to continue.


The txt structure looks like this:

Alejandra Cruz 555-1223
Joe Looney 555-0097
Geri Palmer 555-8787
Li Chen 555-1212
Holly Gaddis 555-8878
Sam Wiggins 555-0998
Bob Kain 555-8712
Tim Haynes 555-7676
Warren Gaddis 555-9037
Jean James 555-4939
Ron Palmer 555-2783


Any suggestions?






closed account (48T7M4Gy)
So, am I to assume that you want
Tim Haynes 555-7676
which is a single string to appear as
Tim Haynes, 555-7676
?
Correct, I have been working on this for the past 3hrs. Still no luck.
closed account (48T7M4Gy)
So, you have to analyse the string.

Either you search the string for the second blank space or perhaps the first number, or the number is always 8 characters, and insert a comma with a space. Only you know which is the better alternative, assuming they are the only possibilities.

Check out the string functions
http://www.cplusplus.com/reference/string/string/
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main()
{
  std::string name = "Tim Haynes 555-7676";
  int position = name.length() - 5;
  
  name.insert(position, "Hint:");
  
  std::cout << name << std::endl;
}
I thought about taking that route of insert BUT the issue with the code above is it has to come from the txt file.
closed account (48T7M4Gy)
I thought about taking that route of insert BUT the issue with the code above is it has to come from the txt file.

It's not magic, and unless I misunderstand you, there is only one way to change the string, and that is by one of the various alternatives before or after the data string goes to or from the text file respectively.
What you can do to keep the data is to build a structure of this form:

1
2
3
4
struct Person
{
    string name, surname, tel;
};


And then make an array of Person, such as Person data[SIZE];.

Then you can input the data from the txt in this manner:

1
2
3
4
5
6
7
8
ifstream file;
file.open("data.txt");
int i = 0;
while(!file.eof())
{
    file >> data[i].name >> data[i].surname >> data[i].tel;
    i++;
}


and you're fine, as long as you know the exact size of the .txt file.

Storing the data in three different strings enables you to do this:

1
2
3
4
for(int i=0; i<SIZE;i++)
{
    cout << data[i].name << " " << data[i].surname << ", " << data[i].tel << endl;
}


It also makes searching easier, provided that you use the string library.

You can search like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void search(Person data[], int size)
{
    string k_name, k_surname;
    int flag = 0, i=0;

    cout << "Please enter surname: ";
    cin >> k_surname;
    cout << "Please enter name: ";
    cin >> k_name;
    do {
         if(k_name.equals(data[i].name) && k_surname.equals(data[i].surname))
        {
            //results
            flag++;
        }
        i++;
    }while(!flag && i==size);
}
Last edited on
Topic archived. No new replies allowed.