Thread: Getline Help!

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    8

    Question Getline Help!

    Hi guys just trying to understand Getline and implement it in a problem im trying to solve.

    First off i have a program that is working:

    Code:
    #include <iostream.h>
    #include <string>
    #include <fstream>
    using namespace std;
    
    
    void main()
    {
    
    	ifstream fin("data.txt");
    	ofstream fout;
    
    	char name[30];
    	char jersey_number[10];
    	char best_time[10];
    	char sport[40];
    	char high_school[40];
    
    	while(!fin.getline(name, 30, '|').eof())
    	{
    		fin.getline(jersey_number, 10, '|'); 
    		fin.getline(best_time, 10);         
    		fin.getline(sport, 40, '|');         
    		fin.getline(high_school, 40);    
                    
    	        cout << jersey_number << best_time << sport << high_school << endl;
            }
    
    	
    }

    This works fine with the input data text file containing the following:

    Code:
    John|83|52.2
    swimming|Jefferson
    Jane|26|10.09
    sprinting|San Marin

    Now the problem! I tried to learn from this and make a new program, which should do almost the same thing, read data from a text file, and cout the information i want.

    The data in the text file is in a different format, for example:

    Code:
    firstName middleName surname
    38 47 38 27 36
    firstName middleName surname
    84 37 29 34 72
    Here is what i programmed, going from the first example:


    Code:
    #include <iostream.h>
    #include <string>
    #include <fstream>
    using namespace std;
    
    
    void main()
    {
    
    	ifstream fin("test.txt");
    	ofstream fout("output.txt");
    
    	char name[30];
    	char fullName[30];
    	char marks[30];
    
    	while(!fin.getline(name, 30, '\n').eof())
    	{
    		fin.getline(fullName, 30, '\n'); 
    		fin.getline(marks, 30, '\n');         
            
    		cout << fullName << " " << marks << endl;
    	}
    
    	
    }


    All this does is bring up the program window which just runs forever, what am i doing wrong?

    The different thing between the working program and the one that isnt working is that the working one uses "|" but 2nd one uses "\n" for end of each line i believe.

    ideas anyone or help?
    Last edited by fp123; 01-16-2006 at 06:43 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In both programs, the getline loop is broken. It should look like this:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    int main()
    {
    	using namespace std;
    
    	ifstream fin("test.txt");
    	ofstream fout("output.txt");
    
    	string fullName;
    	string marks;
    
    	while(getline(fin, fullName) && getline(fin, marks))
    	{
    		cout << fullName << " " << marks << endl;
    	}
    }
    Problems (some of these apply to the original as well):
    1) You used void main(). See the FAQ for why this is bad.
    2) You used character arrays instead of std::string. While not wrong per se, it's useless danger and additional work for you. By using string, for example, names can be longer than 29 characters.
    3) Never use eof() as a test in a file loop. Again, see the FAQ. The short version (and what happened, I think) is that if the stream enters an error state but not eof, the loop won't terminate.
    4) The getline call inside your while condition consumed the complete name. Thus, nothing was left for the second call of the name, and it read the marks instead. The marks read then read the name of the next line instead, and so on.
    5) You should always make the loop conditional on ALL reads, not just the first of a record.
    6) A very minor issue: there's no need to explicitely pass '\n' to getline; it's the default.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    Thank you Bee that was extremely informative, just going to have a mess about with my program now

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    8

    Question using getline in function?

    I have just been messing about trying to figure out how to put the getline into a function, as i want to use as many functions as possible in the program.

    Is it possible to put that line of code into a function and call it from the main( ), how would it be done?
    Code:
    #include <iostream.h>
    #include <string>
    #include <fstream>
    using namespace std;
    
    bool getInputFilename(char fname[])						
    {
    	ifstream fin;										
    
    	cout << "Please enter the filename for input : ";
    	cin >> fname;										
    														
    	fin.open(fname, ios::nocreate);						
    														
    	if (!fin.is_open())
    		return false;									 
    	
    	fin.close();
    	return true;										
    }
    
    
    bool getOutputFilename(char fname[])					
    {
    	ofstream fout;
    
    	cout << "Please enter the filename for output : ";
    	cin >> fname;										
    														
    	fout.open(fname);									
    
    	if (fout.fail())									
    		return false;
    
    	fout.close();
    	return true;										
    }
    
    
    int main()
    {
    	ifstream fin;
    	ofstream fout;
    
    	string fullName;
    	string marks;
    
    
    	char IFname[20], OFname[20];
    
    
    	while (!getInputFilename(IFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    	while (!getOutputFilename(OFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    
    	fout.open(OFname);
    	fin.open(IFname);
    	
    	while(getline(fin, fullName) && getline(fin, marks))
    	{
    		fout << fullName << " " << marks << endl;
    	}
    
    
    	fout.close();
    	fin.close();
    }
    highlighted in red the getline, which works perfectly in getting the information from text file and putting it into output file, just wondering if it can be done as a function.

    Also i only really need the "fullName" to be output, the "marks" need to be put into an array so i can bubble sort them, do some other maths, then output a result next to the fullName of each record?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    To put that (or anything) into a function, first take the code you want to move and put it into the function. Then look at the variables it uses. Do any of those variables have data that needs to be passed into your function (e.g. fin has already been opened outside your new function, so you have to pass it in as a parameter).

    Are there any variables that you only use for the code you moved to the new function? If so, you can move the declaration of those variables out of main and into the function (e.g. you don't use fullName anywhere but in the red code, so you can move it to the new function). In general you should declare your variables right when you are ready to use them instead of at the beginning of the function. If you had, it would be even easier to see what should be brought into the new function and what should be passed as a parameter from main.

    Finally, is there any data generated from the red code that needs to be used again in main. If there is, set the return type of the function so you can return that data. If there isn't, set the function's return type to void.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM