Hi, I want to write a program where it will read from text file and output something based on the input. I just need help in what I need to know or do to write the program. For example:
Text file includes this
1 2 3
BANANA 45 A4 5F 0H AH
APPLE 78 PL JAM LOP
ORANGE OGN 0H A6 LE
And the program will ask for a userinput for example "BANANA"(the program should be case sensitive too) and it will output "45 A4 5F OH AH" and store the output in a variable.
I think this program has already been written to completion in one of the other messages if you want to search the forum you will find it. it wasn't more than a week ago either.
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
usingnamespace std;
int main()
{
map<string,string> index;
string fruit, code;
// ifstream in( "data.txt" ); // you'll need this eventually
stringstream in( " BANANA 45 A4 5F 0H AH\n" // you can use this for now
" APPLE 78 PL JAM LOP\n"" ORANGE OGN 0H A6 LE\n" );
while( in >> fruit >> ws && getline( in, code ) ) index[fruit] = code; // put fruit and code in the map
while ( true )
{
cout << "What would you like ($ to stop)? ";
cin >> fruit;
if ( fruit == "$" ) return 0;
if ( index.count( fruit ) ) cout << index[fruit] << '\n';
else cout << "Item not found\n";
}
}
What would you like ($ to stop)? PLUM
Item not found
What would you like ($ to stop)? APPLE
78 PL JAM LOP
What would you like ($ to stop)? BANANA
45 A4 5F 0H AH
What would you like ($ to stop)? $