Archived

This topic is now archived and is closed to further replies.

Hades11

GetLine woes

Recommended Posts

I can''t find out why this is misbehaving. The problem is that it will read from a text file ten "words" then keep reading but the word objects are empty. Its not a problem with the text file because if I remove the first 10 words it still will only read in 10. I''m basiclly making a mini dictionary so i can practice using files and search methods. Thanks guys header:
#include <iostream>
using namespace std;
class word
{

  public:
      
      void dispWord();
      char word[20], def[700];
}; 

void word::dispWord()
{
cout<<"\nWord:\n"<
cpp:
#include <stdlib.h>
#include <cstring>
#include <fstream>
#include <iostream>
#include "dict.h"
using namespace std;
void readFile();
int main ()
{
readFile();
system("pause");
}
void readFile()
{
ifstream fin;
word *database= new word[23231];//23231
unsigned long i=0;
fin.open("a.txt");
while(i<50){
fin.getline(database.word,20,'')'' );
fin.getline(database[i].def,700,''\n'');
database[i].dispWord();
i++;
}
fin.close();
delete[] database;
} 

Share this post


Link to post
Share on other sites
I just tried this.. and it worked (with a 20 definition file)


#include <stdlib.h>
#include <cstring>
#include <fstream>
#include <iostream>
void readFile();
class word{
public:
void dispWord();
char word[20], def[700];
};

void word::dispWord(){
std::cout<<"Word: "<<word<<std::endl<<"Definition: "<<def<<std::endl<<std::endl;
}

int main ()
{
readFile();
system("pause");
}

void readFile()
{
std::ifstream fin;
word *database= new word[23231];//23231

unsigned long i=0;
fin.open("a.txt");

while(fin.peek()!=EOF&&i<23231){
fin.getline(database[i].word,20,'')'' );
fin.getline(database[i].def,700,''\n'');
database[i].dispWord();
i++;
}
fin.close();
delete[] database;
}


I would however recommend that you store the words and definitions in strings (to let words/definitions be arbitrarily long). Also store the words themselves (database) in a vector, to let the dictionary be arbitrarily large.

Share this post


Link to post
Share on other sites