Jump to content
  • Advertisement

Archived

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

Hades11

GetLine woes

This topic is 5390 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!