Thread: getline question

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Question getline question

    Hi,

    I'm trying to write a program to help me look up Chinese characters in the dictionary. It's really hard on the eyes to use the book-dictionary and it takes forever. I'm a newb C++ programmer w/out even a book to help me, just trying to learn from online tutorials.

    I'm wondering, when I use getline in the following piece of code, it reads from the text file the first line just fine but the next array is just garble. How do I move to the next line of the text file to read the next line into the next array?

    Thanks a million for your help!


    Code:
    if (a_file.is_open()) 
        {
         a_file.getline(radindex1, 40);
         a_file.getline(radindex2, 40);
         a_file.getline(radindex3, 40);

    Swaine777

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    8
    With the code you have provided, you are just reading the first line and that is it. You need to expand your code to read the next line of the file such as a loop.

    Below is an example of a program where it counts the number of names or lines contain in a text file. Let me know if you have any questions.

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    int main()
    {
        ifstream file;
        char s[100]; 
        int count = 0;
        file.open("c:\\temp\\output.txt");
    
        file.getline(s,100); // ATTEMPT
        while (file) // CHECK IF SUCCESS
        {
            cout << s << endl;
            count++;
            file.getline(s,100); // ATTEMPT
        }
        cout << "There were " << count
            << " names" << endl;
        return 0;
    }
    Last edited by pelp; 03-17-2003 at 07:35 PM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Question Feed a multi-dim. array with getline

    Hi,

    Here's the code I've been working on for my Chinese character look up program. Please remember I'm a rookie, teaching myself c++ :P I did get a book but it's too limited as far as getline() and multi-dimensional arrays. What's the best way to feed a multi-dimensional array with getline() reading a text/data file?

    BTW, thanks Pelp for the help on reading data into a one-dim. array.

    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <conio.h>
    
    void FeedRadArray(int numrad_strokes);
    void DisplayRads(int numrad_strokes);
    
    using namespace std;
    
    const int MaxRadsInRow = 51;
    
    int main()
    
    {
    
     int ns, 
         remaining_strokes, 
         rad_choice, 
         char_choice;
    
     cout<<"How many strokes are there in the radical? "<<endl;
    
     cin>>ns;
    
     FeedRadArray(ns);
    
     /*  send ns to a function that reads the radicals
         from the file into the radical array    */
    
     cout<<endl<<"Please choose from the following radicals:"<<endl;
    
     cin>>rad_choice;
    
     // cout<<"You chose "<<y<<". "<<radindex??[y]<<endl;  
    
     cout<<"How many strokes are there in the rest of the character? "<<endl;
    
     cin>>remaining_strokes;
    
     // DisplayHanzi(rad_choice, remaining strokes);
    
     /* Send rad_choice and remaining_strokes to a function that displays the radical and
        the characters under that radical with that many remaining strokes */
    
     cout<<"Which of the following is the character you're looking for? ";
    
     cin>>char_choice;
    
     // LookUpChar(choose_char);
    
     return 0;
    
    }
    
    void FeedRadArray(int numrad_strokes)
    
    {
      char radindex[12][MaxRadsInRow];
      ifstream a_file("c:\\Downloads\\C_Source\radicals3.txt");     //opens the file for reading
      a_file.getline(radindex, MaxRadsInRow);
      while (a_file)
      {
        a_file.getline(radindex, MaxRadsInRow);
      }  
      DisplayRads(numrad_strokes);
      cout<<endl; 
      a_file.close();  //don't forget this!     
    }
    
    void DisplayRads(int numrad_strokes)
    
    {
      int x = numrad_strokes - 1;  //PREPARES numrad_strokes FOR USE IN ARRAY
      for(int y=0; y<=MaxRadsInRow; y++)
        {
          if (y % 2 != 0)
            cout<<y+1<<". "<<radindex[x][y];
            else
              cout<<"     "<<y+1<<". "<<radindex[x][y];
        }
    }
    Thanks for any tips/suggestions you can throw my way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question About Getline
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2009, 05:13 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. A question about getline
    By jriano in forum C++ Programming
    Replies: 2
    Last Post: 06-24-2003, 02:21 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. getline question
    By Kings in forum C++ Programming
    Replies: 9
    Last Post: 01-20-2003, 01:01 PM