Readin text line by line

This is a discussion on Readin text line by line within the C++ Programming forums, part of the General Programming Boards category; I am writing programs for a Advanced Programming class. The classes are merged witht the kiddies learning True-BASIC, the C++ ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Readin text line by line

    I am writing programs for a Advanced Programming class. The classes are merged witht the kiddies learning True-BASIC, the C++ kids learn basicly on our own and code the BASIC programs to C++.

    Being a noob at C++, I must inquire;

    This week the BASIC kids were working on READ-DATA segments in thier class... But me and the other C++ kids thin these would be equivalent to arrays or strings or maybe even a class, we have no idea where to start. Here is a breif summery of the program we are supposed to write:

    Write a program that has an internal datadase of 10 Colleges w/ mascots. Let the user input the number of colleges to display at one time... Here is what the output might look like;

    Code:

    Number of colleges to display: 2
    College State Mascot:
    ---------------------------------------
    UNL NE Huskers
    Greenbay MN Packers



    Here is my code so far if it helps;

    Code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    
    #define BUFFER_SIZE 1000
    
    //Using global variables incase needed in another fuction
    	FILE* fp;
    	//char fname[256];			//Filename.
    	int i;						// i and
    	int j;						// j are variables for the for-loop coming up.
    	int many;					// Number of colleges to display
    	char buff[BUFFER_SIZE+1];
    	char ch;
    
    using namespace std;
    int main () {
    	// Input
    	cout << "How many colleges would you like to display?: ";
    	cin >> many;
    	//File stuff
    	ifstream database ("college.db");
    	if (! database.is_open()){
    		cout << cout << "\n\n"
    			 << "Can't open the input file for reading..." << "\n"
    			 << "Make sure the input file exists,"
    			 << "or and application is not using it.\n\n";
    		system("PAUSE");
      }
    	while (! database.eof() ){
        database.getline (buff,1000);
        cout << buff << endl;
      }
    }
    	system("PAUSE");
      return 0;
    }
    I wan to use a for loop to read a corresponding line of the file to the screen.. eg: you type "two" for how many colleges you want to display... And When you hit "Enter" it reads two lines of the "colleg.db" file to the screnn... each line having the info for the college.

    -Thanks!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Um, isn't that just a basic for-loop, or did I miss something in your question?
    Code:
    for(int i = 0; i < NrOfColleges; i++)
    {
       std::getline(...);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    OK lets say....

    Information for one college is on one seperate line of the db file...
    10 College's info are in the .db file.

    I want to write a for loop that will diplay how ever many of lines lines in the db file as the user wants colleges displayed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Is there any way to read a line from a text file?
    By megareix in forum C Programming
    Replies: 13
    Last Post: 01-09-2009, 12:13 PM
  3. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  4. How do I make my edit box move text to the next line?
    By ElWhapo in forum Windows Programming
    Replies: 2
    Last Post: 01-04-2005, 10:21 PM
  5. text line termination
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-09-2001, 04:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21