plz, urgent.. how to assign string from data

Helo..here i want to read data from a text file arranged in single column like:

dog
cow
crocodile
monkey

-and assign it to a string called 'string product'. Later i have to write code somemore to ask the user to type a word and search for that word in string product that i assign with data from file. So as a first step i typed a program like shown below but cant see anything when compile. may i know what is my wrong and suggestion to correct it. i am a beginner only.




#include <iostream>
#include <conio>
#include <fstream>
#include <string>

string product[10000];


int n, result=0,e=0;
ifstream open1;

int main ()
{
open1.open("dic.txt");

for ( n=0 ;open1 !='\0' ; n++ )
{
product;

}
for ( n=0 ;n< 1000 ; n++ )
{
cout<<product[n];
}

getch();
return 0;
}
If the file looks like your example, you can use getline(open1,product[n]); http://www.cplusplus.com/reference/iostream/istream/getline.html. And you're printing the first thousand element of product[], while this array may consist out of more or less filled elements. Maybe adding a variable to keep track of the number of words? (There're more gentle, efficentic solutions, like vectors, but if you're a beginner, I woud stay away from that).
You can do this to read the words from the file to the console.

1
2
3
4
5
6
7
int Index=0;
while(open1 !eof())
{
     open1>>Product[Index];
     cout<<Product[Index];
     Index++;
}


You can use the previous code to show you all the words. If you want a certain word, you will have to compare. You can do this code :)

1
2
3
4
5
6
7
8
9
10
int Index=0;
string str;
getline(cin,str);
while(open1 !eof())
{
     open1>>Product[Index];
     if(str.compare(Product[Index]) == 0)
          cout<<Product[Index];
     Index++;
}


This code is for comparing the word that the user will enter with the words that are in the dictionary.
Last edited on
http://www.cplusplus.com/forum/articles/6046/

cin is an istream so the code is the same when reading from a file.
cant compile...it says
call to undefined function 'getline'
'while' statement missing.

i dont know whats wrong..this is the full code.

#include <iostream>
#include <conio>
#include <fstream>
#include <string>

string product[10000];



ifstream open1;

int main ()
{
open1.open("dic.txt");

int Index=0;
string str;
getline(cin,str);
while(open1 !eof())
{
open1>>Product[Index];
if(str.compare(Product[Index]) == 0)
cout<<Product[Index];
Index++;

getch();
return 0;
}
below #include<string> put "using namespace std;"
Still cant compile and run.. got error as stated below..

getline(cin,str);---------------------------could not find match for std::getline
while(open1 !eof())----------------------'while' statement missing )

i am using Borland C++ to compile and run it but failed.
Re-read that link i sent you. Try to understand the code instead of just copying+pasting it.

It should be
1
2
3
while(getline(open1, str)) {
 Product[Index] = str;
}
Last edited on
still cant compile oso..the same error come...
Post your complete code again.
Topic archived. No new replies allowed.