Outputting information from a text file

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.

This is what i have so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <string.h>

using namespace std;
int main() {

   ifstream read("code.txt");

   char user_input[500];
   cin.get(user_input, 500);
   for(int i = 0; i < strlen(user_input); i++)
       user_input[i] = toupper(user_input[i]);

   cout << endl << user_input << endl;
   return 0;
}


I just know how to handle lower and upper case input i just dont know how to do the rest.
Last edited on
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.
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
using namespace 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)? $
Last edited on
thanks i managed to modify the program and use it how i want it, but is there any way i can write the program without using the <map> library?
Topic archived. No new replies allowed.