Thread: Why first getline doesnt work?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    19

    Why first getline doesnt work?

    I have written a program, which is supposed to read 3 lines from a text file, the first is char, second string and thirs an integer. The input.txt is:
    Code:
    i
    Arnold Schwarzenegger
    60
    And my code:
    Code:
    int main()
    {
    	ifstream data("input.txt");
    	string name;
    	int age;
    	char instr;
    
    	data >> instr;
    	getline(data, name);
    	data >> age;
    }
    However, the getline does not read anything from the file, and after that, when the program processes to the next line of code, the 'data >> age' statement tries to read the 'Arnold Schwarzenegger' field into the age variable...
    Why is that? Why does the getline read nothing? How do I make it work?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Getline is reading the newline after the i, since all you're doing with the first >> is reading a single character.

    You're generally better off reading each line (using getline) into a buffer, then parsing that buffer to see what it contains.

    Mixing and matching input styles is always a bug at some point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    have a call to the ignore function before your getline so that the '\n' from the previous line won't be parsed by the getline function causing it to read nothing and thus throwing off your data >> age line

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char * argv[])
    {
    	ifstream data("read.txt");
    	char * name = new char[256]; //same as char name[256]
    	int age;
    	char instr;
        if(data.is_open()){
            data >> instr;
            data.ignore(1, '\n');
            data.getline(name, 256, '\n');
            data >> age;
            cout << "instr -> " << instr
                 << endl << "name -> " << name
                 << endl << "age -> " << age;
        }
        else
            cout << "could not open file";
    
        cin.get();
        return 0;
    }
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by codeprada View Post
    have a call to the ignore function before your getline so that the '\n' from the previous line won't be parsed by the getline function causing it to read nothing and thus throwing off your data >> age line
    Gah! All C with some convenient C++ stuff mixed in. Let's fix this.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char * argv[])
    {
    	ifstream data("read.txt");
    	std::string name;
    	int age;
    	char instr;
        if(data.is_open()){
            data >> instr;
            data.ignore(1, '\n');
            getline(name, cin);
            data >> age;
            cout << "instr -> " << instr
                 << endl << "name -> " << name
                 << endl << "age -> " << age;
        }
        else
            cout << "could not open file";
    
        cin.get();
        return 0;
    }
    This is better. Though it still doesn't handle invalid input.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  2. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  3. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 11:53 AM
  4. Help with getline() and file reading
    By evilkillerfiggi in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2005, 03:49 PM
  5. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM