Outputting words with same information from a text file

Mar 29, 2018 at 5:34pm

I have to write a program where I input a word such as "BANANA" and it will read from a text file(this is a modified version of what i am actually working on) containing this

Text File:

BANANA TA GG GZ 95 5G
APPLE AF GC CF CG CA
ORANGE CG CG VG GL CD
GRAPE CG CG VG GL CD


In console i will input:

BANANA or banana

And i get:

Code: TA GG GZ 95 5G


But where I am stuck right now is when I input Orange I want to get

Code: TA GG GZ 95 5G
Similiar fruit: Grape

This is my code
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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <map>

using namespace std;

//main
int main()
{
   map<string,string> index;
   string fruit, code;

   // ifstream in( "data.txt" );
   ifstream in("fruit.txt");


   while( in >> fruit >> ws && getline( in, code ) ) index[fruit] = code;

   while ( true )
   {

    cin >> fruit;
    //converting to uppercase letter

    for(int i = 0; i < fruit.length(); i++){
            fruit[i] = toupper(fruit[i]);
        }

        if (index.count(fruit)){

            cout << "CODE: " << index[fruit] << '\n' << endl;

        break;

        }
        else{

            cout << "Not found\n" << endl;

        break;

        }
    }
}
Mar 29, 2018 at 6:15pm
@badatcoding67,
Why do you not continue with your existing thread:
http://www.cplusplus.com/forum/beginner/234068/#msg1051206
It is just confusing everyone.

Please close one of them down with a green tick.
Mar 29, 2018 at 6:18pm
Process stream as follows. Hilariously, while (iss >> fruit) instead of the for loop would duplicate the last line in stream.

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
#include <iostream>
//#include <fstream>
#include <sstream>
#include <string>
#include <map>

using namespace std;

//main
int main()
{
    const char* data_text = R"LITERAL(
BANANA TA GG GZ 95 5G
APPLE AF GC CF CG CA
ORANGE CG CG VG GL CD
GRAPE CG CG VG GL CD
)LITERAL";

    // Imitate file stream
    istringstream iss(data_text);
    
    map<string,string> index;
    string fruit;
    string rest;
    
    for ( ; iss>>fruit; )
    {
        getline(iss, rest);
        
        cout << "found fruit \""<<fruit << "\" and rest of string is \"" << rest << "\".\n";
    }

    return 0;
}


In action: https://repl.it/repls/UglyUniformDifference
Last edited on Mar 29, 2018 at 6:29pm
Mar 29, 2018 at 6:33pm
I just realized you purposely want two-way lookup. Do you have access to boost (bi-directional map)? Or you could make custom class that links map<a,b> with a map<b,a>.

Edit: nevermind, since the reverse would make it not have unique keys. Not sure which data structure to use for the reverse... How many items are we talking about?
Last edited on Mar 29, 2018 at 6:47pm
Mar 29, 2018 at 7:39pm
should be fully working now at the same link, https://repl.it/repls/UglyUniformDifference .

Uses a map<string,string> to store fruit to the "rest" (code).
Uses a map<string,int> to track frequency of "rest". Only if search term's "rest" has turned up more than once, then a full pass of the original map is done to spit out all similar fruits.

Edit: cleaned up a bit.

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
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <map>

using namespace std;

int main()
{
    const char* data_text = R"LITERAL(
BANANA TA GG GZ 95 5G
APPLE AF GC CF CG CA
ORANGE CG CG VG GL CD
GRAPE CG CG VG GL CD
)LITERAL";

    // Imitate file stream
    istringstream iss(data_text);
    
    map<string, string> fruit_to_rest;
    map<string, int> rest_seen;
    string fruit;
    string rest;
    
    for ( ; iss>>fruit; )
    {
        getline(iss, rest);
        fruit_to_rest[fruit] = rest;
        rest_seen[rest] += 1;
        
        //cout << "found fruit \""<<fruit << "\" and rest of string is \"" << rest << "\".\n";
    }

    cout << "\n\n";
    cout << "Enter 'q' to quit.\n";
    
    string search_term;
    while (true)
    {
        cout << "\nSearch term? ";
        cin >> search_term;
        if (search_term=="q")
            break;
    
        std::transform(search_term.begin(), search_term.end(), search_term.begin(), ::toupper);
        if (fruit_to_rest.count(search_term))
        {
            if (rest_seen[fruit_to_rest[search_term]] > 1)
            {
                cout << "Similar items: ";
                for (auto& item : fruit_to_rest)
                {
                    if (fruit_to_rest[search_term] == item.second)
                        cout << item.first << " ";
                }
                cout << endl;
            }
            cout << "Code for fruit "<<search_term<<" is " << fruit_to_rest[search_term] << endl;
        }
        else
        {
            cout << "Fruit \""<<search_term<<"\" not found.\n";
        }
    }

    return 0;
}


Last edited on Mar 29, 2018 at 7:49pm
Topic archived. No new replies allowed.